diff --git a/src/fish.cpp b/src/fish.cpp index 8223a9e3e..448b4c7ca 100644 --- a/src/fish.cpp +++ b/src/fish.cpp @@ -318,6 +318,14 @@ static int fish_parse_opt(int argc, char **argv, std::vector *cmds) /// Various things we need to initialize at run-time that don't really fit any of the other init /// routines. static void misc_init() { + // If stdout is open on a tty ensure stdio is unbuffered. That's because those functions might + // be intermixed with `write()` calls and we need to ensure the writes are not reordered. See + // issue #3748. + if (isatty(STDOUT_FILENO)) { + fflush(stdout); + setvbuf(stdout, NULL, _IONBF, 0); + } + #ifdef OS_IS_CYGWIN // MS Windows tty devices do not currently have either a read or write timestamp. Those // respective fields of `struct stat` are always the current time. Which means we can't diff --git a/src/reader.cpp b/src/reader.cpp index f7228843a..a12d2b189 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -689,9 +689,6 @@ void reader_write_title(const wcstring &cmd, bool reset_cursor_position) { // Put the cursor back at the beginning of the line (issue #2453). fputwc(L'\r', stdout); } - - // TODO: This should be removed when issue #3748 is fixed. - fflush(stdout); } /// Reexecute the prompt command. The output is inserted into data->prompt_buff.