crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.26k stars 1.61k forks source link

Fix: GC safety in Thread#start #14558

Open ysbaddaden opened 2 months ago

ysbaddaden commented 2 months ago

Follow up to #14554 where a comment by @yxhuvud made me realize that Thread#start wasn't exactly GC safe, in that a GC collection could happen in parallel to a thread starting and it could free the Thread object before the thread is started or before the thread is properly shutdown.

The patch mostly moves the calls to add/remove the thread object from the linked list, along with some explanations.

Note: only the last commit is relevant, the rest is from #14554.

ysbaddaden commented 2 months ago

Interestingly that caused CI to segfault with -Dpreview_mt :raised_eyebrow:

Of course, no stacktrace and I can't reproduce.