-
Notifications
You must be signed in to change notification settings - Fork 96
[MeshSync] Fix channelPool data race: isolate exec/log-stream sessions behind a mutex #587
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
11d0c96
fix(meshsync): move exec/log-stream sessions out of the shared channe…
leecalcote a11f99d
fix(meshsync): harden log-stream lifecycle (deadlock, goroutine leak,…
leecalcote 2a53afb
fix(meshsync): buffer session channels and guard nil sessions map
leecalcote ec117fa
fix(meshsync): terminate log-stream sessions on global stop
leecalcote 4c6c25d
refactor(meshsync): extract log-stream stop waiter to reduce complexity
leecalcote 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package meshsync | ||
|
|
||
| import ( | ||
| "github.com/meshery/meshsync/internal/channels" | ||
| ) | ||
|
|
||
| // Interactive exec and log-stream sessions are keyed by a per-request id. | ||
| // They were previously stored in the shared channelPool alongside the fixed | ||
| // system channels (Stop/OS/ReSync), which meant session goroutines mutated the | ||
| // same map that other goroutines read (system-channel selects, getActiveChannels), | ||
| // a data race that can panic the process. They now live in their own | ||
| // mutex-guarded map so channelPool stays read-only after initialization. | ||
| // | ||
| // Every helper returns the channel (if any) and releases the lock before the | ||
| // caller performs any channel send/receive, so the sessions mutex is never held | ||
| // across a blocking channel operation. | ||
|
|
||
| // addSession registers a new session channel for id and returns it with | ||
| // created=true. If a session already exists for id, the existing channel is | ||
| // returned with created=false so the caller does not start a duplicate. | ||
| func (h *Handler) addSession(id string) (ch channels.StructChannel, created bool) { | ||
| h.sessionsMu.Lock() | ||
| defer h.sessionsMu.Unlock() | ||
| // Defensive: a Handler constructed outside New has a nil map; writing to it | ||
| // would panic. | ||
| if h.sessions == nil { | ||
| h.sessions = make(map[string]channels.StructChannel) | ||
| } | ||
| if existing, ok := h.sessions[id]; ok { | ||
| return existing, false | ||
| } | ||
| // 1-buffered: exec/log-stream stop signals are sent non-blocking, so a stop | ||
| // that arrives before the session goroutine begins receiving must still be | ||
| // recorded rather than dropped (which would leave the session running). | ||
| ch = make(channels.StructChannel, 1) | ||
| h.sessions[id] = ch | ||
| return ch, true | ||
| } | ||
|
|
||
| // getSession returns the session channel for id, if present. | ||
| func (h *Handler) getSession(id string) (channels.StructChannel, bool) { | ||
| h.sessionsMu.Lock() | ||
| defer h.sessionsMu.Unlock() | ||
| ch, ok := h.sessions[id] | ||
| return ch, ok | ||
| } | ||
|
|
||
| // deleteSession removes the session for id. It is safe to call for an id that | ||
| // is not present. | ||
| func (h *Handler) deleteSession(id string) { | ||
| h.sessionsMu.Lock() | ||
| defer h.sessionsMu.Unlock() | ||
| delete(h.sessions, id) | ||
| } | ||
|
|
||
| // activeSessionIDs returns the ids of the currently active sessions. | ||
| func (h *Handler) activeSessionIDs() []string { | ||
| h.sessionsMu.Lock() | ||
| defer h.sessionsMu.Unlock() | ||
| ids := make([]string, 0, len(h.sessions)) | ||
| for id := range h.sessions { | ||
| ids = append(ids, id) | ||
| } | ||
| return ids | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.