Skip to content

fix(voice): improve voice connection handling and state management#537

Open
topi314 wants to merge 2 commits intomasterfrom
fix/voice-reconnect
Open

fix(voice): improve voice connection handling and state management#537
topi314 wants to merge 2 commits intomasterfrom
fix/voice-reconnect

Conversation

@topi314
Copy link
Copy Markdown
Member

@topi314 topi314 commented Apr 8, 2026

might fix: #534

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 AutoReconnect toggle 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

  • handleGatewayClose calls c.Open(...) for closeCode.NewConnection, but then unconditionally calls c.Close(ctx) right after. If Open succeeds 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 so Close only 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.

Comment thread voice/conn.go Outdated
Comment thread voice/conn.go
Comment thread voice/conn.go
Comment thread voice/gateway.go
Comment thread voice/gateway.go
Comment thread voice/gateway_config.go
Comment thread voice/gateway.go
Comment thread voice/gateway_opcodes.go Outdated
Comment thread voice/gateway_config.go
@topi314
Copy link
Copy Markdown
Member Author

topi314 commented Apr 8, 2026

ToDo:

  • API to see close gateway codes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

No audio received after voice gateway connection interrupted

3 participants