Heuristic:AGenUI AGenUI Recursive Mutex Function Dispatch
| Knowledge Sources | |
|---|---|
| Domains | Concurrency, UI_Framework |
| Last Updated | 2026-10-24 17:21 GMT |
Overview
FunctionCallManager uses `std::recursive_mutex` for its dispatch lock, allowing `resolveCalls` to recursively invoke `executeFunctionCallSync` from within a function call's own execution without deadlocking.
Description
The `FunctionCallManager` guards all registry operations and synchronous dispatch with a single `std::recursive_mutex`. This is a deliberate design choice: the `resolveCalls` builtin resolves nested function call references in JSON by recursively calling `executeFunctionCallSync`. If a plain `std::mutex` were used, the inner call would deadlock on the outer call's held lock.
The recursive lock allows the same thread to acquire the mutex multiple times. The inner `executeFunctionCallSync` call finds the referenced function in the registry and dispatches it synchronously, all while the outer call's lock is still held. The platform function pointer is guaranteed valid during this window because `unregisterFunctionCall` (which runs on the main thread) cannot erase the entry until `callSync` returns.
Usage
Use this heuristic when:
- You are implementing a custom builtin that internally calls other registered functions via `executeFunctionCallSync`
- You are debugging a deadlock in function call dispatch — a plain `std::mutex` here would deadlock
- You need to understand why `FunctionCallManager` does not release the lock during platform `callSync`
The Insight (Rule of Thumb)
- Action: Use `std::recursive_mutex` (not `std::mutex`) for any registry whose dispatch method may be called recursively from within a registered function's own execution.
- Value: The lock is held across the entire `callSync` execution — this is intentional, not a bug.
- Trade-off: Recursive mutexes are slightly slower than plain mutexes and can mask design issues (deep recursion may indicate over-coupling). However, correctness is prioritized over micro-performance.
- Safety: Platform function pointers remain valid during dispatch because unregistration is blocked by the same lock.
Reasoning
The `resolveCalls` builtin walks a JSON tree, and for every `{"function": "name", "args": {...}}` node it encounters, it calls `executeFunctionCallSync` to resolve the reference. This happens while the outer `executeFunctionCallSync` is still on the stack holding the lock**.
From `agenui_functioncall_manager.cpp:71-100`:
FunctionCallResolution FunctionCallManager::executeFunctionCallSync(
const std::string& name, const FunctionCallContext& context, const nlohmann::json& args) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
// 1. Look up C++ functionCall first (shared_ptr, inherently safe)
FunctionCallPtr cppFunctionCall = findCppFunctionCall(name);
if (cppFunctionCall) {
return cppFunctionCall->execute(args, context); // ← may recurse back here
}
// 2. Fall back to platform functionCall
FunctionCallEntry* entry = findFunctionCall(name);
if (!entry || !entry->function) {
return FunctionCallResolution::createError("FunctionCall not found: " + name);
}
// 3. Execute platform functionCall synchronously while holding the lock.
// This ensures the pointer remains valid — unregisterFunctionCall (main thread)
// cannot erase the entry until callSync returns.
FunctionCallResult callResult = entry->function->callSync(context, args.dump());
// ...
}
The comment on line 87-89 explicitly documents the invariant: the lock is held across `callSync` so that `unregisterFunctionCall` cannot free the `IPlatformFunction*` pointer mid-call. A `std::recursive_mutex` makes this safe for nested dispatch.