Thread overview
[Issue 19978] D sometimes just crashes on exit with daemon threads
Nov 25, 2019
Atila Neves
Nov 25, 2019
FeepingCreature
May 08, 2020
FeepingCreature
Dec 17, 2022
Iain Buclaw
Feb 21, 2023
RazvanN
Feb 21, 2023
FeepingCreature
Feb 22, 2023
Dlang Bot
November 25, 2019
https://issues.dlang.org/show_bug.cgi?id=19978

Atila Neves <atila.neves@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |atila.neves@gmail.com

--- Comment #1 from Atila Neves <atila.neves@gmail.com> ---
I copied the code, compiled it with dmd 2.089.0, and ran the shell script in the description. It's still running minutes later. No crashes to report on Arch Linux, kernel version 5.13.12-arch1-1.

--
November 25, 2019
https://issues.dlang.org/show_bug.cgi?id=19978

--- Comment #2 from FeepingCreature <default_357-line@yahoo.de> ---
Still reproducible for me with dmd/druntime/phobos master on 4.14.118 on Linux+NixOS.

Surely not a kernel bug? Maybe Arch changed something. If you're bored, try in a Ubuntu VM?

--
May 08, 2020
https://issues.dlang.org/show_bug.cgi?id=19978

--- Comment #3 from FeepingCreature <default_357-line@yahoo.de> ---
Okay, got it.

The problem is with a daemon thread, D doesn't join it when shutting down. As a result, the GC shutdown sequence deletes the thread's memory right under it.

Why do we do this anyway? (gc_term>os_mem_unmap) Why not let the OS handle the
frees?

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=19978

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P2

--
February 21, 2023
https://issues.dlang.org/show_bug.cgi?id=19978

RazvanN <razvan.nitu1305@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305@gmail.com

--- Comment #4 from RazvanN <razvan.nitu1305@gmail.com> ---
(In reply to FeepingCreature from comment #3)
> Okay, got it.
> 
> The problem is with a daemon thread, D doesn't join it when shutting down. As a result, the GC shutdown sequence deletes the thread's memory right under it.
> 
> Why do we do this anyway? (gc_term>os_mem_unmap) Why not let the OS handle
> the frees?

The problem is that we have (shared) static constructors/destructors to worry about. Currently, I haven't found any information about the behavior of daemon threads in the presence of module constructors, however, my expectation is that the behavior should be the same as with normal threads (although one could argue that this goes against the spirit of daemon threads - you create them and then you stop caring about them). If we just let the OS do the cleanup then we will not be able to call the static destructors.

I see 2 possible solutions to this:

1. Before tearing down the process, we just stop the daemon threads and call static destructors. Since the program is exiting, I don't see a problem since the OS would stop them anyway.

2. Daemon threads do not call static destructors (in the spirit of not caring about them) - I don't really like this since it deviates from the general rule.

Essentially, once you start tearing down the process, you cannot allow daemon threads to run, if you hope for graceful exit.

--
February 21, 2023
https://issues.dlang.org/show_bug.cgi?id=19978

--- Comment #5 from FeepingCreature <default_357-line@yahoo.de> ---
How do you run module dtors without stopping the threads anyways? I don't see how that'd ever be safe.

--
February 22, 2023
https://issues.dlang.org/show_bug.cgi?id=19978

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #6 from Dlang Bot <dlang-bot@dlang.rocks> ---
@RazvanN7 created dlang/dmd pull request #14907 "Fix Issue 19978 - D sometimes just crashes on exit with daemon threads" fixing this issue:

- Fix Issue 19978 - D sometimes just crashes on exit with daemon threads

https://github.com/dlang/dmd/pull/14907

--