Bug 18815 – thread_attachThis crash

Status
RESOLVED
Resolution
WORKSFORME
Severity
regression
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2018-05-01T22:00:22Z
Last change time
2019-12-08T11:18:12Z
Keywords
C++, industry
Assigned to
No Owner
Creator
Manu

Comments

Comment #0 by turkeyman — 2018-05-01T22:00:22Z
C++ main() call rt_init create thread (using C++11 std::thread) from thread_entry_func: call into D code (via extern(C) function) call thread_attachThis to register the C++ thread with druntime *CRASH* It appears to invoke a GC collect in thread_attachThis(), which calls thread_suspendAll(), which calls Thread.getThis(), which returns null (because thread_attachThis hasn't finished), which is passed to suspend(), where it is de-referenced and segfault. Crashes every time. Every call to thread_attachThis() without fail.
Comment #1 by turkeyman — 2018-05-01T23:10:46Z
I just confirmed, this is a regression from 2.78 to 2.79. It runs when building with 2.78, newer releases segfault. main.cpp: --------- #include <thread> extern "C" void rt_init(); extern "C" void dfunc(); void thread_func() { dfunc(); } void main() { rt_init(); std::thread thread(thread_func); thread.join(); } test.d: ------- import std.stdio; extern(C) void dfunc() { import core.thread; thread_attachThis(); writeln("Hello\n"); }
Comment #2 by turkeyman — 2018-05-14T04:37:42Z
This is kinda critical...
Comment #3 by bugzilla — 2018-12-14T05:26:24Z
It is likely crashing because your main() is on the C++ side, and so the D runtime library never gets initialized.
Comment #4 by bugzilla — 2018-12-14T05:27:51Z
(In reply to Walter Bright from comment #3) > It is likely crashing because your main() is on the C++ side, and so the D > runtime library never gets initialized. Oh, I see you called rt_init().
Comment #5 by chalucha — 2019-03-22T13:50:10Z
Isn't this solved in bug 19313?
Comment #6 by iamthewilsonator — 2019-05-26T08:15:53Z
It would seem so.
Comment #7 by r.sagitario — 2019-12-08T11:18:12Z
>> Isn't this solved in bug 19313? > It would seem so. Indeed, I verified on Windows. Please note that you should also call thread_detachThis() for cleanup.