Skip to content

Low-level threading support on Windows DLLs are broken #22399

Closed

Description

@limepoutine

In core.thread.osthread, there is some rather elaborate mechanism for dealing with DLL unloading.

// If the runtime is dynamically loaded as a DLL, there is a problem with
// threads still running when the DLL is supposed to be unloaded:
//
// - with the VC runtime starting with VS2015 (i.e. using the Universal CRT)
// a thread created with _beginthreadex increments the DLL reference count
// and decrements it when done, so that the DLL is no longer unloaded unless
// all the threads have terminated. With the DLL reference count held up
// by a thread that is only stopped by a signal from a static destructor or
// the termination of the runtime will cause the DLL to never be unloaded.
//
// - with the DigitalMars runtime and VC runtime up to VS2013, the thread
// continues to run, but crashes once the DLL is unloaded from memory as
// the code memory is no longer accessible. Stopping the threads is not possible
// from within the runtime termination as it is invoked from
// DllMain(DLL_PROCESS_DETACH) holding a lock that prevents threads from
// terminating.
//
// Solution: start a watchdog thread that keeps the DLL reference count above 0 and
// checks it periodically. If it is equal to 1 (plus the number of started threads), no
// external references to the DLL exist anymore, threads can be stopped
// and runtime termination and DLL unload can be invoked via FreeLibraryAndExitThread.
// Note: runtime termination is then performed by a different thread than at startup.
//
// Note: if the DLL is never unloaded, process termination kills all threads
// and signals their handles before unconditionally calling DllMain(DLL_PROCESS_DETACH).

When linking against a shared druntime_shared.dll, all calls to createLowLevelThread() execute the code compiled in the druntime DLL instead of in caller DLLs, so all additional reference counts are registered on druntime_shared.dll. As a result, cbDllUnload callbacks will only be called when druntime is unloaded. When non-runtime DLLs are unloaded, their running threads crash immediately.

I believe this is what causes the dllgc test to be very flaky: the test assumes term() to be called before dllgc.dll is unloaded. However, term() is never called during unloading of dllgc.dll, but instead during runtime cleanup. If the program exits before the watchdog thread calls term(), CI will be green, otherwise red. Also, the test is misusing createLowLevelThread() on GC-allocated objects. The GC is free to collect whatever the delegate references and causes a crash because the thread is not managed by the runtime.

Activity

  1. rainers commented on Jan 16, 2026

    @rainers
    Member

    Good catch. Either the dllgc test is invalid and we should specify that cbDllUnload only works if the runtime is linked statically into the DLL, or we need to increment (and monitor?) the DLL reference count for other DLLs, too. The DLL can be found by checking the function pointer of the delegate.

    Also, the test is misusing createLowLevelThread() on GC-allocated objects.

    Indeed, the reference to the Task object is stored in the delegate to run, but that is not stored in scanned memory. The tsk variables should be static instead. This is very unlikely to trigger, though, as there are two few allocations in this test to actually trigger a GC.

  2. added 5 commits that reference this issue on Jan 18, 2026
    725de10
    9fe9f41
    468e65d
    57b8d85
    a414119
  3. added a commit that references this issue on Jan 22, 2026
    6f03ad8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions