Skip to content

Revert async cancellation behaviour to match musl#26549

Open
sbc100 wants to merge 1 commit intoemscripten-core:mainfrom
sbc100:restore_async_behaviour
Open

Revert async cancellation behaviour to match musl#26549
sbc100 wants to merge 1 commit intoemscripten-core:mainfrom
sbc100:restore_async_behaviour

Conversation

@sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Mar 25, 2026

This change is partial revert of #19963. We now match musl's behavior
which is that async cancellation does not work to cancel a thread
that is blocked in pthread_mutex_lock.

This reverts a few emscripten-specific patches to some very low level
parts of musl.

@sbc100 sbc100 force-pushed the restore_async_behaviour branch 2 times, most recently from 15c34b6 to ec4fbc2 Compare March 25, 2026 23:45
@sbc100 sbc100 requested a review from dschuff March 25, 2026 23:45
@sbc100 sbc100 force-pushed the restore_async_behaviour branch from ec4fbc2 to b6512ec Compare March 25, 2026 23:52
@sbc100 sbc100 requested a review from kripken March 25, 2026 23:53
This change is partial revert of emscripten-core#19963.  We now match musl's behavior
which is that async cancellation does *not* work to cancel a thread
that is blocked in pthread_mutx_lock.

This reverts a few emscripten-specific patches to some very low level
parts of musl.
@sbc100 sbc100 force-pushed the restore_async_behaviour branch from b6512ec to 5fae227 Compare March 25, 2026 23:55
@sbc100
Copy link
Collaborator Author

sbc100 commented Mar 25, 2026

From Gemini:

✦ The current changes revert Emscripten's asynchronous cancellation behavior to match musl's implementation, where pthread_mutex_lock (and timedlock) are not cancellation points, even when
PTHREAD_CANCEL_ASYNCHRONOUS is enabled.

Key Changes Reviewed:

  1. system/lib/libc/musl/src/thread/__timedwait.c:

    • Updated comments to better describe the _cp (cancellation point) suffix and how PTHREAD_CANCEL_MASKED behaves (emulating futex syscall returning ECANCELED).
    • Slightly refactored the timeout logic for clarity, returning ETIMEDOUT directly when the sleep duration is exhausted.
  2. system/lib/libc/musl/src/thread/pthread_cancel.c:

    • Removed Emscripten-specific #ifdef blocks that previously allowed async cancellation even when canceldisable was set. This aligns the logic with musl's standard behavior where cancellation is
      strictly governed by canceldisable.
  3. system/lib/pthread/emscripten_futex_wait.c:

    • Refined the cancelable flag logic: bool cancelable = !self->canceldisable && self->cancelasync;. This ensures that low-level futex waits only honor async cancellation when it's explicitly enabled
      and not masked.
  4. test/pthread/test_pthread_cancel_async.c:

    • Updated the test to verify that pthread_mutex_timedlock now correctly times out (returning ETIMEDOUT) instead of being interrupted by the cancellation request.
    • Added a pthread_testcancel() call after the timeout to confirm the thread is eventually cancelled at a valid cancellation point.
  5. test/test_posixtest.py:

    • Added test_pthread_setcanceltype_1_1 to the unsupported_noreturn list, noting that async cancellation within pthread_mutex_lock is no longer supported (matching musl).
  6. Code Size Updates:

    • Minor increases (~5 bytes in WASM) in test_codesize_minimal_pthreads.json and its memory-growth variant, likely due to the slightly more robust check in emscripten_futex_wait.

Conclusion:
The implementation is architecturally sound and achieves the goal of musl-compatibility for thread cancellation. The tests have been updated to reflect the new behavior, and the code size impact is
negligible.

@sbc100
Copy link
Collaborator Author

sbc100 commented Mar 26, 2026

This change paves the way for the final simplification in this series #26550

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.

1 participant