www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23593] New: core.thread: suspendAll doesn't wait for all if

https://issues.dlang.org/show_bug.cgi?id=23593

          Issue ID: 23593
           Summary: core.thread: suspendAll doesn't wait for all if
                    current thread has detached itself
           Product: D
           Version: D2
          Hardware: x86_64
                OS: FreeBSD
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: ibuclaw gdcproject.org

See the following for all the gory details.
https://github.com/dlang/dmd/pull/14710
https://github.com/dlang/dmd/pull/14753
https://github.com/dlang/dmd/pull/14754

Essentially
---
new Thread({
    thread_detachThis();
    GC.collect();
}).start();
---

The GC collection will call thread_suspendAll, and as the calling thread
detached itself, the suspend counter will be `1` (instead of `2`) - however the
counter is decremented before the entering the loop that calls sem_wait until
all threads have signalled resume - meaning there's an implicit assumption that
the current thread was counted - so it exits immediately rather than waiting
for the main thread to respond.

This means there's now a race condition between the detached thread running the
GC and the main thread setting up the Thread.tstack/bstack values.  If the GC
scan wins, then the main thread stack won't have any live data marked.

--
Dec 30 2022