From b056ed6d985cf3b649a3b9f000ad228f543ff894 Mon Sep 17 00:00:00 2001 From: Vladislav Vaintroub Date: Mon, 20 Jan 2025 14:07:23 +0100 Subject: [PATCH] MDEV-35891 mtr, Windows - fix multi-process append for stdout and stderr This backports https://github.com/mysql/mysql-server/commit/e2f685972985, which works around perl issue https://github.com/Perl/perl5/issues/17570 MySQL fix ensures that FILE_APPEND_DATA is used in redirected stdout/stderr open flags, rather than FILE_GENERIC_WRITE. Only this gives lossless append behavior, if multiple processes use the same file. --- .../lib/My/SafeProcess/safe_process_win.cc | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/mysql-test/lib/My/SafeProcess/safe_process_win.cc b/mysql-test/lib/My/SafeProcess/safe_process_win.cc index 8a5bb60a3f5..6e1fd414287 100644 --- a/mysql-test/lib/My/SafeProcess/safe_process_win.cc +++ b/mysql-test/lib/My/SafeProcess/safe_process_win.cc @@ -152,6 +152,24 @@ void handle_signal (int signal) } } +/** + Sets the append flag (FILE_APPEND_DATA) so that the handle inherited by the + child process will be in append mode. + Workaround for perl bug https://github.com/Perl/perl5/issues/17570 +*/ +static void fix_file_append_flag_inheritance(DWORD std_handle) +{ + HANDLE old_handle = GetStdHandle(std_handle); + HANDLE new_handle = ReOpenFile(old_handle, FILE_APPEND_DATA, + FILE_SHARE_WRITE | FILE_SHARE_READ, 0); + if (new_handle != INVALID_HANDLE_VALUE) + { + SetHandleInformation(new_handle, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT); + SetStdHandle(std_handle, new_handle); + CloseHandle(old_handle); + } +} + int main(int argc, const char** argv ) { @@ -270,6 +288,9 @@ int main(int argc, const char** argv ) SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX); + fix_file_append_flag_inheritance(STD_OUTPUT_HANDLE); + fix_file_append_flag_inheritance(STD_ERROR_HANDLE); + #if 0 /* Setup stdin, stdout and stderr redirect */ si.dwFlags= STARTF_USESTDHANDLES;