Skip to content

The agent that grows with you

fix(gateway): abort-aware parallel startup connects (restart race parity)

Teknium059e3464228d
diff --git a/gateway/run.py b/gateway/run.py
index b8ffe3eeffa..a3485ccb592 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -12293,10 +12293,60 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
return (p, adp, p_cfg, "ok" if ok else "failed", None)
if _pending_connects:
- _raw = await asyncio.gather(
- *(_connect_one_startup(p, c, a) for (p, c, a) in _pending_connects),
- return_exceptions=True,
- )
+ # Abort-aware concurrent wait (parity with the serial loop's
+ # between-platforms abort check): a restart/shutdown requested
+ # while connects are in flight must cancel the still-pending
+ # connects — no later platform may finish connecting — clean up
+ # the ones that already completed, and abort startup.
+ _task_map: dict = {}
+ for (p, c, a) in _pending_connects:
+ _t = asyncio.ensure_future(_connect_one_startup(p, c, a))
+ _task_map[_t] = (p, c, a)
+ _pending_tasks = set(_task_map)
+ _abort_mid_connect = False
+ while _pending_tasks:
+ _done, _pending_tasks = await asyncio.wait(
+ _pending_tasks, timeout=0.05
+ )
+ if _pending_tasks and self._startup_should_abort():
+ _abort_mid_connect = True
+ break
+ if _abort_mid_connect:
+ # Cancel and fully settle the in-flight connects FIRST, so a
+ # completed adapter's disconnect cannot unblock a sibling's
+ # connect() before the sibling is cancelled.
+ for _t in _pending_tasks:
+ _t.cancel()
+ await asyncio.gather(*_pending_tasks, return_exceptions=True)
+ for _t in _pending_tasks:
+ _p, _c, _a = _task_map[_t]
+ try:
+ await _a.cancel_background_tasks()
+ except Exception as e:
+ logger.debug(
+ "✗ %s background-task cancel error: %s", _p.value, e
+ )
+ await self._safe_adapter_disconnect(_a, _p)
+ # Tear down adapters whose connect already succeeded — they
+ # were never registered, so stop() won't reach them.
+ for _t, (_p, _c, _a) in _task_map.items():
+ if _t in _pending_tasks or _t.cancelled():
+ continue
+ _res = _t.exception() is None and _t.result() or None
+ if _res and _res[3] == "ok":
+ try:
+ await _a.cancel_background_tasks()
+ except Exception as e:
+ logger.debug(
+ "✗ %s background-task cancel error: %s",
+ _p.value, e,
+ )
+ await self._safe_adapter_disconnect(_a, _p)
+ await self._abort_startup_if_shutdown_requested()
+ return True
+ _raw = [
+ _t.exception() or _t.result() for _t in _task_map
+ ]
else:
_raw = []