fix: dedup, idempotent start, and ensurePrivateSession mutex for Feishu WebSocket#73
Open
hmhmdcy wants to merge 1 commit into
Open
fix: dedup, idempotent start, and ensurePrivateSession mutex for Feishu WebSocket#73hmhmdcy wants to merge 1 commit into
hmhmdcy wants to merge 1 commit into
Conversation
…Feishu WebSocket Three concurrency fixes for duplicate message handling on Feishu/Lark: 1. **client.ts - message dedup**: WebSocket may deliver the same im.message.receive_v1 event twice. Cache message_id in a Map with 30s TTL and skip duplicates. 2. **client.ts - idempotent start()**: Guard against repeated start() calls that could create multiple WSClient connections. 3. **p2p.ts - ensurePrivateSession mutex**: Per-chatId promise lock so concurrent handleMessage calls don't both enter the session-creation critical section (TOCTOU race), creating duplicate sessions. Without these fixes, every message on Feishu could trigger two replies and two OpenCode sessions.
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.
Problem
When sending a message on Feishu/Lark, the bridge replies twice and creates duplicate OpenCode sessions. Root cause: Feishu WebSocket may deliver the same
im.message.receive_v1event twice.Three concurrency gaps exist:
handleMessage()processes every event without checkingmessage_idstart()can be called multiple times, creating multipleWSClientconnectionshandleMessagecalls both readchatSessionStore.getSession()before either writes, then both create a new sessionChanges
1.
src/feishu/client.ts— message_id dedup cacheMap<string, number>tracking recentmessage_idvalues with 30s TTLhandleMessage(), skip if samemessage_idseen within TTL2.
src/feishu/client.ts— idempotent start()start()with early return ifconnectionStateis already'connected'or'connecting'3.
src/handlers/p2p.ts— ensurePrivateSession mutex_ensureSessionLocks(per-chatId promise lock)_ensurePrivateSessionImplensurePrivateSessionwrapper: if a lock exists forchatId, await the pending promise instead of entering the critical sectionTesting