Bug 19313 – Attaching external thread may result in segfault
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-10-19T12:57:52Z
Last change time
2019-02-01T10:14:21Z
Assigned to
No Owner
Creator
Mihails Strasuns
Comments
Comment #0 by mihails.strasuns — 2018-10-19T12:57:52Z
Split from https://issues.dlang.org/show_bug.cgi?id=19288 as on of sub-issues
Essence of the issue:
```
import core.sys.posix.pthread;
import core.memory;
extern(C)
void* entry_point(void*)
{
// in real world case this may be called through `thread_attachThis`:
GC.collect();
return null;
}
void main()
{
// ensure some garbage exists
auto x = new int[1000];
pthread_t thread;
auto status = pthread_create(&thread, null, &entry_point, null);
assert(status == 0);
pthread_join(thread, null);
}
```
Same applies to other operating systems. Problems comes from the fact that GC implementation is not ready for `Thread.getThis()` to be `null` and will crash in several places if this happens. And because `thread_attachThis` has to call `new Thread` to allocate new context, collection may happen before thread is actually registered.