Skip to content

thread: Fix race condition in initialization#350

Merged
lhmouse merged 2 commits into
masterfrom
thread
Jul 1, 2026
Merged

thread: Fix race condition in initialization#350
lhmouse merged 2 commits into
masterfrom
thread

Conversation

@lhmouse

@lhmouse lhmouse commented Jul 1, 2026

Copy link
Copy Markdown
Owner

ca3396f removed the initialization protocol.
There were idempotant writes to ensure __tid have a correct value before the
user-defined thread procedure, but __handle was left unsynchronized between
the new thread and the caller.

The fix is to make the new thread wait for the creator. The creator will not
wait for the new thread; if _MCF_thread_new() is called in DllMain(), it
would deadlock.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR restores a safe thread-initialization handshake to eliminate a race where the newly created thread could observe an unsynchronized __handle (and potentially __tid) before the creator had finished publishing them.

Changes:

  • Introduces a startup latch (__init_done) so the new thread waits until the creator signals initialization completion.
  • Updates thread startup to block on the latch and assert __tid/__handle have been published before running the user procedure.
  • Adjusts the CreateThread() thread-id output parameter cast in preparation for the new protocol.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
mcfgthread/thread.h Adds an internal startup latch field (under __MCF_THREAD_DETAILS) using event storage.
mcfgthread/src/thread.c Implements the “new thread waits for creator” initialization protocol and signals readiness after successful thread creation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread mcfgthread/src/thread.c
Comment thread mcfgthread/thread.h
Comment thread mcfgthread/src/thread.c
ca3396f removed the initialization protocol.
There were idempotant writes to ensure `__tid` have a correct value before the
user-defined thread procedure, but `__handle` was left unsynchronized between
the new thread and the caller.

The fix is to make the new thread wait for the creator. The creator will not
wait for the new thread; if `_MCF_thread_new()` is called in `DllMain()`, it
would deadlock.
@lhmouse
lhmouse merged commit f9fb0c9 into master Jul 1, 2026
15 checks passed
@lhmouse
lhmouse deleted the thread branch July 16, 2026 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants