Heuristic:AGenUI AGenUI Reverse Order Destruction Invariant
| Knowledge Sources | |
|---|---|
| Domains | Concurrency, Software_Architecture |
| Last Updated | 2026-10-24 17:21 GMT |
Overview
During engine shutdown, the shared worker thread is destroyed first, before any subsystem it references — guaranteeing no queued task can run after its target object has been freed.
Description
The AGenUI engine uses a single shared worker thread (`MessageThread`) that processes queued tasks for all surface managers. Every surface operation (action dispatch, streaming, data model sync) is posted to this thread via `shared_ptr`-captured lambdas.
During `stop()`, the destruction order is strictly:
1. Worker thread destroyed first (`ThreadManager::destroyThread`) — this drains the queue and joins the thread, ensuring no task is in flight when objects are freed. 2. SurfaceManagers detached and uninitialized — swapped out under lock, then `uninit()` called on each. 3. MeasurementManager reset (`_measurementManager.reset()`). 4. Engine context cleared (`setEngineContext(nullptr)`). 5. Singleton modules destroyed in reverse creation order: ComponentPropertySpecManager → TemplateRegistry (stop + shutdown) → PathConfig → FunctionCallManager.
Within each `SurfaceManager::uninit()`, a similar reverse-order teardown applies: EventDispatcher is destroyed first (under lock), then StreamingContentParser, then SurfaceCoordinator.
Usage
Use this heuristic when:
- You are debugging a crash during engine or surface shutdown
- You are adding a new subsystem to the engine and need to determine its destruction order
- You need to understand why the worker thread is destroyed before surface managers
The Insight (Rule of Thumb)
- Action: Always destroy the worker thread before any object it references via posted lambdas.
- Value: `ThreadManager::getInstance().destroyThread(AGENUI_SHARED_THREAD_ID)` is the first call in `AGenUIEngine::stop()`, before any subsystem is freed.
- Trade-off: The thread join blocks `stop()` until the queue drains. This is acceptable — shutdown latency is bounded by the longest queued task.
- Cascade: Each level follows the same pattern: `SurfaceManager::uninit()` destroys in reverse creation order (dispatcher → streaming parser → surface coordinator).
Reasoning
The worker thread holds `shared_ptr<SurfaceManager>` captures in its queued lambdas. If the thread were destroyed after the SurfaceManagers, a queued task could execute after the SurfaceManager's `uninit()` but before its `shared_ptr` refcount drops to zero — accessing partially-destroyed state.
From `agenui_engine_impl.cpp:50-87`:
void AGenUIEngine::stop() {
_isRunning.store(false);
// Destroy the shared worker thread first to ensure:
// 1. All object instances remain valid while the thread is running
// 2. No thread is running when objects are destroyed
ThreadManager::getInstance().destroyThread(AGENUI_SHARED_THREAD_ID);
// Detach all SurfaceManagers under the lock, then uninit outside the lock.
std::map<int32_t, std::shared_ptr<SurfaceManager>> surfaceManagersToStop;
{
std::lock_guard<std::mutex> lock(_surfaceManagersMutex);
surfaceManagersToStop.swap(_surfaceManagers);
}
for (auto& pair : surfaceManagersToStop) {
pair.second->uninit();
}
surfaceManagersToStop.clear();
_measurementManager.reset();
// Clear engine context before destroying modules
setEngineContext(nullptr);
// Destroy single-instance modules in reverse order
SAFELY_DELETE(_componentPropertySpecManager);
// ...
SAFELY_DELETE(_functionCallManager);
}
From `agenui_surface_manager.cpp:59-75` (surface-level reverse order):
void SurfaceManager::uninit() {
// Tear down the dispatcher first (under lock)
{
std::lock_guard<std::mutex> mutexWrap(_cachedListenersMutex);
_cachedListeners.clear();
if (_dispatcher) {
_dispatcher->removeAllEventListeners();
SAFELY_DELETE(_dispatcher);
}
}
// Destroy in strict reverse order
destroyStreamingContentParser();
destroySurfaceCoordinator();
}
The pattern mirrors the construction order in `start()`: FunctionCallManager → ComponentPropertySpecManager → TemplateRegistry → PathConfig → ThreadManager → MeasurementManager → EngineContext. Destruction reverses this.