Bug 19314 – Thread object destruction may result in UB
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-10-19T13:06:50Z
Last change time
2018-10-27T00:39:08Z
Assigned to
No Owner
Creator
Mihails Strasuns
Comments
Comment #0 by mihails.strasuns — 2018-10-19T13:06:50Z
Split from https://issues.dlang.org/show_bug.cgi?id=19288
Expectation is that after thread is detached from druntime, it becomes
responsibility of external environment to terminate it properly (for
example, by calling `join` from C/C++ code).
However, `Thread` object for that thread will still remain and will
eventually be collected by GC. Destructor tries to call `pthread_detach`
unconditionally but it is undefined behaviour if thread was alread
joined before (see `man pthread_detach`).
Comment #1 by github-bugzilla — 2018-10-27T00:39:07Z
Commit pushed to master at https://github.com/dlang/druntimehttps://github.com/dlang/druntime/commit/f60eb358ccbc14a1a5fc1774eab505ed0132e999
Fix crash in destructor of detached thread
Fix issue 19314
Expectation is that after thread is detached from druntime, it becomes
responsibility of external environment to terminate it properly (for
example, by calling `join` from C/C++ code).
However, `Thread` object for that thread will still remain and will
eventually be collected by GC. Destructor tries to call `pthread_detach`
unconditionally but it is undefined behaviour if thread was already
joined before (see `man pthread_detach`).