-
Notifications
You must be signed in to change notification settings - Fork 102
Fix connection reset logic to ensure the old subscribe goroutine is cancelled before a new connection is made #1700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dhurley
wants to merge
1
commit into
main
Choose a base branch
from
fix/connection-reset-operations-order
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,7 @@ type ( | |
| commandServerType model.ServerType | ||
| subscribeMutex sync.Mutex | ||
| agentConfigMutex sync.RWMutex | ||
| subscribeWg sync.WaitGroup | ||
| } | ||
| ) | ||
|
|
||
|
|
@@ -195,7 +196,11 @@ func (cp *CommandPlugin) createConnection(ctx context.Context, resource *mpi.Res | |
| subscribeCtx, cp.subscribeCancel = context.WithCancel(ctx) | ||
| cp.subscribeMutex.Unlock() | ||
|
|
||
| go cp.commandService.Subscribe(subscribeCtx) | ||
| cp.subscribeWg.Add(1) | ||
| go func() { | ||
| defer cp.subscribeWg.Done() | ||
| cp.commandService.Subscribe(subscribeCtx) | ||
| }() | ||
|
|
||
| cp.messagePipe.Process(ctx, &bus.Message{ | ||
| Topic: bus.ConnectionCreatedTopic, | ||
|
|
@@ -294,18 +299,14 @@ func (cp *CommandPlugin) processConnectionReset(ctx context.Context, msg *bus.Me | |
| cp.subscribeMutex.Lock() | ||
| defer cp.subscribeMutex.Unlock() | ||
|
|
||
| // Update the command service with the new client first | ||
| err := cp.commandService.UpdateClient(ctxWithMetadata, newConnection.CommandServiceClient()) | ||
| if err != nil { | ||
| slog.ErrorContext(ctx, "Failed to reset connection", "error", err) | ||
| return | ||
| } | ||
|
|
||
| // Once the command service is updated, we close the old connection | ||
| slog.InfoContext(ctx, "Canceling old subscribe stream after connection reset") | ||
| // Cancel the old subscribe stream and close the old connection first, so the server removes | ||
| // the connection from its tracker before we call CreateConnection | ||
| // with the same UUID in UpdateClient. Without this ordering, the server would track the UUID | ||
| // from CreateConnection and then immediately remove it when the old stream exits. | ||
| slog.InfoContext(ctx, "Canceling old subscribe stream before connection reset") | ||
| if cp.subscribeCancel != nil { | ||
| cp.subscribeCancel() | ||
| slog.InfoContext(ctxWithMetadata, "Successfully canceled old subscribe stream after connection reset") | ||
| slog.InfoContext(ctxWithMetadata, "Successfully canceled old subscribe stream before connection reset") | ||
| } | ||
|
|
||
| connectionErr := cp.conn.Close(ctx) | ||
|
|
@@ -314,9 +315,24 @@ func (cp *CommandPlugin) processConnectionReset(ctx context.Context, msg *bus.Me | |
| } | ||
|
|
||
| cp.conn = newConnection | ||
|
|
||
| // Wait for the old Subscribe goroutine to fully exit before creating a new one with the new connection. . | ||
| cp.subscribeWg.Wait() | ||
|
|
||
| // Update the command service with the new client after the old stream has been torn down | ||
| err := cp.commandService.UpdateClient(ctxWithMetadata, newConnection.CommandServiceClient()) | ||
| if err != nil { | ||
| slog.ErrorContext(ctx, "Failed to reset connection", "error", err) | ||
| return | ||
| } | ||
|
|
||
| slog.InfoContext(ctxWithMetadata, "Starting new subscribe stream after connection reset") | ||
| subscribeCtx, cp.subscribeCancel = context.WithCancel(ctxWithMetadata) | ||
| go cp.commandService.Subscribe(subscribeCtx) | ||
| cp.subscribeWg.Add(1) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Are we missing a cp.subscribeWg.Wait() statement? |
||
| go func() { | ||
| defer cp.subscribeWg.Done() | ||
| cp.commandService.Subscribe(subscribeCtx) | ||
| }() | ||
|
|
||
| slog.InfoContext(ctx, "Command plugin connection reset finished successfully") | ||
| } | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: Trailing dot.