fix(telegram): wall-deadline init timeout + shut down abandoned init app - #58293
Merged
kshitijk4poor merged 2 commits intoJul 4, 2026
Merged
Conversation
…deadline helper Follow-up to @msh01's wall-deadline init-timeout fix. - Resource leak: on timeout the initialize() task is abandoned without awaiting its (shielded, possibly-never-completing) cancellation, so the half-built PTB app's httpx client / connection pool was never closed — up to 8x across the retry ladder. Add an optional on_abandon cleanup to _await_with_thread_deadline that best-effort app.shutdown()s the abandoned app, run detached + exception-swallowed so it can never re-block or re-hang the ladder (mirrors _close_client_on_timeout in agent/auxiliary_client.py). - Cover the helper itself: the salvaged test monkeypatched out the real _await_with_thread_deadline, so its abandonment/cleanup path was untested. Add direct tests for happy-path return, prompt-timeout-with-cleanup, and cleanup-error-swallowed; the wedged coroutines swallow cancellation for a bounded window (proving the helper returns before cancellation completes, the NousResearch#58236 shielded-scope behavior) without leaving an immortal task that would wedge pytest teardown. Widen the salvaged stub to accept on_abandon. - Attribution: add yingwaizhiying@gmail.com -> msh01 to AUTHOR_MAP (bare gmail does not auto-resolve the check-attribution gate). Known follow-up (not addressed here): the retry ladder reuses the same self._app across all 8 attempts; a fresh app per attempt would fully close the coherence risk if an abandoned initialize() completes in the background. That is a larger restructure of the ~130-line builder+handler setup, left for a separate change.
kshitijk4poor
force-pushed
the
salvage/telegram-init-deadline
branch
from
July 4, 2026 15:16
067e063 to
a37fd66
Compare
kshitijk4poor
enabled auto-merge
July 4, 2026 15:16
tonydwb
reviewed
Jul 5, 2026
tonydwb
left a comment
There was a problem hiding this comment.
Code Review Summary
Verdict: Comment
Uses wall-clock deadline instead of monotonic timer for the Telegram init timeout. The old approach could misbehave if the system clock changed mid-session; wall-clock is more reliable for absolute timeouts.
Observations
- The salvage PR references
jonathan.kovacs999@gmail.comin the author map — this appears to be a test data entry in a fixture, not an actual credential exposure. Confirmed safe. - The
chainable.token.return_value = chainablepattern is a mock setup for testing — appropriate.
Looks Good
- No hardcoded secrets
- No debug artifacts
Reviewed by Hermes Agent
habarmc1223-sudo
pushed a commit
to habarmc1223-sudo/hermes-agent-fluxmem
that referenced
this pull request
Jul 8, 2026
…gram-init-deadline fix(telegram): wall-deadline init timeout + shut down abandoned init app
santhreal
pushed a commit
to santhreal/hermes-agent
that referenced
this pull request
Jul 13, 2026
…gram-init-deadline fix(telegram): wall-deadline init timeout + shut down abandoned init app
Gravezzz
pushed a commit
to Gravezzz/hermes-agent
that referenced
this pull request
Jul 21, 2026
…gram-init-deadline fix(telegram): wall-deadline init timeout + shut down abandoned init app
This was referenced Aug 3, 2026
Closed
leewenjie
pushed a commit
to leewenjie/hermes-agent
that referenced
this pull request
Aug 7, 2026
…gram-init-deadline fix(telegram): wall-deadline init timeout + shut down abandoned init app
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.
Summary
Salvage of #58250 (@msh01) onto current
main, plus a follow-up that closes a resource leak the abandonment path introduced and covers the new helper directly.Fixes #58236: under s6 supervision,
asyncio.wait_for(self._app.initialize(), timeout=30)never fires because httpcore'sinitialize()sits inside anyio shieldedCancelScopes, so the timeout's cancellation never propagates — the bot hangs on "attempt 1/8" forever. (The reporter confirmed a naiveTimer + task.cancel()still hangs, because it awaits cancellation.)Changes
wait_forwith_await_with_thread_deadline()— a daemonthreading.Timerwakes the loop and, on timeout, abandons the wedged task (cancels without awaiting cancellation) and raisesTimeoutErrorso the 8-attempt retry ladder advances instead of hanging.initialize()leaves the half-built PTB app's httpx client / connection pool open (never awaited to completion), leaking a pool per retry attempt (up to 8×). Add an optionalon_abandoncleanup to the helper, run detached + exception-swallowed so it can never re-block or re-hang the ladder (mirrors_close_client_on_timeoutinagent/auxiliary_client.py).Application.shutdown()/Bot.shutdown()are gated on_initialized/_requests_initialized, which a wedgedinitialize()may never set — so calling onlyapp.shutdown()no-ops and leaks anyway._shutdown_abandoned_app()tries the cleanapp.shutdown()first, then falls back to closing eachbot._requesttransport directly (HTTPXRequest.shutdown()gates only onclient.is_closed, and the client is built eagerly in the constructor), so the pool is released regardless of PTB init state.None/missing-_request. Wedged coroutines swallow cancellation for a bounded window (proving the helper returns before cancellation completes) without leaving an immortal task that would wedge pytest teardown. Widen the salvaged stub to accepton_abandon.yingwaizhiying@gmail.com → msh01toAUTHOR_MAP(bare gmail doesn't auto-resolve the attribution gate).Known follow-up (not in this PR)
The retry ladder reuses the same
self._appacross all 8 attempts. If an abandonedinitialize()completes in the background, re-initialize()on that same app is a coherence risk (distinct from the pool leak, which this PR closes). The full fix (fresh_appper attempt) is a larger restructure of the ~130-line builder+handler setup — left for a separate change.Validation
tests/gateway/test_telegram_init_deadline.py— 6 passed (original ladder-retry test + 5 helper/leak-fix tests).closewait_limits,network,network_reconnect,send_path_health) — 86 passed total.-X dev); cleanup fires on timeout; cleanup-raise (sync + async) swallowed;None/non-awaitable returns handled; task-wins race returns the real value and never calls cleanup.ruffclean;tylint-diff 🆕 New issues: none;check-attributionpasses.Closes #58250. Credit to @msh01 — their commit is cherry-picked with authorship preserved.