fix(photon): stop sidecar-crash fatal handler from cancelling its own supervisor task - #73170
Closed
ygd58 wants to merge 1 commit into
Closed
fix(photon): stop sidecar-crash fatal handler from cancelling its own supervisor task#73170ygd58 wants to merge 1 commit into
ygd58 wants to merge 1 commit into
Conversation
… supervisor task Fixes NousResearch#73159. When the Photon sidecar exits unexpectedly, _supervise_sidecar() (running as self._sidecar_supervisor_task) correctly detects SIDECAR_CRASHED and calls self._notify_fatal_error(). The Gateway's fatal-error handler answers that by calling adapter.disconnect(), which calls _stop_sidecar() -- from INSIDE the very task that's currently executing this whole chain. _stop_sidecar()'s cleanup unconditionally cancelled self._sidecar_supervisor_task. Cancelling the currently-running task raises CancelledError at its own next await point (inside _notify_fatal_error() or _stop_sidecar() itself). Since asyncio.CancelledError inherits from BaseException (not Exception), the Gateway's `except Exception` guards around the fatal-error handler don't catch it -- the handler aborts before ever reaching the "queue platform for background reconnection" step. Photon then stays permanently in `retrying` state until the whole process is manually restarted, even though detection worked correctly and the underlying transient upstream outage had long since recovered. Fix: in _stop_sidecar()'s cleanup, check whether self._sidecar_supervisor_task is asyncio.current_task() before cancelling it. A task cannot legally cancel itself in any useful way anyway (the cancellation only takes effect at its own next await, which is exactly the corruption described above) -- when we're inside the supervisor's own call stack, it's already in the process of finishing on its own once _notify_fatal_error() returns, so skip the cancel and just clear the reference. This mirrors an existing precedent in the same file: disconnect() already guards its OTHER task-cancellation (self._sidecar_health_task) the same way (`if task is not asyncio.current_task()`), just not the supervisor task in _stop_sidecar(). Per the issue's own note that existing tests mock out _notify_fatal_error() entirely (so this integration chain was never exercised), added two tests that drive the REAL chain: one runs _supervise_sidecar() as an actual asyncio task with a real _notify_fatal_error() that calls the real disconnect() -> _stop_sidecar(), confirming the task completes without CancelledError and that the post-disconnect reconnect-queue step actually executes; a second confirms the OTHER call path (external cleanup, a different task) still correctly cancels a running supervisor exactly as before. Reverting only the adapter.py fix (keeping the new test) reproduces the exact CancelledError from the bug report, confirming this is a genuine regression test. 2 new tests pass; 113/113 in the full tests/plugins/platforms/photon/ directory (no regression to sidecar lifecycle, overflow recovery, or health-monitoring behavior).
Collaborator
Related: #72475 repairs the same Photon fatal-handoff self-cancellation by detaching notification, while this patch guards |
teknium1
added a commit
that referenced
this pull request
Jul 28, 2026
…isconnect() Follow-up widening of the #73170 pattern: apply the same asyncio.current_task() guard used for the health task (and now the supervisor task in _stop_sidecar) to the inbound-task cancel path in disconnect(), so an inbound task that triggers disconnect() cannot cancel-and-await itself.
teknium1
added a commit
that referenced
this pull request
Jul 28, 2026
…isconnect() Follow-up widening of the #73170 pattern: apply the same asyncio.current_task() guard used for the health task (and now the supervisor task in _stop_sidecar) to the inbound-task cancel path in disconnect(), so an inbound task that triggers disconnect() cannot cancel-and-await itself.
teknium1
added a commit
that referenced
this pull request
Jul 29, 2026
…isconnect() Follow-up widening of the #73170 pattern: apply the same asyncio.current_task() guard used for the health task (and now the supervisor task in _stop_sidecar) to the inbound-task cancel path in disconnect(), so an inbound task that triggers disconnect() cannot cancel-and-await itself.
teknium1
added a commit
that referenced
this pull request
Jul 29, 2026
…isconnect() Follow-up widening of the #73170 pattern: apply the same asyncio.current_task() guard used for the health task (and now the supervisor task in _stop_sidecar) to the inbound-task cancel path in disconnect(), so an inbound task that triggers disconnect() cannot cancel-and-await itself.
teknium1
added a commit
that referenced
this pull request
Jul 29, 2026
…isconnect() Follow-up widening of the #73170 pattern: apply the same asyncio.current_task() guard used for the health task (and now the supervisor task in _stop_sidecar) to the inbound-task cancel path in disconnect(), so an inbound task that triggers disconnect() cannot cancel-and-await itself.
Contributor
randlee
pushed a commit
to randlee/hermes-agent
that referenced
this pull request
Aug 11, 2026
…isconnect() Follow-up widening of the NousResearch#73170 pattern: apply the same asyncio.current_task() guard used for the health task (and now the supervisor task in _stop_sidecar) to the inbound-task cancel path in disconnect(), so an inbound task that triggers disconnect() cannot cancel-and-await itself.
33hodl
pushed a commit
to 33hodl/hermes-agent
that referenced
this pull request
Aug 12, 2026
…isconnect() Follow-up widening of the NousResearch#73170 pattern: apply the same asyncio.current_task() guard used for the health task (and now the supervisor task in _stop_sidecar) to the inbound-task cancel path in disconnect(), so an inbound task that triggers disconnect() cannot cancel-and-await itself.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Fixes #73159.
Problem
When the Photon sidecar exits unexpectedly,
_supervise_sidecar()(running asself._sidecar_supervisor_task) correctly detectsSIDECAR_CRASHEDand callsself._notify_fatal_error(). The Gateway's fatal-error handler answers by callingadapter.disconnect()->_stop_sidecar()-- from INSIDE the very task executing this whole chain._stop_sidecar()unconditionally cancelledself._sidecar_supervisor_task. Cancelling the currently-running task raisesCancelledErrorat its own next await point. SinceCancelledErrorinherits fromBaseException, the Gateway'sexcept Exceptionguards don't catch it -- the handler aborts before ever reaching the "queue for background reconnection" step. Photon then stays permanently dead until a manual restart.Fix
In
_stop_sidecar()'s cleanup, check whetherself._sidecar_supervisor_taskisasyncio.current_task()before cancelling it. When we're inside the supervisor's own call stack, it's already finishing on its own once_notify_fatal_error()returns, so skip the cancel and just clear the reference.This mirrors an existing precedent in the same file:
disconnect()already guards its OTHER task-cancellation (self._sidecar_health_task) the same way, just not the supervisor task in_stop_sidecar().Verification
Per the issue's note that existing tests mock out
_notify_fatal_error()(so this chain was never exercised), added two tests that drive the REAL chain: one confirms the task completes withoutCancelledErrorand the post-disconnect reconnect-queue step actually runs; a second confirms external cleanup (a different task) still correctly cancels a running supervisor. Reverting only the fix (keeping the test) reproduces the exactCancelledErrorfrom the bug report.2 new tests pass; 113/113 in the full
tests/plugins/platforms/photon/directory (no regression).