fix: prevent engine_destroy crash on stale task handle#100
Merged
ladvoc merged 1 commit intoJun 10, 2026
Merged
Conversation
engine_destroy() could crash with a LoadProhibited exception when the engine task self-deleted (vTaskDelete(NULL)) during the fixed 100ms shutdown delay. Both the task and engine_destroy() raced to delete the same task, so engine_destroy() could call vTaskDelete() on an already-freed handle. This commonly happened after a server-side disconnect, where in-flight reconnect events would wake the blocked task during the destroy window. Coordinate shutdown with a join semaphore instead: the task signals completion before self-deleting, and engine_destroy() enqueues a stop event to wake the (possibly blocked) task, then waits on the semaphore. The handle is only force-deleted as a last resort if the task fails to exit within a timeout. Fixes livekit#85
Collaborator
|
@anujdeshpande, thank you for your contribution—I will take a look. |
ladvoc
approved these changes
Jun 10, 2026
ladvoc
left a comment
Collaborator
There was a problem hiding this comment.
LGTM ✅, this will be included in the next release.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #85.
engine_destroy()could crash with aLoadProhibitedexception (vTaskDelete→uxListRemove) because both the engine task andengine_destroy()raced to delete the same task:engine_task()ends withvTaskDelete(NULL)(self-delete).engine_destroy()waited a fixedvTaskDelay(100ms)and then calledvTaskDelete(eng->task_handle).If the task happened to exit during that 100 ms window — common after a server-side disconnect, where in-flight reconnect/timer/peer events wake the task blocked on
xQueueReceive(portMAX_DELAY)— it self-deleted first, leavingeng->task_handlestale.engine_destroy()then calledvTaskDelete()on the freed handle and crashed, which made it impossible to safely free room resources and leaked ~100 KB of internal RAM per session.Fix
Replace the fixed delay + unconditional delete with an explicit join:
vTaskDelete(NULL).engine_destroy()enqueues a new internal_EV_STOPevent to wake the (possibly blocked) task so it observesis_running == falseand exits promptly, then waits on the semaphore.ENGINE_TASK_JOIN_TIMEOUT_MS(5 s).This removes the double-delete entirely: in the normal path only the task deletes itself, and
engine_destroy()just joins and clears the handle._EV_STOPis ignored by every state handler (default: break;→ event freed), so waking the task has no side effects on the state machine.Testing
minimalexample foresp32s3with ESP-IDF v5.5.1 — compiles cleanly with no warnings onengine.c, image created successfully._EV_STOPis a safe no-op for all states.The change is contained to
components/livekit/core/engine.c.