Skip to content

Initialize thread handle sentinel before starting Windows thread - #218

Merged
michaelrsweet merged 1 commit into
OpenPrinting:masterfrom
ManasBagul23:fix/thread-win32-create-race-pr
Nov 18, 2026
Merged

michaelrsweet merged 1 commit into
OpenPrinting:masterfrom
ManasBagul23:fix/thread-win32-create-race-pr

Conversation

@ManasBagul23

@ManasBagul23 ManasBagul23 commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

win32_wrapper waits for thread->h to change from (HANDLE)-1 before deciding whether it can free the thread object. Since the object is zero-allocated, that sentinel is not present while _beginthreadex starts the new thread. A fast-returning thread can observe zero, free the object, and race with cupsThreadCreate storing the real handle. Set the sentinel before calling _beginthreadex so the wait protects the handle assignment.

win32_wrapper() busy-waits on thread->h == (HANDLE)-1 to avoid tearing
down the thread object before cupsThreadCreate() has finished storing
the real handle, but the object comes from calloc() so thread->h starts
out as 0, not -1 - the sentinel the wait loop checks for was never
actually in place during the window it's meant to protect. If the new
thread's function returned quickly, win32_wrapper() would see h == 0,
conclude the thread was detached, and free the object while
cupsThreadCreate() could still be in the middle of writing to it. Set
the sentinel before starting the thread so the wait is real.
@michaelrsweet michaelrsweet self-assigned this Nov 18, 2026
@michaelrsweet michaelrsweet added bug Something isn't working platform issue Issue is specific to an OS or desktop labels Nov 18, 2026
@michaelrsweet michaelrsweet added this to the Stable milestone Nov 18, 2026
@michaelrsweet
michaelrsweet merged commit 11ebe9b into OpenPrinting:master Nov 18, 2026
6 checks passed
michaelrsweet added a commit that referenced this pull request Nov 18, 2026
@michaelrsweet

Copy link
Copy Markdown
Member

FYI, I tweaked the comments a bit in the function to make it clearer why the double assignment is necessary:

[master a74d071] Changelog and cleanup (Issue #218)

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working platform issue Issue is specific to an OS or desktop

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants