Skip to content

Fatal error in GC: SuspendThread failed due to race condition #303

Closed

Description

@tobil4sk

From: #281 (comment)

It looks like this happens because the thread is deleted by DLLMain
https://github.com/ivmai/bdwgc/blob/2558568aceaf7fc5cc64cf87e244cbcfd7f9bd53/win32_threads.c#L3009

Somehow this happens at the same time as the GC_gcollect call within neko_gc_major() while neko is shutting down, which also tries to access the same thread to suspend it.

Can be reproduced by running this program repeatedly:

function main() {
	final lock = new sys.thread.Lock();
	sys.thread.Thread.create(function() {
		lock.release();
	});
	lock.wait();
}

Here is a minimal sample. For this to reproduce the following conditions must be met:

  • App must contain thread
  • Thread must complete before the main thread
  • DllMain call for detaching thread must be interleaved with a GC_gcollect call in neko_gc_major()

See: bdwgc/bdwgc#704

Activity

  1. tobil4sk commented on Dec 23, 2025

    @tobil4sk
    MemberAuthor

    This original issue should have been fixed with 8.2.10 (or 7.6.24). I tested with boehmgc 8.2.10 (#307) and the error appears to have gone away:

    Fatal error in GC
    SuspendThread loop failed
    

    The relevant upstream patches are:

    However, in this version there is still a race condition which can cause thread creation to hang:

    function main() {
        while (true) {
            final lock = new sys.thread.Lock();
            Sys.print("Creating thread\r");
            sys.thread.Thread.create(function() {
                Sys.print("               \r");
                Sys.print(Date.now().getSeconds() + "\r");
                lock.release();
            });
            lock.wait();
            neko.vm.Gc.run(true);
        }
    }

    In this snippet, it eventually gets stuck on the "Creating thread" message.

    Further information is found in bdwgc/bdwgc#710.

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