-
Notifications
You must be signed in to change notification settings - Fork 896
fix(coderd): fix memory leak in watchWorkspaceAgentMetadata
#10685
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
e6ad02f
fix(coderd): fix memory leak in `watchWorkspaceAgentMetadata`
mafredri 65a9899
fixes
mafredri ab3cea2
add test that can reproduce memory leak
mafredri 1da56e6
remove unused testutil function
mafredri 5bb2a4b
break out slice set merger into appendUnique
mafredri 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 |
---|---|---|
|
@@ -1767,10 +1767,10 @@ func (api *API) workspaceAgentUpdateMetadata(ctx context.Context, workspaceAgent | |
|
||
datum := database.UpdateWorkspaceAgentMetadataParams{ | ||
WorkspaceAgentID: workspaceAgent.ID, | ||
Key: []string{}, | ||
Value: []string{}, | ||
Error: []string{}, | ||
CollectedAt: []time.Time{}, | ||
Key: make([]string, 0, len(req.Metadata)), | ||
Value: make([]string, 0, len(req.Metadata)), | ||
Error: make([]string, 0, len(req.Metadata)), | ||
CollectedAt: make([]time.Time, 0, len(req.Metadata)), | ||
} | ||
|
||
for _, md := range req.Metadata { | ||
|
@@ -1835,41 +1835,36 @@ func (api *API) workspaceAgentUpdateMetadata(ctx context.Context, workspaceAgent | |
// @Router /workspaceagents/{workspaceagent}/watch-metadata [get] | ||
// @x-apidocgen {"skip": true} | ||
func (api *API) watchWorkspaceAgentMetadata(rw http.ResponseWriter, r *http.Request) { | ||
var ( | ||
ctx = r.Context() | ||
workspaceAgent = httpmw.WorkspaceAgentParam(r) | ||
log = api.Logger.Named("workspace_metadata_watcher").With( | ||
slog.F("workspace_agent_id", workspaceAgent.ID), | ||
) | ||
// Allow us to interrupt watch via cancel. | ||
ctx, cancel := context.WithCancel(r.Context()) | ||
defer cancel() | ||
r = r.WithContext(ctx) // Rewire context for SSE cancellation. | ||
|
||
workspaceAgent := httpmw.WorkspaceAgentParam(r) | ||
log := api.Logger.Named("workspace_metadata_watcher").With( | ||
slog.F("workspace_agent_id", workspaceAgent.ID), | ||
) | ||
|
||
// Send metadata on updates, we must ensure subscription before sending | ||
// initial metadata to guarantee that events in-between are not missed. | ||
update := make(chan workspaceAgentMetadataChannelPayload, 1) | ||
cancelSub, err := api.Pubsub.Subscribe(watchWorkspaceAgentMetadataChannel(workspaceAgent.ID), func(_ context.Context, byt []byte) { | ||
if ctx.Err() != nil { | ||
return | ||
} | ||
|
||
var payload workspaceAgentMetadataChannelPayload | ||
err := json.Unmarshal(byt, &payload) | ||
if err != nil { | ||
api.Logger.Error(ctx, "failed to unmarshal pubsub message", slog.Error(err)) | ||
log.Error(ctx, "failed to unmarshal pubsub message", slog.Error(err)) | ||
return | ||
} | ||
|
||
log.Debug(ctx, "received metadata update", "payload", payload) | ||
|
||
select { | ||
case prev := <-update: | ||
// This update wasn't consumed yet, merge the keys. | ||
newKeysSet := make(map[string]struct{}) | ||
for _, key := range payload.Keys { | ||
newKeysSet[key] = struct{}{} | ||
} | ||
keys := prev.Keys | ||
for _, key := range prev.Keys { | ||
if _, ok := newKeysSet[key]; !ok { | ||
keys = append(keys, key) | ||
} | ||
} | ||
payload.Keys = keys | ||
payload.Keys = appendUnique(prev.Keys, payload.Keys) | ||
default: | ||
} | ||
// This can never block since we pop and merge beforehand. | ||
|
@@ -1881,22 +1876,9 @@ func (api *API) watchWorkspaceAgentMetadata(rw http.ResponseWriter, r *http.Requ | |
} | ||
defer cancelSub() | ||
|
||
sseSendEvent, sseSenderClosed, err := httpapi.ServerSentEventSender(rw, r) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Internal error setting up server-sent events.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
// Prevent handler from returning until the sender is closed. | ||
defer func() { | ||
<-sseSenderClosed | ||
}() | ||
|
||
// We always use the original Request context because it contains | ||
// the RBAC actor. | ||
md, err := api.Database.GetWorkspaceAgentMetadata(ctx, database.GetWorkspaceAgentMetadataParams{ | ||
initialMD, err := api.Database.GetWorkspaceAgentMetadata(ctx, database.GetWorkspaceAgentMetadataParams{ | ||
WorkspaceAgentID: workspaceAgent.ID, | ||
Keys: nil, | ||
}) | ||
|
@@ -1908,15 +1890,45 @@ func (api *API) watchWorkspaceAgentMetadata(rw http.ResponseWriter, r *http.Requ | |
return | ||
} | ||
|
||
metadataMap := make(map[string]database.WorkspaceAgentMetadatum) | ||
for _, datum := range md { | ||
log.Debug(ctx, "got initial metadata", "num", len(initialMD)) | ||
|
||
metadataMap := make(map[string]database.WorkspaceAgentMetadatum, len(initialMD)) | ||
for _, datum := range initialMD { | ||
metadataMap[datum.Key] = datum | ||
} | ||
//nolint:ineffassign // Release memory. | ||
initialMD = nil | ||
Comment on lines
+1899
to
+1900
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. My understanding here is that we only need the initial slice of metadata from the DB once in order to massage it into a map. |
||
|
||
sseSendEvent, sseSenderClosed, err := httpapi.ServerSentEventSender(rw, r) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Internal error setting up server-sent events.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
// Prevent handler from returning until the sender is closed. | ||
defer func() { | ||
cancel() | ||
<-sseSenderClosed | ||
}() | ||
// Synchronize cancellation from SSE -> context, this lets us simplify the | ||
// cancellation logic. | ||
go func() { | ||
select { | ||
case <-ctx.Done(): | ||
case <-sseSenderClosed: | ||
cancel() | ||
} | ||
}() | ||
|
||
var lastSend time.Time | ||
sendMetadata := func() { | ||
lastSend = time.Now() | ||
values := maps.Values(metadataMap) | ||
|
||
log.Debug(ctx, "sending metadata", "num", len(values)) | ||
|
||
_ = sseSendEvent(ctx, codersdk.ServerSentEvent{ | ||
Type: codersdk.ServerSentEventTypeData, | ||
Data: convertWorkspaceAgentMetadata(values), | ||
|
@@ -1935,10 +1947,11 @@ func (api *API) watchWorkspaceAgentMetadata(rw http.ResponseWriter, r *http.Requ | |
fetchedMetadata := make(chan []database.WorkspaceAgentMetadatum) | ||
go func() { | ||
defer close(fetchedMetadata) | ||
defer cancel() | ||
|
||
for { | ||
select { | ||
case <-sseSenderClosed: | ||
case <-ctx.Done(): | ||
return | ||
case payload := <-update: | ||
md, err := api.Database.GetWorkspaceAgentMetadata(ctx, database.GetWorkspaceAgentMetadataParams{ | ||
|
@@ -1948,24 +1961,35 @@ func (api *API) watchWorkspaceAgentMetadata(rw http.ResponseWriter, r *http.Requ | |
if err != nil { | ||
if !errors.Is(err, context.Canceled) { | ||
log.Error(ctx, "failed to get metadata", slog.Error(err)) | ||
_ = sseSendEvent(ctx, codersdk.ServerSentEvent{ | ||
Type: codersdk.ServerSentEventTypeError, | ||
Data: codersdk.Response{ | ||
Message: "Failed to get metadata.", | ||
Detail: err.Error(), | ||
}, | ||
}) | ||
} | ||
return | ||
} | ||
select { | ||
case <-sseSenderClosed: | ||
case <-ctx.Done(): | ||
return | ||
// We want to block here to avoid constantly pinging the | ||
// database when the metadata isn't being processed. | ||
case fetchedMetadata <- md: | ||
log.Debug(ctx, "fetched metadata update for keys", "keys", payload.Keys, "num", len(md)) | ||
} | ||
} | ||
} | ||
}() | ||
defer func() { | ||
<-fetchedMetadata | ||
}() | ||
|
||
pendingChanges := true | ||
for { | ||
select { | ||
case <-sseSenderClosed: | ||
case <-ctx.Done(): | ||
return | ||
case md, ok := <-fetchedMetadata: | ||
if !ok { | ||
|
@@ -1989,9 +2013,24 @@ func (api *API) watchWorkspaceAgentMetadata(rw http.ResponseWriter, r *http.Requ | |
} | ||
} | ||
|
||
// appendUnique is like append and adds elements from src to dst, | ||
// skipping any elements that already exist in dst. | ||
func appendUnique[T comparable](dst, src []T) []T { | ||
exists := make(map[T]struct{}, len(dst)) | ||
for _, key := range dst { | ||
exists[key] = struct{}{} | ||
} | ||
for _, key := range src { | ||
if _, ok := exists[key]; !ok { | ||
dst = append(dst, key) | ||
} | ||
} | ||
return dst | ||
} | ||
|
||
func convertWorkspaceAgentMetadata(db []database.WorkspaceAgentMetadatum) []codersdk.WorkspaceAgentMetadata { | ||
// An empty array is easier for clients to handle than a null. | ||
result := []codersdk.WorkspaceAgentMetadata{} | ||
result := make([]codersdk.WorkspaceAgentMetadata, 0, len(db)) | ||
for _, datum := range db { | ||
result = append(result, codersdk.WorkspaceAgentMetadata{ | ||
Result: codersdk.WorkspaceAgentMetadataResult{ | ||
|
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.