Skip to content

fix(discord): isolate voice connections and close auto-join race - #87530

Merged
steipete merged 4 commits into
openclaw:mainfrom
geekhuashan:pr-discord-voice-autoojoin-group
Jul 5, 2026
Merged

steipete merged 4 commits into
openclaw:mainfrom
geekhuashan:pr-discord-voice-autoojoin-group

Conversation

@geekhuashan

@geekhuashan geekhuashan commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

OpenClaw can run multiple Discord bot accounts in one guild, but @discordjs/voice stores connections by (group, guildId) and defaults every caller to the same default group. One account can therefore reuse or destroy another account's voice connection. Auto-join can also miss startup when the Discord gateway is already READY before the voice listener is registered.

Why This Change Was Made

The connection owner now supplies a stable openclaw:<accountId> group for lookup, join, and cleanup. The gateway lifecycle also checks its initial readiness after taking ownership of voice cleanup. This closes the ordering race directly, keeps failure cleanup symmetric, and removes the earlier 6-, 8-, and 75-second timing workarounds.

User Impact

Discord accounts sharing a guild keep independent voice connections. Configured voice auto-join starts whether readiness happened just before listener registration or arrives later through READY/RESUMED. No configuration or command changes are required.

Evidence

Implementation

  • Scope every voice connection operation by account ID.
  • Start auto-join when an already-connected gateway enters lifecycle ownership.
  • Preserve READY and RESUMED listeners for later reconnects.
  • Remove all timer-based startup retries.

Release-note context and contributor credit are preserved here; the thanked changelog bullet is queued with the final batch to avoid active-main ordering races.

Copilot AI review requested due to automatic review settings May 28, 2026 05:04
@openclaw-barnacle openclaw-barnacle Bot added channel: discord Channel integration: discord size: XS triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 28, 2026
@clawsweeper

clawsweeper Bot commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the context here. I swept through the related work, and this is now duplicate or superseded.

Keep open: this PR targets a real Discord multi-account voice bug, but the current head is still not merge-ready because account-scoped voice connections are added without making the gateway adapter account-safe, lifecycle-started auto-join remains fire-and-forget during cleanup, and the after-fix proof is not a real multi-account Discord voice run.

Canonical path: Close this stale PR. The latest review rated it F, the branch still lacks merge-ready proof, and there has been no human follow-up after the durable review.

So I’m closing this here because the remaining work is already tracked in the canonical issue.

Review details

Best possible solution:

Close this stale PR. The latest review rated it F, the branch still lacks merge-ready proof, and there has been no human follow-up after the durable review.

Do we have a high-confidence way to reproduce the issue?

Yes for source-level reproduction: the PR adds per-account @discordjs/voice groups, while the unchanged adapter dispatch still forwards guild voice-state packets without checking the owning bot user id. I did not run a live two-account Discord voice session in this read-only review.

Is this the best way to solve the issue?

No. Grouping @discordjs/voice connections by account is the right layer for the registry collision, but the best fix must also carry ownership into adapter dispatch and make lifecycle-started auto-join owned through cleanup.

Security review:

Security review cleared: The diff touches Discord plugin runtime and tests only; I found no concrete secret-handling, dependency source, workflow, package, permission, or supply-chain regression.

AGENTS.md: found and applied where relevant.

What I checked:

  • stale F-rated PR: PR was opened 2026-05-28T05:04:37Z, is older than 30 days, and the latest review rated it F.
  • proof blocker: real behavior proof is insufficient and proof tier is F, so this branch is not merge-ready without contributor follow-up.
  • no human follow-up: live comments and timeline hydrated by apply contain no non-automation activity after the ClawSweeper review.

