fix(voice): improve voice connection handling and state management#537
Open
fix(voice): improve voice connection handling and state management#537
Conversation
Closed
There was a problem hiding this comment.
Pull request overview
This PR targets issue #534 by adjusting the voice connection lifecycle so the library can better recover from voice gateway interruptions, including differentiating between “resume/reconnect” vs “full new connection” scenarios and tracking additional connection state.
Changes:
- Extend voice connection state to include self mute/deaf and introduce “new connection required” handling for certain voice gateway close codes.
- Rework voice connection open/close signaling (channel-based → cancel func–based) and adjust gateway close handler semantics.
- Simplify voice gateway config by removing the
AutoReconnecttoggle and related config option.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 8 comments.
| File | Description |
|---|---|
voice/gateway.go |
Updates close handler signature and modifies reconnect/close handling paths in the voice gateway listener. |
voice/gateway_opcodes.go |
Adds NewConnection to close-code metadata and updates reconnect semantics per close code. |
voice/gateway_config.go |
Removes exported auto-reconnect configuration from the voice gateway config. |
voice/conn.go |
Updates voice conn state management (self mute/deaf), rewires open/close coordination, and adds logic to react to gateway close errors. |
Comments suppressed due to low confidence (1)
voice/conn.go:299
handleGatewayClosecallsc.Open(...)forcloseCode.NewConnection, but then unconditionally callsc.Close(ctx)right after. IfOpensucceeds this will immediately disconnect/tear down the voice connection again, defeating the purpose of the full reconnect. Consider returning early after a successful reopen (or reorganizing soCloseonly happens when you actually intend to give up).
func (c *connImpl) handleGatewayClose(_ Gateway, err error) {
var closeError *websocket.CloseError
if errors.As(err, &closeError) {
closeCode := GatewayCloseEventCodeByCode(closeError.Code)
if closeCode.NewConnection {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err = c.Open(ctx, *c.state.ChannelID, false, false); err != nil {
c.config.Logger.Error("voice: failed to reopen voice conn after full reconnect close code", slog.Any("err", err))
return
}
}
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
c.Close(ctx)
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Qreepex
suggested changes
Apr 8, 2026
Member
Author
|
ToDo:
|
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.
might fix: #534