fix: prevent duplicate dispatcher creation race condition in invoke_global_service

Concurrent coroutines could all pass the `if key in self.dispatchers` check
before any of them wrote the result back, because `await dispatcher.start()`
yields to the event loop. This caused multiple Pulsar consumers to be created
on the same shared subscription, distributing responses round-robin and
dropping ~2/3 of them — manifesting as a permanent spinner in the Workbench UI.

Apply a double-checked asyncio.Lock in both `invoke_global_service` and
`invoke_flow_service` so only one dispatcher is ever created per service key.
This commit is contained in:
Sreeram Venkatasubramanian 2026-03-26 14:22:38 +05:30
parent 3ccff800c7
commit 928bda41ae

View file

@ -116,6 +116,7 @@ class DispatcherManager:
self.flows = {} self.flows = {}
self.dispatchers = {} self.dispatchers = {}
self.dispatcher_lock = asyncio.Lock()
async def start_flow(self, id, flow): async def start_flow(self, id, flow):
logger.info(f"Starting flow {id}") logger.info(f"Starting flow {id}")
@ -163,10 +164,9 @@ class DispatcherManager:
key = (None, kind) key = (None, kind)
if key in self.dispatchers: if key not in self.dispatchers:
return await self.dispatchers[key].process(data, responder) async with self.dispatcher_lock:
if key not in self.dispatchers:
# Get queue overrides if specified for this service
request_queue = None request_queue = None
response_queue = None response_queue = None
if kind in self.queue_overrides: if kind in self.queue_overrides:
@ -183,10 +183,9 @@ class DispatcherManager:
) )
await dispatcher.start() await dispatcher.start()
self.dispatchers[key] = dispatcher self.dispatchers[key] = dispatcher
return await dispatcher.process(data, responder) return await self.dispatchers[key].process(data, responder)
def dispatch_flow_import(self): def dispatch_flow_import(self):
return self.process_flow_import return self.process_flow_import
@ -297,9 +296,9 @@ class DispatcherManager:
key = (flow, kind) key = (flow, kind)
if key in self.dispatchers: if key not in self.dispatchers:
return await self.dispatchers[key].process(data, responder) async with self.dispatcher_lock:
if key not in self.dispatchers:
intf_defs = self.flows[flow]["interfaces"] intf_defs = self.flows[flow]["interfaces"]
if kind not in intf_defs: if kind not in intf_defs:
@ -325,8 +324,7 @@ class DispatcherManager:
raise RuntimeError("Invalid kind") raise RuntimeError("Invalid kind")
await dispatcher.start() await dispatcher.start()
self.dispatchers[key] = dispatcher self.dispatchers[key] = dispatcher
return await dispatcher.process(data, responder) return await self.dispatchers[key].process(data, responder)