fix(gateway): offload handoff-watcher SQLite calls to avoid blocking the async heartbeat (#40695) - #51890
Merged
Conversation
… loop
The Discord gateway heartbeat stalled ('Shard ID None heartbeat blocked
for more than N seconds') because _handoff_watcher polled the synchronous,
blocking SQLite-backed SessionDB directly on the asyncio event loop every
2s. Each list_pending/claim/complete/fail call performed blocking disk I/O
on the loop thread, starving the Discord heartbeat coroutine.
Wrap every blocking SessionDB call inside the watcher loop in
asyncio.to_thread(...) so the SQLite work runs on a worker thread and the
event loop (and heartbeat) stays responsive. These four call sites are the
only synchronous self._session_db.* calls inside the watcher loop body.
Adds tests/gateway/test_handoff_watcher_async_db.py asserting the watcher
offloads its SessionDB calls via asyncio.to_thread (mutation-survivable:
reverting any to_thread wrap fails the corresponding assertion).
Fixes #40695
Co-authored-by: kshitijk4poor <82637225+kshitijk4poor@users.noreply.github.com>
Contributor
🔎 Lint report:
|
| Rule | Count |
|---|---|
unresolved-attribute |
2 |
unresolved-import |
1 |
First entries
run_agent.py:2984: [unresolved-attribute] unresolved-attribute: Object of type `Self@get_credits_spent_micros` has no attribute `_credits_session_start_micros`
tests/gateway/test_handoff_watcher_async_db.py:20: [unresolved-import] unresolved-import: Cannot resolve imported module `pytest`
tests/run_agent/test_credits_notices_toggle.py:76: [unresolved-attribute] unresolved-attribute: Unresolved attribute `_credits_session_start_micros` on type `AIAgent`
✅ Fixed issues (1):
| Rule | Count |
|---|---|
invalid-assignment |
1 |
First entries
tests/run_agent/test_credits_notices_toggle.py:76: [invalid-assignment] invalid-assignment: Object of type `None` is not assignable to attribute `_credits_session_start_micros` of type `int`
Unchanged: 5904 pre-existing issues carried over.
Diagnostics are surfaced as warnings — this check never fails the build.
Contributor
Related: salvage of #40782 (@r266-tech), re-applied to the current |
kshitijk4poor
enabled auto-merge
June 24, 2026 13:36
5 tasks
waefrebeorn
pushed a commit
to waefrebeorn/slermes
that referenced
this pull request
Jul 2, 2026
…-handoff-watcher-async fix(gateway): offload handoff-watcher SQLite calls to avoid blocking the async heartbeat (NousResearch#40695)
habarmc1223-sudo
pushed a commit
to habarmc1223-sudo/hermes-agent-fluxmem
that referenced
this pull request
Jul 8, 2026
…-handoff-watcher-async fix(gateway): offload handoff-watcher SQLite calls to avoid blocking the async heartbeat (NousResearch#40695)
santhreal
pushed a commit
to santhreal/hermes-agent
that referenced
this pull request
Jul 13, 2026
…-handoff-watcher-async fix(gateway): offload handoff-watcher SQLite calls to avoid blocking the async heartbeat (NousResearch#40695)
Gravezzz
pushed a commit
to Gravezzz/hermes-agent
that referenced
this pull request
Jul 21, 2026
…-handoff-watcher-async fix(gateway): offload handoff-watcher SQLite calls to avoid blocking the async heartbeat (NousResearch#40695)
leewenjie
pushed a commit
to leewenjie/hermes-agent
that referenced
this pull request
Aug 7, 2026
…-handoff-watcher-async fix(gateway): offload handoff-watcher SQLite calls to avoid blocking the async heartbeat (NousResearch#40695)
19 tasks
melon-xf
added a commit
to melon-xf/hermes-agent
that referenced
this pull request
Sep 3, 2026
…-handoff-watcher-async fix(gateway): offload handoff-watcher SQLite calls to avoid blocking the async heartbeat (NousResearch#40695)
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
Closes #40695 — the Discord gateway heartbeat is blocked by synchronous handoff SQLite polling (
discord.gateway: Shard ID None heartbeat blocked for more than N seconds)._handoff_watcher(anasync defbackground task) pollsSessionDBsynchronously every 2s on the platform asyncio loop. A slow/contended SQLite call stalls the loop and starves Discord heartbeats.Verified still live on current
main(gateway/run.py:5950): noto_thread/run_in_executoranywhere in the watcher.Fix
Offload all four blocking
SessionDBcalls inside the watcher loop viaasyncio.to_threadso they run off the event loop:list_pending_handoffs(),claim_handoff(...),complete_handoff(...),fail_handoff(...)These four are the only sync
self._session_db.*calls inside the watcher loop (verified). Themaybe_auto_prune_and_vacuumcall runs in__init__before the loop starts, so it is not a heartbeat hazard and is intentionally left alone.Salvage / attribution
Salvaged from #40782 (@r266-tech), whose
asyncio.to_threadapproach was correct but based on an older line location;_handoff_watcherhas since moved togateway/run.py:5950. Re-applied to the current location, authorship preserved. (Supersedes the #40695 cluster: #40853, #40974. Note: #43504'shandoff_stateDB index is not the fix here — the query already plans fast; the hazard is event-loop blocking under contention — so it's not part of this PR.)Tests
tests/gateway/test_handoff_watcher_async_db.py— a recordingSessionDBasserts each call runs on a worker thread (not the event-loop thread), plus ato_threadspy, plus the error path (fail_handoff). Mutation-checked (reverting thelist_pending_handoffswrap fails the off-loop assertions). 3 pass.Fixes #40695