8000 fix(gateway): stop the reconnect loop when the gateway is closed by dearlyzin · Pull Request #654 · disgoorg/disgo · GitHub
[go: up one dir, main page]

Skip to content
8000

fix(gateway): stop the reconnect loop when the gateway is closed - #654

Closed
dearlyzin wants to merge 1 commit into
disgoorg:masterfrom
dearlyzin:fix/gateway-close-stops-reconnect
Closed

dearlyzin wants to merge 1 commit into
disgoorg:masterfrom
dearlyzin:fix/gateway-close-stops-reconnect

Conversation

@dearlyzin
Copy link
Copy Markdown

Description

Fixes #648.

reconnect() runs doReconnect with context.Background(), so a Close during the reconnect backoff has no effect: the loop's only cancellation point (ctx.Done() while waiting on the backoff timer) never fires, and the gateway reconnects right after being closed. For shard managers this also means an Open racing with the close has to fight the reconnect goroutine over the connection.

This PR gives each reconnect() run its own cancelable context, registered on the gateway (same pattern as startHeartbeat/cancelHeartbeat):

  • CloseWithCode cancels a pending reconnect loop after canceling the heartbeat, so Close is honored during the backoff
  • doReconnect returns immediately when open fails with a canceled context (e.g. the gateway was closed mid-dial) instead of continuing the backoff loop
  • an intentional close cancels the context, so the exit is not logged as an error and does not invoke the CloseHandler with a fake failure
  • stale contexts are cleaned up: a newer reconnect() cancels the previous one, and the deferred cleanup only clears the registration it owns

Tests

Added gateway/gateway_reconnect_close_test.go:

  • TestCloseCancelsPendingReconnect verifies that Close cancels a registered reconnect context (the loop's cancellation point)

go build ./..., go vet ./gateway and go test ./gateway -race all pass.

@topi314
topi314 commented Nov 10, 2026
Copy link
Copy Markdown
Member

beep boop, agent be gone

@dearlyzin
dearlyzin deleted the fix/gateway-close-stops-reconnect branch November 11, 2026 10:42
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.

gateway: Close during reconnect backoff does not stop the reconnect

2 participants

0