Bug 2549482

Summary: 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
Product: [Fedora] Fedora Reporter: jorge.gonzalez
Component: mingw-crtAssignee: Kalev Lember <kalevlember>
Status: CLOSED ERRATA QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: medium Docs Contact:
Priority: unspecified    
Version: 44CC: kalevlember, manisandro, marcandre.lureau, rjones
Target Milestone: ---   
Target Release: ---   
Hardware: x86_64   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: ---
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2026-10-29 01:27:29 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Attachments:
Description Flags
test program
none
test dll
none
proposed patch none

Description jorge.gonzalez 2026-10-12 06:37:09 UTC
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

Comment 1 jorge.gonzalez 2026-10-12 06:38:53 UTC
Created attachment 2160862 [details]
test program

Comment 2 jorge.gonzalez 2026-10-12 06:39:13 UTC
Created attachment 2160863 [details]
test dll

Comment 3 jorge.gonzalez 2026-10-12 06:39:38 UTC
Created attachment 2160864 [details]
proposed patch

Comment 4 Sandro Mani 2026-10-17 08:47:39 UTC
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

Comment 5 jorge.gonzalez 2026-10-17 18:28:04 UTC
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 :-)

Comment 6 Richard W.M. Jones 2026-10-19 11:21:07 UTC
I've just (re-)subscribed, I'll post it later.

Comment 7 Richard W.M. Jones 2026-10-19 11:59:06 UTC
I posted it just now, hopefully it will appear in the ML archives soon.

Comment 8 jorge.gonzalez 2026-10-19 12:25:08 UTC
Thanks, really appreciated :-)

Comment 9 Richard W.M. Jones 2026-10-20 07:39:55 UTC
The patch is upstream now:
https://sourceforge.net/p/mingw-w64/mingw-w64/ci/c4ec7b6e63fcdcb9d5d9aebd7bf513c28b055cdf/

Comment 10 Fedora Update System 2026-10-20 08:30:27 UTC
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

Comment 11 Fedora Update System 2026-10-20 08:30:35 UTC
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

Comment 12 Fedora Update System 2026-10-21 01:59:41 UTC
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.

Comment 13 Fedora Update System 2026-10-21 02:22:43 UTC
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.

Comment 14 Fedora Update System 2026-10-29 01:27:29 UTC
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.

Comment 15 Fedora Update System 2026-10-31 18:48:41 UTC
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.