fix(gateway): offload /model switch off the event loop - #53603
Merged
Merged
Conversation
The Telegram/Discord /model command's actual switch calls switch_model() directly on the asyncio event loop. switch_model() can fall through to a synchronous models.dev HTTP fetch (requests.get, 15s timeout) on a cold or expired cache, freezing the gateway for up to 15s and dropping the Telegram connection while a user switches models. The picker provider-list and fallback text-list sites were already offloaded (#41289), but the two _switch_model() calls — the picker callback and the direct /model <name> path — were not. Wrap both in asyncio.to_thread. Closes #20525.
Contributor
🔎 Lint report:
|
| Rule | Count |
|---|---|
invalid-argument-type |
1 |
First entries
gateway/slash_commands.py:1463: [invalid-argument-type] invalid-argument-type: Argument to function `switch_model` is incorrect: Expected `dict[Unknown, Unknown]`, found `None | Unknown`
Unchanged: 6055 pre-existing issues carried over.
Diagnostics are surfaced as warnings — this check never fails the build.
This was referenced Jun 27, 2026
pai-scaffolde
pushed a commit
to Scaffolde/hermes-agent
that referenced
this pull request
Jun 28, 2026
…53603) The Telegram/Discord /model command's actual switch calls switch_model() directly on the asyncio event loop. switch_model() can fall through to a synchronous models.dev HTTP fetch (requests.get, 15s timeout) on a cold or expired cache, freezing the gateway for up to 15s and dropping the Telegram connection while a user switches models. The picker provider-list and fallback text-list sites were already offloaded (NousResearch#41289), but the two _switch_model() calls — the picker callback and the direct /model <name> path — were not. Wrap both in asyncio.to_thread. Closes NousResearch#20525.
waefrebeorn
pushed a commit
to waefrebeorn/slermes
that referenced
this pull request
Jul 2, 2026
…53603) The Telegram/Discord /model command's actual switch calls switch_model() directly on the asyncio event loop. switch_model() can fall through to a synchronous models.dev HTTP fetch (requests.get, 15s timeout) on a cold or expired cache, freezing the gateway for up to 15s and dropping the Telegram connection while a user switches models. The picker provider-list and fallback text-list sites were already offloaded (NousResearch#41289), but the two _switch_model() calls — the picker callback and the direct /model <name> path — were not. Wrap both in asyncio.to_thread. Closes NousResearch#20525.
habarmc1223-sudo
pushed a commit
to habarmc1223-sudo/hermes-agent-fluxmem
that referenced
this pull request
Jul 8, 2026
…53603) The Telegram/Discord /model command's actual switch calls switch_model() directly on the asyncio event loop. switch_model() can fall through to a synchronous models.dev HTTP fetch (requests.get, 15s timeout) on a cold or expired cache, freezing the gateway for up to 15s and dropping the Telegram connection while a user switches models. The picker provider-list and fallback text-list sites were already offloaded (NousResearch#41289), but the two _switch_model() calls — the picker callback and the direct /model <name> path — were not. Wrap both in asyncio.to_thread. Closes NousResearch#20525.
This was referenced Jul 10, 2026
santhreal
pushed a commit
to santhreal/hermes-agent
that referenced
this pull request
Jul 13, 2026
…53603) The Telegram/Discord /model command's actual switch calls switch_model() directly on the asyncio event loop. switch_model() can fall through to a synchronous models.dev HTTP fetch (requests.get, 15s timeout) on a cold or expired cache, freezing the gateway for up to 15s and dropping the Telegram connection while a user switches models. The picker provider-list and fallback text-list sites were already offloaded (NousResearch#41289), but the two _switch_model() calls — the picker callback and the direct /model <name> path — were not. Wrap both in asyncio.to_thread. Closes NousResearch#20525.
Gravezzz
pushed a commit
to Gravezzz/hermes-agent
that referenced
this pull request
Jul 21, 2026
…53603) The Telegram/Discord /model command's actual switch calls switch_model() directly on the asyncio event loop. switch_model() can fall through to a synchronous models.dev HTTP fetch (requests.get, 15s timeout) on a cold or expired cache, freezing the gateway for up to 15s and dropping the Telegram connection while a user switches models. The picker provider-list and fallback text-list sites were already offloaded (NousResearch#41289), but the two _switch_model() calls — the picker callback and the direct /model <name> path — were not. Wrap both in asyncio.to_thread. Closes NousResearch#20525.
leewenjie
pushed a commit
to leewenjie/hermes-agent
that referenced
this pull request
Aug 7, 2026
…53603) The Telegram/Discord /model command's actual switch calls switch_model() directly on the asyncio event loop. switch_model() can fall through to a synchronous models.dev HTTP fetch (requests.get, 15s timeout) on a cold or expired cache, freezing the gateway for up to 15s and dropping the Telegram connection while a user switches models. The picker provider-list and fallback text-list sites were already offloaded (NousResearch#41289), but the two _switch_model() calls — the picker callback and the direct /model <name> path — were not. Wrap both in asyncio.to_thread. Closes NousResearch#20525.
19 tasks
melon-xf
added a commit
to melon-xf/hermes-agent
that referenced
this pull request
Sep 3, 2026
…53603) The Telegram/Discord /model command's actual switch calls switch_model() directly on the asyncio event loop. switch_model() can fall through to a synchronous models.dev HTTP fetch (requests.get, 15s timeout) on a cold or expired cache, freezing the gateway for up to 15s and dropping the Telegram connection while a user switches models. The picker provider-list and fallback text-list sites were already offloaded (NousResearch#41289), but the two _switch_model() calls — the picker callback and the direct /model <name> path — were not. Wrap both in asyncio.to_thread. Closes NousResearch#20525.
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
The Telegram/Discord
/modelswitch no longer freezes the gateway event loop.Root cause: the
/modelcommand's actual switch calledswitch_model()directly on the asyncio event loop.switch_model()can fall through to a synchronousmodels.devHTTP fetch (requests.get, 15s timeout) on a cold/expired cache, stalling the gateway for up to 15s and dropping the Telegram connection mid-switch.The picker provider-list and fallback text-list sites were already offloaded in #41289, but the two
_switch_model()calls — the picker callback and the direct/model <name>path — were not.Changes
gateway/slash_commands.py: wrap both_switch_model()calls inasyncio.to_thread(picker callback_on_model_selected+ direct/model <name>path).tests/gateway/test_model_command_custom_providers.py: regression test proving the direct switch routesswitch_modelthroughto_thread.Validation
/model <name>switchswitch_model()on event loop → up to 15s freeze on cold cachetest_model_command_custom_providers.py)Closes #20525. Supersedes the stale author PRs #20530/#20979 (both 5700+ commits behind, targeting
gateway/run.pywhere the handler no longer lives — it moved togateway/slash_commands.py).Infographic