Likely related people:

  • steipete: Current-main blame for the Discord gateway dispatch/voice files points to the merged current-main import in feat(android): restore in-flight runs after reconnect #100384, and this account is assigned to the PR and authored the current head commits. (role: recent area contributor and current assignee; confidence: high; commits: f1641571f119, 4316fd71a126, 2293c0b3c6ec; files: extensions/discord/src/voice/manager.ts, extensions/discord/src/internal/gateway-dispatch.ts, extensions/discord/src/internal/voice.ts)
  • asock: Authored the recently merged Discord voice manager fix in [codex] fix discord missing voice state handling #90969, which touched the same voice manager area but for a distinct missing voice-state behavior. (role: recent adjacent contributor; confidence: medium; commits: 20163ee182ec, cfec99afa92f; files: extensions/discord/src/voice/manager.ts, extensions/discord/src/send.guild.ts)
  • scoootscooob: Authored the merged extension migration that introduced the Discord voice manager under extensions/discord/src, so they are useful historical context for the plugin boundary but not the recent voice-state logic. (role: migration author; confidence: low; commits: 5682ec37fada, d1c837b9c135; files: extensions/discord/src/voice/manager.ts)

Codex review notes: model internal, reasoning high; reviewed against 91f5a65df9f8.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 08e8c4d4cf

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".


