Bug 16979 – Race in druntime leads to undefined behaviour

Status
RESOLVED
Resolution
INVALID
Severity
critical
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
Linux
Creation time
2016-12-18T05:06:00Z
Last change time
2016-12-27T02:57:47Z
Assigned to
nobody
Creator
safety0ff.bugz
See also
https://issues.dlang.org/show_bug.cgi?id=15939

Comments

Comment #0 by safety0ff.bugz — 2016-12-18T05:06:23Z
This issue affects Posix (excl. Darwin) druntime implementation of thread suspend. Order of events: Thread 1 | Thread 2 thread_suspendAll ↳suspend(Thread 2) reads Thread 2.isRunning as true thread_cleanupHandler ↳ sets isRunning to false Thread 2 exits calls pthread_kill with the invalid Thread 2.m_addr This problem is not present on the normal exit path because Thread 1 holds the lock for the thread list and thread 2 would block trying to remove itself. Possible solutions: #1: Remove the thread from the list in the cleanup handler. There is a comment in the current code stating that it is safer to defer this operation, however, there is no full explanation & rationale for deferring. #2: Make Thread.suspend prevent the target thread from returning from thread_cleanupHandler until after it has called pthread_kill on it. This may be the cause of #15939.