Fedora Account System
Red Hat Associate
Red Hat Customer
winpthreads: pthread_detach of an already-ended thread recycles its _pthread_v while the thread's DLL_THREAD_DETACH still reaches it through _pthread_tls Mechanism: 1. `pthread_create_wrapper` (l.1524) runs the thread function. If the thread is still joinable (`tv->h != NULL`) when the function returns, i.e. `pthread_detach` has not run yet, the wrapper only sets `tv->ended = 1` (l.1582) and calls `_endthreadex`. The thread's TLS slot `_pthread_tls` still points at `tv`. 2. `pthread_detach` (l.1798) sees `tv->ended` and calls `push_pthread_mem(tv)` (l.1832): `memset` to zero, onto the reuse pool (l.318-341). 3. The next `pthread_create` (l.1604) pops the same struct and fills `func`, `ret_arg`, `ended = 0` for the NEW thread; `std::thread::detach()` then sets PTHREAD_CREATE_DETACHED. 4. The OLD thread's `DLL_THREAD_DETACH` callback `__dyn_tls_pthread` (l.439, branch l.487-503) runs late (CPU overload, or any delay between thread exit and the notification), reads the same struct through its own TLS slot, sees `ended == 0` and DETACHED, takes the "detached thread ended" branch and calls `push_pthread_mem` on the NEW thread's struct. 5. The new thread finds `tv->func == NULL` (l.1547) and exits without running its body (exit code 128, the wrapper's default), or, if the memset lands between the two reads, calls `func(NULL)` and crashes. Needs: a thread that ends before `pthread_detach` is called on it, then another `pthread_create` before the first one's DLL_THREAD_DETACH. A joined thread cannot trigger it (the join waits for the handle, signalled after the callbacks). Reproducible: Always Steps to Reproduce: ## Reproducer Deterministic: `probe.cpp` + `slowexit.c` (a DLL that imports winpthreads, so it is notified first, and sleeps 200 ms in its DLL_THREAD_DETACH). Loop: `std::thread(instant).detach()` twice in a row; the second never runs. Forced mode: 20/20 stalls at idle, every stalled thread has exited with code 128. Same with the thread joined: 0/20. (Run under wine 11; the loader-lock and notification-order model is wine's, not verified on Windows.) Natural, no forcing: 12 busy loops, load 12-32 Actual Results: `std::thread` + detach in a tight loop: 90/2300 threads never ran, all with exit code 128. `std::thread` + join: 0/2300. `CreateThread`: 0/1300. Expected Results: All threads run successfully Additional Information: To rebuild test programs and test: download all attachments to a directory, then, change into that dir and run: x86_64-w64-mingw32-gcc -O2 -shared -o slowexit.dll slowexit.c -lpthread -Wl,--out-implib,libslowexit.a x86_64-w64-mingw32-g++ -O2 -std=c++17 -o probe.exe probe.cpp -L. -lslowexit -lpthread cp /usr/x86_64-w64-mingw32/sys-root/mingw/bin/{libwinpthread-1,libstdc++-6,libgcc_s_seh-1}.dll . export WINEPREFIX=$PWD/wineprefix WINEDEBUG=-all wine probe.exe 40 force 3000 # the failing case wine probe.exe 40 forcej 3000 # control: same timing, thread joined instead of detached wine probe.exe 300 detach 3000 # failing without forcing; needs a busy machine wine probe.exe 300 join 3000 # control for that The three arguments are the number of threads to create, the mode, and how long to wait for each thread, in milliseconds. My results (machine load about 17-20 on 12 CPUs, from the review agents): Mode Stalls force 20 out of 20, all exited with code 128 forcej 0 detach 8 out of 300, all exited with code 128 join 0
Created attachment 2160862 [details] test program
Created attachment 2160863 [details] test dll
Created attachment 2160864 [details] proposed patch
Thanks, can you please propose this to upstream mingw-w64 by posting to their mailing list at [1]? [1] https://sourceforge.net/projects/mingw-w64/lists/mingw-w64-public
Sorry, I can't post to that list unless I'm subscribed, and I'd prefer not subscribing to yet another list I won't read. I'd be very glad to post it on some public bugtracker if they provided one, even if I have to register on it. But no more mailling lists, please :-)
I've just (re-)subscribed, I'll post it later.
I posted it just now, hopefully it will appear in the ML archives soon.
Thanks, really appreciated :-)
The patch is upstream now: https://sourceforge.net/p/mingw-w64/mingw-w64/ci/c4ec7b6e63fcdcb9d5d9aebd7bf513c28b055cdf/
FEDORA-2026-fda38dd4ed (mingw-winpthreads-13.0.0-4.fc44) has been submitted as an update to Fedora 44. https://bodhi.fedoraproject.org/updates/FEDORA-2026-fda38dd4ed
FEDORA-2026-549fb87622 (mingw-winpthreads-14.0.0-3.fc45) has been submitted as an update to Fedora 45. https://bodhi.fedoraproject.org/updates/FEDORA-2026-549fb87622
FEDORA-2026-549fb87622 has been pushed to the Fedora 45 testing repository. Soon you'll be able to install the update with the following command: `sudo dnf upgrade --enablerepo=updates-testing --refresh --advisory=FEDORA-2026-549fb87622` You can provide feedback for this update here: https://bodhi.fedoraproject.org/updates/FEDORA-2026-549fb87622 See also https://fedoraproject.org/wiki/QA:Updates_Testing for more information on how to test updates.
FEDORA-2026-fda38dd4ed has been pushed to the Fedora 44 testing repository. Soon you'll be able to install the update with the following command: `sudo dnf upgrade --enablerepo=updates-testing --refresh --advisory=FEDORA-2026-fda38dd4ed` You can provide feedback for this update here: https://bodhi.fedoraproject.org/updates/FEDORA-2026-fda38dd4ed See also https://fedoraproject.org/wiki/QA:Updates_Testing for more information on how to test updates.
FEDORA-2026-fda38dd4ed (mingw-winpthreads-13.0.0-4.fc44) has been pushed to the Fedora 44 stable repository. If problem still persists, please make note of it in this bug report.
FEDORA-2026-549fb87622 (mingw-winpthreads-14.0.0-3.fc45) has been pushed to the Fedora 45 stable repository. If problem still persists, please make note of it in this bug report.