Skip to content

[Proxying] Do not allocate while holding em_task_queue mutex - #28457

Draft
sbc100 wants to merge 1 commit into
emscripten-core:mainfrom
sbc100:fix_task_queue_send_deadlock
Draft

sbc100 wants to merge 1 commit into
emscripten-core:mainfrom
sbc100:fix_task_queue_send_deadlock

Conversation

@sbc100

@sbc100 sbc100 commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Like #28186 fixed for em_proxying_queue, em_task_queue must not call malloc or free while holding queue->mutex because allocator lock contention can run _emscripten_yield ->
emscripten_proxy_execute_queue, re-locking queue->mutex on the same thread or blocking another thread executing the queue.

Previously both em_task_queue_send and emscripten_thread_mailbox_send held queue->mutex across em_task_queue_enqueue -> em_task_queue_grow.

Move locking into em_task_queue_enqueue and drop queue->mutex around allocating the larger buffer and freeing the old one, re-checking queue state after re-acquiring the lock.

Extend test_pthread_proxy_deadlock with growth test cases covering the system queue, user queues, contended workers, and free.

Like emscripten-core#28186 fixed for `em_proxying_queue`, `em_task_queue` must not call
`malloc` or `free` while holding `queue->mutex` because allocator lock
contention can run `_emscripten_yield` ->
`emscripten_proxy_execute_queue`, re-locking `queue->mutex` on the same
thread or blocking another thread executing the queue.

Previously both `em_task_queue_send` and `emscripten_thread_mailbox_send`
held `queue->mutex` across `em_task_queue_enqueue` -> `em_task_queue_grow`.

Move locking into `em_task_queue_enqueue` and drop `queue->mutex` around
allocating the larger buffer and freeing the old one, re-checking queue
state after re-acquiring the lock.

Extend `test_pthread_proxy_deadlock` with growth test cases covering the
system queue, user queues, contended workers, and `free`.

This branch has not been deployed

No deployments
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