Skip to content

Fix pool_mutex initialization order on Windows#1107

Merged
raphael-proust merged 2 commits intomasterfrom
fix-extracted-from-1103
Apr 20, 2026
Merged

Fix pool_mutex initialization order on Windows#1107
raphael-proust merged 2 commits intomasterfrom
fix-extracted-from-1103

Conversation

@raphael-proust
Copy link
Copy Markdown
Collaborator

Move initialize_threading() to run before pool_mutex is accessed in lwt_unix_start_job, rather than only inside the DETACH/SWITCH case.

On Windows, CRITICAL_SECTION must be explicitly initialized via InitializeCriticalSection before use. Unlike pthreads where a zero-initialized mutex (PTHREAD_MUTEX_INITIALIZER) is valid, a zero-initialized CRITICAL_SECTION is invalid. The previous code could access pool_mutex before initialize_threading() was called, causing crashes or undefined behavior on Windows when the first job used async_method != NONE.

initialize_threading() is idempotent (guarded by threading_initialized flag), so calling it earlier is safe and has no effect on Unix platforms.

Move initialize_threading() to run before pool_mutex is accessed in
lwt_unix_start_job, rather than only inside the DETACH/SWITCH case.

On Windows, CRITICAL_SECTION must be explicitly initialized via
InitializeCriticalSection before use. Unlike pthreads where a
zero-initialized mutex (PTHREAD_MUTEX_INITIALIZER) is valid, a
zero-initialized CRITICAL_SECTION is invalid. The previous code could
access pool_mutex before initialize_threading() was called, causing
crashes or undefined behavior on Windows when the first job used
async_method != NONE.

initialize_threading() is idempotent (guarded by threading_initialized
flag), so calling it earlier is safe and has no effect on Unix platforms.
@raphael-proust
Copy link
Copy Markdown
Collaborator Author

this is the first commit of #1103 from @mtelvers

@raphael-proust raphael-proust merged commit 167a6ea into master Apr 20, 2026
18 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants