Skip to content

Commit 3cb8e5e

Browse files
tyan0dscho
authored andcommitted
Cygwin: fork: Call pthread::atforkchild () after other initializations
Previously, the callback registered by pthread_atfork() was called in pthread::atforkchild() before _my_tls.fixup_after_fork(). This caused misbehaviour if the callback used TLS-related functions. More specifically, cygwait() for a mutex at the beginning of fhandler_fifo_pipe::raw_write() failed because the event handle _my_tls.signal_arrived, which is used in cygwait() internally and designed to be initialized in _cygtls::fixup_after_fork(), was not yet initialized at that point. Due to this problem, subprocesses of CMake (versions >= 3.29.x) sometimes failed after the commit 7ed9adb ("Cygwin: pipe: Switch pipe mode to blocking mode by default, 2024-09-05"). This commit triggered the issue because it introduced cygwait() for the mutex in fhandler_fifo_pipe::raw_write(). This patch moves the pthread::atforkchild() at the end of the fork:: child(), i.e. after all initializations for child process is finished. The reason why the issue happens not always but sometimes: The event handle signal_arrived was never properly initialized when fhandler_fifo_pipe::raw_write() was called from the callback. As a result, its value was merely copied from the parent process during a fork() operation. Since the event signal_arrived was not created as inheritable, the handle value was fundamentally invalid. Despite this, the issue only occurred occasionally. This inconsistency was due to the handle value often coinciding with other existing handles because of its small value, such as 0x1ac. As evidence of this, in many cases where the issue did not manifest, the signal_arrived handle was not even an event handle. Addresses: https://cygwin.com/pipermail/cygwin/2025-March/257800.html Addresses: msys2/msys2-runtime#272 Fixes: f02b22d ("* fork.cc (frok::child): Change order of cleanup prior to return.") Reported-by: Christoph Reiter <[email protected]> Reviewed-by: Jeremy Drake <[email protected]> Signed-off-by: Takashi Yano <[email protected]> (cherry picked from commit 779e46b)
1 parent 21630fd commit 3cb8e5e

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

winsup/cygwin/fork.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ frok::child (volatile char * volatile here)
187187

188188
ForceCloseHandle1 (fork_info->forker_finished, forker_finished);
189189

190-
pthread::atforkchild ();
191190
cygbench ("fork-child");
192191
ld_preload ();
193192
fixup_hooks_after_fork ();
@@ -199,6 +198,7 @@ frok::child (volatile char * volatile here)
199198
CloseHandle (hParent);
200199
hParent = NULL;
201200
cygwin_finished_initializing = true;
201+
pthread::atforkchild ();
202202
return 0;
203203
}
204204

winsup/cygwin/release/3.6.1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ Fixes:
1111
in the SA_ONSTACK case, because locally-copied context on the normal
1212
stack area is not accessible from the signal handler.
1313
Addresses: https://cygwin.com/pipermail/cygwin/2025-March/257714.html
14+
15+
- Move pthread::atforkchild() at the end of fork::child(). This fixes
16+
subprocess failure in cmake (>= 3.29.x).
17+
Addresses: https://cygwin.com/pipermail/cygwin/2025-March/257800.html
18+
Addresses: https://github.com/msys2/msys2-runtime/issues/272

0 commit comments

Comments
 (0)