You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.9 KiB

​
​
​
​
​
​
​
​
Event heap: fix hangs on shutdown (signal / web exit) and dispatch deadlocks Why the shutdown could hang: the event heap was never stopped, and its thread called every handler's dispatchEvent() while holding the handler list mutex. ~mojoKernel deletes the node / module factories, filters, media helper ... - all of them event handlers - while the heap kept calling into them (half-destroyed objects, destroyed mutexes); the deleting thread then waited forever in removeHandler() for the mutex the dispatch thread held. A dispatchEvent() that created or deleted a handler deadlocked the dispatch thread itself (non-recursive mutex). basicEventHeap: - Handlers are called without the list locked (snapshot + "still registered" check per handler); removeHandler() waits until the handler is no longer being called (5 s cap, logged), except when called from that very dispatchEvent(). - New pause() / resume() / flush() / clearQueue() and destroyInstance() (stops the thread; leaves it alone if a handler is stuck instead of hanging); existingInstance(); getInstance() is thread-safe. basicEventHandler: - detachEvents(): stop delivery first thing in a destructor (the base destructor runs only after the derived members are gone); basicEventHandler(false) + attachEvents(): register at the end of the constructor (registration in the base constructor lets the heap call into an object whose members are not built yet). Defaults unchanged. - Removal never creates a new heap (late destructors after exit). la_mojo: - ~mojoKernel: deliver queued events (2 s), pause the heap before deleting the components, drop what was queued during the teardown; the next kernel (restart) resumes it. - mojoBackend: destroyInstance() before the log is deleted at exit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
19 hours ago
​
​
​
Event heap: fix hangs on shutdown (signal / web exit) and dispatch deadlocks Why the shutdown could hang: the event heap was never stopped, and its thread called every handler's dispatchEvent() while holding the handler list mutex. ~mojoKernel deletes the node / module factories, filters, media helper ... - all of them event handlers - while the heap kept calling into them (half-destroyed objects, destroyed mutexes); the deleting thread then waited forever in removeHandler() for the mutex the dispatch thread held. A dispatchEvent() that created or deleted a handler deadlocked the dispatch thread itself (non-recursive mutex). basicEventHeap: - Handlers are called without the list locked (snapshot + "still registered" check per handler); removeHandler() waits until the handler is no longer being called (5 s cap, logged), except when called from that very dispatchEvent(). - New pause() / resume() / flush() / clearQueue() and destroyInstance() (stops the thread; leaves it alone if a handler is stuck instead of hanging); existingInstance(); getInstance() is thread-safe. basicEventHandler: - detachEvents(): stop delivery first thing in a destructor (the base destructor runs only after the derived members are gone); basicEventHandler(false) + attachEvents(): register at the end of the constructor (registration in the base constructor lets the heap call into an object whose members are not built yet). Defaults unchanged. - Removal never creates a new heap (late destructors after exit). la_mojo: - ~mojoKernel: deliver queued events (2 s), pause the heap before deleting the components, drop what was queued during the teardown; the next kernel (restart) resumes it. - mojoBackend: destroyInstance() before the log is deleted at exit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
19 hours ago
​
​
​
​
Event heap: fix hangs on shutdown (signal / web exit) and dispatch deadlocks Why the shutdown could hang: the event heap was never stopped, and its thread called every handler's dispatchEvent() while holding the handler list mutex. ~mojoKernel deletes the node / module factories, filters, media helper ... - all of them event handlers - while the heap kept calling into them (half-destroyed objects, destroyed mutexes); the deleting thread then waited forever in removeHandler() for the mutex the dispatch thread held. A dispatchEvent() that created or deleted a handler deadlocked the dispatch thread itself (non-recursive mutex). basicEventHeap: - Handlers are called without the list locked (snapshot + "still registered" check per handler); removeHandler() waits until the handler is no longer being called (5 s cap, logged), except when called from that very dispatchEvent(). - New pause() / resume() / flush() / clearQueue() and destroyInstance() (stops the thread; leaves it alone if a handler is stuck instead of hanging); existingInstance(); getInstance() is thread-safe. basicEventHandler: - detachEvents(): stop delivery first thing in a destructor (the base destructor runs only after the derived members are gone); basicEventHandler(false) + attachEvents(): register at the end of the constructor (registration in the base constructor lets the heap call into an object whose members are not built yet). Defaults unchanged. - Removal never creates a new heap (late destructors after exit). la_mojo: - ~mojoKernel: deliver queued events (2 s), pause the heap before deleting the components, drop what was queued during the teardown; the next kernel (restart) resumes it. - mojoBackend: destroyInstance() before the log is deleted at exit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
19 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
Event heap: fix hangs on shutdown (signal / web exit) and dispatch deadlocks Why the shutdown could hang: the event heap was never stopped, and its thread called every handler's dispatchEvent() while holding the handler list mutex. ~mojoKernel deletes the node / module factories, filters, media helper ... - all of them event handlers - while the heap kept calling into them (half-destroyed objects, destroyed mutexes); the deleting thread then waited forever in removeHandler() for the mutex the dispatch thread held. A dispatchEvent() that created or deleted a handler deadlocked the dispatch thread itself (non-recursive mutex). basicEventHeap: - Handlers are called without the list locked (snapshot + "still registered" check per handler); removeHandler() waits until the handler is no longer being called (5 s cap, logged), except when called from that very dispatchEvent(). - New pause() / resume() / flush() / clearQueue() and destroyInstance() (stops the thread; leaves it alone if a handler is stuck instead of hanging); existingInstance(); getInstance() is thread-safe. basicEventHandler: - detachEvents(): stop delivery first thing in a destructor (the base destructor runs only after the derived members are gone); basicEventHandler(false) + attachEvents(): register at the end of the constructor (registration in the base constructor lets the heap call into an object whose members are not built yet). Defaults unchanged. - Removal never creates a new heap (late destructors after exit). la_mojo: - ~mojoKernel: deliver queued events (2 s), pause the heap before deleting the components, drop what was queued during the teardown; the next kernel (restart) resumes it. - mojoBackend: destroyInstance() before the log is deleted at exit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
19 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Event heap: fix hangs on shutdown (signal / web exit) and dispatch deadlocks Why the shutdown could hang: the event heap was never stopped, and its thread called every handler's dispatchEvent() while holding the handler list mutex. ~mojoKernel deletes the node / module factories, filters, media helper ... - all of them event handlers - while the heap kept calling into them (half-destroyed objects, destroyed mutexes); the deleting thread then waited forever in removeHandler() for the mutex the dispatch thread held. A dispatchEvent() that created or deleted a handler deadlocked the dispatch thread itself (non-recursive mutex). basicEventHeap: - Handlers are called without the list locked (snapshot + "still registered" check per handler); removeHandler() waits until the handler is no longer being called (5 s cap, logged), except when called from that very dispatchEvent(). - New pause() / resume() / flush() / clearQueue() and destroyInstance() (stops the thread; leaves it alone if a handler is stuck instead of hanging); existingInstance(); getInstance() is thread-safe. basicEventHandler: - detachEvents(): stop delivery first thing in a destructor (the base destructor runs only after the derived members are gone); basicEventHandler(false) + attachEvents(): register at the end of the constructor (registration in the base constructor lets the heap call into an object whose members are not built yet). Defaults unchanged. - Removal never creates a new heap (late destructors after exit). la_mojo: - ~mojoKernel: deliver queued events (2 s), pause the heap before deleting the components, drop what was queued during the teardown; the next kernel (restart) resumes it. - mojoBackend: destroyInstance() before the log is deleted at exit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
19 hours ago
​
​
​
Event heap: fix hangs on shutdown (signal / web exit) and dispatch deadlocks Why the shutdown could hang: the event heap was never stopped, and its thread called every handler's dispatchEvent() while holding the handler list mutex. ~mojoKernel deletes the node / module factories, filters, media helper ... - all of them event handlers - while the heap kept calling into them (half-destroyed objects, destroyed mutexes); the deleting thread then waited forever in removeHandler() for the mutex the dispatch thread held. A dispatchEvent() that created or deleted a handler deadlocked the dispatch thread itself (non-recursive mutex). basicEventHeap: - Handlers are called without the list locked (snapshot + "still registered" check per handler); removeHandler() waits until the handler is no longer being called (5 s cap, logged), except when called from that very dispatchEvent(). - New pause() / resume() / flush() / clearQueue() and destroyInstance() (stops the thread; leaves it alone if a handler is stuck instead of hanging); existingInstance(); getInstance() is thread-safe. basicEventHandler: - detachEvents(): stop delivery first thing in a destructor (the base destructor runs only after the derived members are gone); basicEventHandler(false) + attachEvents(): register at the end of the constructor (registration in the base constructor lets the heap call into an object whose members are not built yet). Defaults unchanged. - Removal never creates a new heap (late destructors after exit). la_mojo: - ~mojoKernel: deliver queued events (2 s), pause the heap before deleting the components, drop what was queued during the teardown; the next kernel (restart) resumes it. - mojoBackend: destroyInstance() before the log is deleted at exit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
19 hours ago
​
​
Event heap: fix hangs on shutdown (signal / web exit) and dispatch deadlocks Why the shutdown could hang: the event heap was never stopped, and its thread called every handler's dispatchEvent() while holding the handler list mutex. ~mojoKernel deletes the node / module factories, filters, media helper ... - all of them event handlers - while the heap kept calling into them (half-destroyed objects, destroyed mutexes); the deleting thread then waited forever in removeHandler() for the mutex the dispatch thread held. A dispatchEvent() that created or deleted a handler deadlocked the dispatch thread itself (non-recursive mutex). basicEventHeap: - Handlers are called without the list locked (snapshot + "still registered" check per handler); removeHandler() waits until the handler is no longer being called (5 s cap, logged), except when called from that very dispatchEvent(). - New pause() / resume() / flush() / clearQueue() and destroyInstance() (stops the thread; leaves it alone if a handler is stuck instead of hanging); existingInstance(); getInstance() is thread-safe. basicEventHandler: - detachEvents(): stop delivery first thing in a destructor (the base destructor runs only after the derived members are gone); basicEventHandler(false) + attachEvents(): register at the end of the constructor (registration in the base constructor lets the heap call into an object whose members are not built yet). Defaults unchanged. - Removal never creates a new heap (late destructors after exit). la_mojo: - ~mojoKernel: deliver queued events (2 s), pause the heap before deleting the components, drop what was queued during the teardown; the next kernel (restart) resumes it. - mojoBackend: destroyInstance() before the log is deleted at exit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
19 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Event heap: fix hangs on shutdown (signal / web exit) and dispatch deadlocks Why the shutdown could hang: the event heap was never stopped, and its thread called every handler's dispatchEvent() while holding the handler list mutex. ~mojoKernel deletes the node / module factories, filters, media helper ... - all of them event handlers - while the heap kept calling into them (half-destroyed objects, destroyed mutexes); the deleting thread then waited forever in removeHandler() for the mutex the dispatch thread held. A dispatchEvent() that created or deleted a handler deadlocked the dispatch thread itself (non-recursive mutex). basicEventHeap: - Handlers are called without the list locked (snapshot + "still registered" check per handler); removeHandler() waits until the handler is no longer being called (5 s cap, logged), except when called from that very dispatchEvent(). - New pause() / resume() / flush() / clearQueue() and destroyInstance() (stops the thread; leaves it alone if a handler is stuck instead of hanging); existingInstance(); getInstance() is thread-safe. basicEventHandler: - detachEvents(): stop delivery first thing in a destructor (the base destructor runs only after the derived members are gone); basicEventHandler(false) + attachEvents(): register at the end of the constructor (registration in the base constructor lets the heap call into an object whose members are not built yet). Defaults unchanged. - Removal never creates a new heap (late destructors after exit). la_mojo: - ~mojoKernel: deliver queued events (2 s), pause the heap before deleting the components, drop what was queued during the teardown; the next kernel (restart) resumes it. - mojoBackend: destroyInstance() before the log is deleted at exit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
19 hours ago
​
​
  1. #ifndef __BASICEVENTHEAP_H_INCL__
  2. #define __BASICEVENTHEAP_H_INCL__
  3. #include "basicEvent.h"
  4. #include "basicEventHandler.h"
  5. #include "basicLogCollection.h"
  6. #include "basicMutex.h"
  7. #include "basicThread.h"
  8. #include <condition_variable>
  9. #include <mutex>
  10. typedef std::vector<basicEventHandler *> V_EVT_HANDLER;
  11. // How long removeHandler() / pause() wait for a running dispatchEvent() before giving up (logged)
  12. #define EVENT_HEAP_WAIT_DISPATCH_MS 5000
  13. /**
  14. * @Description Singleton class to manage Events in the System
  15. *
  16. * Handlers are called WITHOUT the handler list locked, so a dispatchEvent() may create or delete
  17. * handlers (add/removeHandler) and another thread may delete a handler while events are dispatched:
  18. * removeHandler() returns only once the heap no longer calls that handler.
  19. *
  20. * Shutdown: pause() stops the dispatching (after the running dispatch finished) so objects can be
  21. * deleted safely, resume() continues, clearQueue() drops what was not delivered,
  22. * destroyInstance() stops the thread for good.
  23. *
  24. * @author Gagua
  25. */
  26. class basicEventHeap : public basicThread
  27. {
  28. private:
  29. basicLogCollection * m_pLog;
  30. basicMutex * m_pDispatchingMutex; // event queue
  31. std::mutex m_mHandlers; // m_vHandlers, m_pCurrentHandler, m_bPaused, m_bInDispatch
  32. std::condition_variable m_cvDispatch; // signalled when a handler call or a dispatch round ends
  33. V_EVT_HANDLER m_vHandlers;
  34. basicEventHandler * m_pCurrentHandler; // handler being called right now (NULL if none)
  35. bool m_bPaused;
  36. bool m_bInDispatch; // an event is being dispatched
  37. pthread_t m_tDispatchThread;
  38. pEventQueue m_vEventQueue;
  39. bool isDispatchThread(void);
  40. bool isRegistered(basicEventHandler * handler); // m_mHandlers held
  41. public:
  42. static basicEventHeap * getInstance();
  43. // The instance if it exists, NULL otherwise (never creates one)
  44. static basicEventHeap * existingInstance();
  45. // Stops the dispatch thread and drops undelivered events (end of the application)
  46. static void destroyInstance();
  47. virtual ~basicEventHeap();
  48. void addHandler(basicEventHandler * handler, bool atBottom = false);
  49. void removeHandler(basicEventHandler * handler);
  50. void broadcast(EVENT_CONTAINER * Evt);
  51. void moveHandlerBottom(basicEventHandler * handler);
  52. void moveHandlerTop(basicEventHandler * handler);
  53. // Waits (up to timeoutMs) until every queued event is delivered; false on timeout
  54. bool flush(uint32_t timeoutMs = 2000);
  55. // Stops dispatching; returns once the running dispatch round ended (false on timeout)
  56. bool pause(void);
  57. void resume(void);
  58. // Releases all events still queued (not delivered); returns how many
  59. size_t clearQueue(void);
  60. protected:
  61. basicEventHeap();
  62. void m_fThreadProc(void);
  63. private:
  64. static basicEventHeap * _instance;
  65. static std::mutex _instanceLock; // getInstance() may be called from several threads at once
  66. };
  67. #endif // __BASICEVENTHEAP_H_INCL__