Event heap: fix hangs on shutdown and dispatch deadlocks #19

Merged
ClaudeAI merged 1 commits from fix/event-heap-shutdown into master 26 minutes ago
Collaborator

Summary

Fixes the event heap sometimes not exiting when la_mojo is stopped, whether by signal or by a web exit request. Also fixes a deadlock when a handler creates or deletes another handler while it handles an event.

Why it hung

  • The heap was never stopped. Its thread kept calling dispatchEvent() while ~mojoKernel deleted the node and module factories, the filters and the media helper. All of these are event handlers.
  • The handler-list mutex was held during the calls. The dispatch thread kept it locked while it called each handler. When a call into a half-destroyed object blocked, the deleting thread waited forever in removeHandler().
  • Self-deadlock. A dispatchEvent() that created or deleted a handler deadlocked the dispatch thread itself, because the mutex is not recursive.

Changes

basicEventHeap

  • No lock while calling handlers. The heap takes a snapshot of the list and checks that each handler is still registered before calling it.
  • Waiting on removal: removeHandler() waits until the handler is no longer being called. The wait is capped at 5 s and logged, and it is skipped when called from that same dispatchEvent().
  • New calls:
    • pause(), resume(), flush() and clearQueue();
    • destroyInstance(): stops the thread. If a handler is stuck, it logs this and does not wait, instead of hanging;
    • existingInstance().
  • Thread-safe getInstance().

basicEventHandler

  • detachEvents(): stops event delivery. A class can call it first thing in its own destructor, because the base destructor only runs after the derived members are destroyed.
  • basicEventHandler(false) + attachEvents(): registers at the end of the class's own constructor. Registering in the base constructor lets the heap call into an object whose members are not built yet.
  • Defaults unchanged: existing classes behave as before.
  • No new heap on removal: a late destructor after exit no longer creates a new heap.

la_mojo

  • ~mojoKernel:

    • delivers the queued events (up to 2 s);
    • pauses the heap before deleting the components;
    • drops what was queued during the teardown.

    On a restart, the next kernel resumes the heap.

  • mojoBackend: calls destroyInstance() at exit, before the log is deleted.

Testing

The real heap and handler code was tested under AddressSanitizer and ThreadSanitizer:

Test Original heap This PR
Handler creates or deletes a handler while handling an event hangs works
Shutdown (flush() + pause(), then delete handlers while events keep arriving) - no call after the pause
Handler deleted from another thread during its own dispatchEvent() - delete returns after the call finished
~7,000 handlers created/deleted concurrently with broadcasts, using attachEvents()/detachEvents() crashes (use-after-free) passes
destroyInstance() - returns

ThreadSanitizer reports nothing in the new code. Its remaining reports are in existing patterns: the plain basicThread status flag, and ~basicMutex unlocking a mutex it does not hold.

Builds (Debug86, full make -k):

  • la_mojo: compiles and links, except the three live555 files (not installable in the build container).
  • la_serva: builds and links completely.

Not run on the device: stop la_mojo by signal and by a web exit request, and check that it exits.

Not covered

  • Runtime create/delete: objects created or deleted while la_mojo runs (for example a node reload) can still receive a dispatchEvent() while partly built or destroyed. This is unchanged from before. The fix is to use attachEvents() / detachEvents() in those classes; that is not done in this PR.
  • la_serva: uses the same heap, but its shutdown sequence is unchanged.

Generated with Claude Code

https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16

## Summary Fixes the event heap sometimes not exiting when la_mojo is stopped, whether by signal or by a web exit request. Also fixes a deadlock when a handler creates or deletes another handler while it handles an event. ## Why it hung - **The heap was never stopped.** Its thread kept calling `dispatchEvent()` while `~mojoKernel` deleted the node and module factories, the filters and the media helper. All of these are event handlers. - **The handler-list mutex was held during the calls.** The dispatch thread kept it locked while it called each handler. When a call into a half-destroyed object blocked, the deleting thread waited forever in `removeHandler()`. - **Self-deadlock.** A `dispatchEvent()` that created or deleted a handler deadlocked the dispatch thread itself, because the mutex is not recursive. ## Changes ### `basicEventHeap` - **No lock while calling handlers.** The heap takes a snapshot of the list and checks that each handler is still registered before calling it. - **Waiting on removal:** `removeHandler()` waits until the handler is no longer being called. The wait is capped at 5 s and logged, and it is skipped when called from that same `dispatchEvent()`. - **New calls:** - `pause()`, `resume()`, `flush()` and `clearQueue()`; - `destroyInstance()`: stops the thread. If a handler is stuck, it logs this and does not wait, instead of hanging; - `existingInstance()`. - **Thread-safe `getInstance()`.** ### `basicEventHandler` - **`detachEvents()`:** stops event delivery. A class can call it first thing in its own destructor, because the base destructor only runs after the derived members are destroyed. - **`basicEventHandler(false)` + `attachEvents()`:** registers at the end of the class's own constructor. Registering in the base constructor lets the heap call into an object whose members are not built yet. - **Defaults unchanged:** existing classes behave as before. - **No new heap on removal:** a late destructor after exit no longer creates a new heap. ### la_mojo - **`~mojoKernel`:** - delivers the queued events (up to 2 s); - pauses the heap before deleting the components; - drops what was queued during the teardown. On a restart, the next kernel resumes the heap. - **`mojoBackend`:** calls `destroyInstance()` at exit, before the log is deleted. ## Testing The real heap and handler code was tested under AddressSanitizer and ThreadSanitizer: | Test | Original heap | This PR | |---|---|---| | Handler creates or deletes a handler while handling an event | hangs | works | | Shutdown (`flush()` + `pause()`, then delete handlers while events keep arriving) | - | no call after the pause | | Handler deleted from another thread during its own `dispatchEvent()` | - | delete returns after the call finished | | ~7,000 handlers created/deleted concurrently with broadcasts, using `attachEvents()`/`detachEvents()` | crashes (use-after-free) | passes | | `destroyInstance()` | - | returns | ThreadSanitizer reports nothing in the new code. Its remaining reports are in existing patterns: the plain `basicThread` status flag, and `~basicMutex` unlocking a mutex it does not hold. **Builds (Debug86, full `make -k`):** - **la_mojo:** compiles and links, except the three live555 files (not installable in the build container). - **la_serva:** builds and links completely. **Not run on the device:** stop la_mojo by signal and by a web exit request, and check that it exits. ## Not covered - **Runtime create/delete:** objects created or deleted while la_mojo runs (for example a node reload) can still receive a `dispatchEvent()` while partly built or destroyed. This is unchanged from before. The fix is to use `attachEvents()` / `detachEvents()` in those classes; that is not done in this PR. - **la_serva:** uses the same heap, but its shutdown sequence is unchanged. Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
ClaudeAI added 1 commit 27 minutes ago
a81bb8dc2c
Event heap: fix hangs on shutdown (signal / web exit) and dispatch deadlocks
ClaudeAI merged commit 66c5100717 into master 26 minutes ago
The pull request has been merged as 66c5100717.
Sign in to join this conversation.
No reviewers
No Label
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date

No due date set.

Dependencies

This pull request currently doesn't have any dependencies.

Loading…
There is no content yet.