fix(bot): supervise Feishu WebSocket connection to recover from silent SDK failures#3350
Open
WnareaJunior wants to merge 2 commits into
Open
fix(bot): supervise Feishu WebSocket connection to recover from silent SDK failures#3350WnareaJunior wants to merge 2 commits into
WnareaJunior wants to merge 2 commits into
Conversation
…mbedding setups - Added a new specification for one-command installer profiles targeting Codex and Claude Code users. - Implemented profiles for `codex`, `codex-min`, `claude-code`, and `claude-code-hybrid` to streamline installation and configuration. - Enhanced the CLI with new commands for service management and project initialization. - Introduced a bootstrap script to facilitate installation without prior Python setup. - Established per-project memory isolation for Claude Code users, ensuring data privacy across projects. - Documented the implementation plan and design decisions for future reference and clarity.
…t SDK failures The lark-oapi ws client's blocking start() never returns after startup: all receive/reconnect work runs in orphaned asyncio tasks whose failures are never observed. When the SDK's reconnect task dies (reconnect budget exhausted, ClientException during reconnect, internal lock leaked by _connect's early return, or a timeout-less blocking HTTP call wedging its event loop), the connection stays dead while start() keeps sleeping, so the channel's restart loop never fires and the bot silently stops responding until the process is restarted. Replace the fire-and-forget ws thread with supervised connection generations: each generation runs a fresh client on its own event loop, and a watchdog on the channel loop checks connection health, gives the SDK's auto-reconnect a grace period, then neutralizes the dead client (disables its reconnect, closes its connection bypassing the leak-prone internal lock, stops its loop) and rebuilds a clean generation - even if the old thread is stuck in a blocking call. stop() now performs a real teardown instead of calling a close() method the SDK client doesn't have. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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
Primary affected domain: Integration / Bot Runtime (
bot/vikingbot) — cc@yeshion23333The Feishu bot stops responding after running for a while and only recovers on process restart. Root cause: the lark-oapi ws client's blocking
start()never returns after startup — all receive/reconnect work runs in orphaned asyncio tasks whose failures are never observed. Four SDK defects (verified in lark-oapi 1.5.3 source,lark_oapi/ws/client.py) can leave the connection permanently dead whilestart()keeps sleeping:ServerUnreachableExceptioninside an orphaned task — reconnection silently stops forever.ClientExceptionduring reconnect (transient auth failure, exceed-connection-limit) is re-raised by_try_connect— same silent task death._connect()returns while holding its internal lock when a connection already exists (early return before thetry/finally) — later sends/ACKs/disconnects deadlock._get_conn_urluses syncrequests.postwith no timeout inside the event loop — a hang freezes the SDK's whole loop.In all four cases the old
run_wsretry loop infeishu.pynever fires becausestart()neither returns nor raises, so the bot looks alive but is deaf.This PR replaces the fire-and-forget ws thread with supervised connection generations:
lark.ws.Clienton its own event loop, so a poisoned client (dead reconnect task, leaked lock, wedged loop) can always be replaced with a clean one.start()unwinds) and rebuilds a clean generation — even when the old thread is stuck in a blocking call (it is abandoned with a warning).connection is down→Restarting Feishu WebSocket client: <reason>→connection recovered), and messages arriving with no running channel loop are no longer dropped silently.stop()performs a real teardown instead of calling aclose()method the SDK client does not have.Type of Change
Testing
New regression suite
bot/tests/test_feishu_ws_lifecycle.py(11 tests) uses fake ws clients so it runs with or without lark-oapi installed. It covers: watchdog rebuilds a dead-connection client and neutralizes the old generation; healthy connections are never churned;start()crashes are retried until success; a dead ws thread is respawned;stop()terminates thread and watchdog cleanly; a handler exception does not break later messages; duplicate deliveries are deduped.Verified against the real SDK (lark-oapi 1.5.3): with invalid credentials the thread cycles real
ClientExceptionfailures, the watchdog's forced rebuild fires, and shutdown is clean. To reproduce recovery end-to-end: run a connected bot, cut its network to Feishu for 5+ minutes (long enough to exhaust the SDK's reconnect budget), restore it — the bot recovers within ~60–75s without a restart, with the full log sequence above.ruff checkpasses on both files.Related Issues
Checklist
🤖 Generated with Claude Code