June 13, 2019
https://issues.dlang.org/show_bug.cgi?id=19956

          Issue ID: 19956
           Summary: Subclassing Thread with synchronized (this) may
                    deadlock
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: druntime
          Assignee: nobody@puremagic.com
          Reporter: default_357-line@yahoo.de

Consider the following program:

import core.thread;
import core.time;

class BadThread : Thread
{
    this()
    {
        super(&run);
    }
    void run()
    {
        synchronized (this)
        {
            Thread.sleep(1.seconds);
            (new Thread({})).start;
        }
    }
}

void main() {
    (new BadThread).start;
    Thread.sleep(500.msecs);
}

This program should terminate after a second. However, it deadlocks.

What happens here? The sequence of events is such:
Thread 1: starts Thread 2
Thread 1: sleeps
Thread 2: locks BadThread
Thread 2: sleeps

Thread 1: wakes up
Thread 1: exits main, runs thread_joinAll
Thread 1: thread_joinAll: locks Thread.slock
Thread 1: thread_joinAll: checks BadThread.isDaemon
Thread 1: BadThread.isDaemon: tries to grab the monitor of BadThread, which is
locked in Thread 2
Thread 1 blocks.

Thread 2: wakes up
Thread 2: tries to start a new Thread
Thread 2: Thread.start: tries to grab slock
Thread 2 blocks.

And the program is deadlocked.

--