Skip to content

The agent that grows with you

fix(gateway): return a failed reconnect candidate's held inbound to its predecessor

kshitijk4poorba72e540d9c2
diff --git a/gateway/platforms/helpers.py b/gateway/platforms/helpers.py
index 09674f82e26..27bf671fad1 100644
--- a/gateway/platforms/helpers.py
+++ b/gateway/platforms/helpers.py
@@ -81,10 +81,11 @@ def carry_inbound_dedup(predecessor: Any, adapter: Any) -> None:
def hand_over_held_inbound(predecessor: Any, adapter: Any) -> None:
- """Move inbound the retired instance is holding to its now-published replacement (#132829).
+ """Move inbound ``predecessor`` is holding to ``adapter`` (#132829, #133399).
- Runs at publish time, not before connect: a candidate that fails to connect never owns the
- queue, and the predecessor keeps holding until a replacement is registered."""
+ The runner calls it at publish time (retired instance -> replacement) and after disposing a
+ failed reconnect candidate (candidate -> retained predecessor): a candidate that fails connect
+ can hold acked updates and salvage pending batches in disconnect(), which must not die with it."""
adopt = getattr(adapter, "adopt_held_inbound", None)
if predecessor is not None and callable(adopt):
adopt(predecessor)
diff --git a/gateway/run_adapters.py b/gateway/run_adapters.py
index 16e16169ab4..6b298972b56 100644
--- a/gateway/run_adapters.py
+++ b/gateway/run_adapters.py
@@ -870,10 +870,13 @@ class GatewayAdapterLifecycleMixin:
# __init__ stay open until the next GC pass — and aiohttp/SQLite handles don't get GC'd
# promptly, so 2 fds/retry leak at 300s backoff cap = ~12 fds/hour (#37011).
await _dispose_unused_adapter(adapter)
+ # Inbound it held during connect or salvaged in disconnect goes back to the predecessor.
+ hand_over_held_inbound(adapter, info.get("predecessor"))
except Exception as e:
if adapter is not None:
# An exception escaping connect leaves the adapter in the same unowned state.
await _dispose_unused_adapter(adapter)
+ hand_over_held_inbound(adapter, info.get("predecessor"))
# A reconnect exception is transient; keep retrying at the cap rather than auto-pausing.
backoff = self._bump_reconnect_backoff(platform, info, attempt, None, str(e))
logger.warning("Reconnect %s error: %s, next retry in %ds", platform.value, e, backoff)
@@ -1446,8 +1449,9 @@ class GatewayAdapterLifecycleMixin:
self._configure_profile_adapter(adapter, profile_name, platform)
success = await self._connect_adapter_with_timeout(adapter, platform, is_reconnect=True)
except BaseException:
- # Caller never sees this adapter; release its partial resources here.
+ # Caller never sees this adapter; release its partial resources and return its held inbound.
await self._safe_adapter_disconnect(adapter, platform)
+ hand_over_held_inbound(adapter, predecessor)
raise
return adapter, success
@@ -1489,8 +1493,9 @@ class GatewayAdapterLifecycleMixin:
platform.value, profile_name, exc_info=True)
return
# Not installed (newer reconnect won the slot, shutdown began, or connect failed):
- # release partial resources; stop only for a non-retryable fatal.
+ # release partial resources and return its held inbound; stop only for a non-retryable fatal.
await self._safe_adapter_disconnect(adapter, platform)
+ hand_over_held_inbound(adapter, predecessor)
if success or (
getattr(adapter, "has_fatal_error", False)
and not getattr(adapter, "fatal_error_retryable", True)
@@ -1499,6 +1504,7 @@ class GatewayAdapterLifecycleMixin:
except BaseException as exc:
if adapter is not None:
await self._safe_adapter_disconnect(adapter, platform)
+ hand_over_held_inbound(adapter, predecessor)
if not isinstance(exc, Exception):
raise # CancelledError (and other BaseExceptions) propagate after release
logger.debug(