[client] Update RaceDial to accept context for improved cancellation#5849
[client] Update RaceDial to accept context for improved cancellation#5849
RaceDial to accept context for improved cancellation#5849Conversation
📝 WalkthroughWalkthroughThe relay client's race dialer has been updated to accept and propagate a context parameter through its Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
shared/relay/client/dialer/race_dialer_test.go (1)
76-252: Add a regression test for parent-context cancellation propagation.Current tests update the signature but don’t verify the new cancellation behavior this PR is meant to deliver.
Proposed test addition
+func TestRaceDialParentContextCancellation(t *testing.T) { + logger := logrus.NewEntry(logrus.New()) + serverURL := "test.server.com" + + mockDialer := &MockDialer{ + dialFunc: func(ctx context.Context, address string) (net.Conn, error) { + <-ctx.Done() + return nil, ctx.Err() + }, + protocolStr: "proto1", + } + + rd := NewRaceDial(logger, 30*time.Second, serverURL, mockDialer) + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + start := time.Now() + conn, err := rd.Dial(ctx) + if err == nil { + t.Fatalf("expected cancellation error, got nil") + } + if conn != nil { + t.Fatalf("expected nil conn, got %v", conn) + } + if time.Since(start) > 500*time.Millisecond { + t.Fatalf("dial did not abort promptly after parent context cancellation") + } +}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@shared/relay/client/dialer/race_dialer_test.go` around lines 76 - 252, Add a new unit test (e.g., TestRaceDialParentContextCancellation) that verifies parent-context cancellation is propagated to dialers: create a parent context with cancel, make one or more MockDialer implementations whose dialFunc blocks on <-ctx.Done() and sends ctx.Err() to a channel, call rd.Dial in a goroutine using the parent context, call cancel() before any dialer returns, then assert Dial returns an error (and nil conn) and that each mock dialer received context.Canceled via the channel; reference NewRaceDial, rd.Dial, and the MockDialer dialFunc/mock2err-style channels used in other tests to locate where to add this test.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@shared/relay/client/dialer/race_dialer_test.go`:
- Around line 76-252: Add a new unit test (e.g.,
TestRaceDialParentContextCancellation) that verifies parent-context cancellation
is propagated to dialers: create a parent context with cancel, make one or more
MockDialer implementations whose dialFunc blocks on <-ctx.Done() and sends
ctx.Err() to a channel, call rd.Dial in a goroutine using the parent context,
call cancel() before any dialer returns, then assert Dial returns an error (and
nil conn) and that each mock dialer received context.Canceled via the channel;
reference NewRaceDial, rd.Dial, and the MockDialer dialFunc/mock2err-style
channels used in other tests to locate where to add this test.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 56d98e9a-e717-49e7-a9c7-cb98eed40458
📒 Files selected for processing (3)
shared/relay/client/client.goshared/relay/client/dialer/race_dialer.goshared/relay/client/dialer/race_dialer_test.go



Describe your changes
The relay RaceDial.Dial() created its abort context from context.Background(), making it immune to parent context cancellation.
During shutdown, this caused the client to block for ~27 seconds, waiting for the WebSocket dial timeout to expire instead of exiting immediately.
Fix:
Propagate the parent context into RaceDial.Dial(ctx) so that when the engine cancels its context on shutdown, all in-flight dial attempts are aborted immediately.
Issue ticket number and link
Stack
Checklist
Documentation
Select exactly one:
Docs PR URL (required if "docs added" is checked)
Paste the PR link from https://github.com/netbirdio/docs here:
https://github.com/netbirdio/docs/pull/__
Summary by CodeRabbit