const scheduleVoiceAutoJoin = (label: string, delayMs: number) => {
const startedAt = Date.now();
setTimeout(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Unref or cancel delayed auto-join timers

When voice is enabled and the Discord monitor exits before this 75s fallback fires (for example an abort/reload or a startup failure after the voice manager is created), the ref'ed timeout keeps Node's event loop alive even though lifecycle cleanup has already torn the manager down (extensions/discord/src/monitor/provider.lifecycle.ts:556 and extensions/discord/src/voice/manager.ts:347). The callback will mostly no-op later, but the process/test can hang until the longest delayed autoJoin fires; store these handles and clear them on cleanup or call unref() for fire-and-forget safety nets.

Useful? React with 👍 / 👎.

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels May 28, 2026
@clawsweeper

clawsweeper Bot commented May 28, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper PR egg: 🎁 locked until real behavior proof passes.

Details
  • No creature or rarity is rolled until proof passes.
  • Eggs are collectible flavor only; they do not affect labels, ratings, merge decisions, or automation.

@clawsweeper clawsweeper Bot added P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. labels May 28, 2026
@geekhuashan

Copy link
Copy Markdown
Contributor Author

Behavior proof & fix summary

Fixed in this update (commit 2920af3dac)

Real-world reproduction

Cherry is the 3rd Discord account; OpenClaw staggers startup by 20s per account. By the time Cherry's provider starts, the Discord READY event has already fired, so DiscordVoiceReadyListener never triggers the initial autoJoin. The delayed safety-net timers (8s/75s) were added to recover from exactly this scenario:

# gateway.log 2026-05-21 — local patch logs (labels differ from PR code, same logic)
[PATCH] delayed autoJoin fired after 8002ms voiceManager=true voiceEnabled=true
[discord] voice: joining guild=1478052227103850558 channel=1478932146138644500
          mode=agent-proxy agent=buddy
[PATCH] delayed autoJoin settled status=[]

# 75s fallback also fired when 8s join succeeded but voice stayed disconnected:
[PATCH] retry-after-timeout autoJoin fired after 75000ms voiceManager=true voiceEnabled=true
[discord] voice: joining guild=1478052227103850558 channel=1478932146138644500

The timers work correctly. The issue raised by Codex was about process-exit safety.

Fix

Added .unref() to all three delayed autoJoin timers:

  • scheduleVoiceAutoJoin("8s", 8000) in provider.ts
  • scheduleVoiceAutoJoin("75s", 75_000) in provider.ts
  • startAutoJoin() timer in manager.ts

.unref() means: if the monitor exits before the timer fires, Node's event loop is not kept alive by these handles. The timers still fire normally during the process lifetime. autoJoin() already guards against destroyed and !voiceEnabled managers (line 348).

Note on the Codex "first join succeeds → retry overwrites session" concern: join() already handles the already-connected-same-channel case at lines 510–534 by returning early with ok: true. So the retries are safe when the first join succeeded to the configured channel.

@geekhuashan
geekhuashan force-pushed the pr-discord-voice-autoojoin-group branch from 2920af3 to fb6b0ec Compare May 29, 2026 01:35
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels May 29, 2026
@geekhuashan

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented May 29, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@geekhuashan

Copy link
Copy Markdown
Contributor Author

Note on check-prod-types / check-test-types failures

The two failing type checks are not caused by this PR:

  • extensions/memory-wiki/src/apply.ts(105,5): error TS2322 — in the memory-wiki extension, not touched by this PR
  • src/cron/schedule-identity.test.ts(19,38): error TS2322 — in the cron package, not touched by this PR

Both files have 0 lines changed between this branch and upstream/main. These are pre-existing type errors in the current upstream main that affect all PRs running against this base. They are unrelated to the changes in this PR.

@clawsweeper clawsweeper Bot added rating: 🌊 off-meta tidepool PR readiness rating does not apply to this item. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels May 29, 2026
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🌊 off-meta tidepool PR readiness rating does not apply to this item. labels Jun 15, 2026
@steipete
steipete force-pushed the pr-discord-voice-autoojoin-group branch from d8559e3 to f671e90 Compare July 5, 2026 21:21

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f671e90cf4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +523 to +524
void params.voiceManager
.autoJoin()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Await or cancel lifecycle-started auto-join

Fresh evidence after the prior timer comments: this new lifecycle-owned path still starts autoJoin() fire-and-forget, so if the lifecycle is already aborted or fails immediately after this point while autoJoin() is awaiting fetchChannel/entersState, finally calls destroy() without waiting for that join task. DiscordVoiceManager.joinUnlocked only checks this.destroyed after joinVoiceChannel and the Ready wait, so the bot can still join voice during shutdown and remain there until the voice connect timeout; gate this on the abort signal or make the lifecycle await/cancel the auto-join task before destroying the manager.

Useful? React with 👍 / 👎.

@steipete
steipete force-pushed the pr-discord-voice-autoojoin-group branch from f671e90 to dc20431 Compare July 5, 2026 21:27
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Jul 5, 2026
@clawsweeper

clawsweeper Bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper applied the proposed close for this PR.

@clawsweeper clawsweeper Bot closed this Jul 5, 2026
@steipete steipete reopened this Jul 5, 2026
@steipete
steipete force-pushed the pr-discord-voice-autoojoin-group branch from dc20431 to 28f13b9 Compare July 5, 2026 21:43

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 28f13b990b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@@ -568,6 +573,7 @@ export class DiscordVoiceManager {
const joinedConnection = voiceSdk.joinVoiceChannel({
channelId,
guildId,
group: voiceConnectionGroup,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Filter adapter voice-state events by bot

Adding the per-account group separates the @discordjs/voice registry, but it does not make the gateway adapter account-safe: dispatchVoiceGatewayEvent still forwards every guild VOICE_STATE_UPDATE to adapter?.onVoiceStateUpdate?.(data) based only on guildId. In a guild where two configured bot accounts join voice, account B can receive account A's voice-state event before its own while joining; @discordjs/voice treats that packet as the connection's own state and can combine A's session/user with B's voice-server update, leaving B stuck before Ready. Please pass/filter the bot user id into the voice adapter before calling onVoiceStateUpdate.

Useful? React with 👍 / 👎.

@steipete
steipete force-pushed the pr-discord-voice-autoojoin-group branch from 28f13b9 to 5802e2a Compare July 5, 2026 21:57
@steipete

steipete commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Exact-head land-ready proof for 5802e2a8b244fa6f932507731767983a741202c5:

Known proof gap: this maintainer host has no configured two-account Discord voice guild. Focused tests exercise the exact dependency registry and lifecycle contracts.

@steipete
steipete merged commit 46d8e96 into openclaw:main Jul 5, 2026
95 checks passed
@steipete

steipete commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 6, 2026
…nclaw#87530)

* fix(discord): isolate voice connections by account

Co-authored-by: geekhuashan <[email protected]>

* chore: defer Discord voice accounts changelog

* fix(discord): own auto-join cleanup in lifecycle

* test(discord): bind lifecycle voice mocks

---------

Co-authored-by: Peter Steinberger <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: discord Channel integration: discord merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants