Skip to content

fix: fix goroutine leak in log streaming over websocket - #15709

Merged
spikecurtis merged 1 commit into
mainfrom
spike/14881-read-from-json-websockets
Dec 3, 2024
Merged

spikecurtis merged 1 commit into
mainfrom
spike/14881-read-from-json-websockets

Conversation

@spikecurtis

@spikecurtis spikecurtis commented Dec 2, 2024 •

Copy link
Copy Markdown
Collaborator

fixes #14881

Our handlers for streaming logs don't read from the websocket. We don't allow the client to send us any data, but the websocket library we use requires reading from the websocket to properly handle pings and closing. Not doing so can can cause the websocket to hang on write, leaking go routines which were noticed in #14881.

This fixes the issue, and in process refactors our log streaming to a encoder/decoder package which provides generic types for sending JSON over websocket.

I'd also like for us to upgrade to the latest https://github.com/coder/websocket but we should also upgrade our tailscale fork before doing so to avoid including two copies of the websocket library.

Copy link
Copy Markdown
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@spikecurtis
spikecurtis requested review from Emyrk and mafredri December 2, 2024 06:54
@spikecurtis
spikecurtis marked this pull request as ready for review December 2, 2024 06:55
@spikecurtis
spikecurtis force-pushed the spike/14881-read-from-json-websockets branch from e379c84 to 2ae0a74 Compare December 2, 2024 08:54

@mafredri mafredri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small suggestions but otherwise LGTM 👍🏻

Comment thread coderd/workspaceagents.go Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we use going away instead of normal closure? It's kind of an error state and a divergence from before (i.e. status from calling wsNetConn.Close()).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose GoingAway because one likely reason for the server closing the connection is that it's shutting down. We could also be closing because there is a new build and this agent is no longer current.

If we actually used status codes for anything we'd want to send different codes in these cases, but we don't. I can change it to match the old wsNetConn for consistency.

Comment thread coderd/workspaceagents.go Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

Comment thread codersdk/wsjson/decoder.go Resolved
Comment thread codersdk/wsjson/decoder.go Resolved
Comment thread codersdk/wsjson/decoder.go Resolved
Comment thread codersdk/wsjson/decoder.go Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as previously, why not use normal closure? Also, why not take status like encoder for consistency?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've switched to StatusNormalClosure.

Here I don't want to take the websocket status like Encoder because it's useful for the Decoder to implement io.Closer to more easily fit into existing code.

@spikecurtis
spikecurtis force-pushed the spike/14881-read-from-json-websockets branch from 2ae0a74 to 76911bb Compare December 3, 2024 06:04
@spikecurtis
spikecurtis merged commit 148a5a3 into main Dec 3, 2024

Copy link
Copy Markdown
Collaborator Author

Merge activity

  • Dec 3, 1:12 AM EST: A user merged this pull request with Graphite.

@spikecurtis
spikecurtis deleted the spike/14881-read-from-json-websockets branch December 3, 2024 06:12
stirby pushed a commit that referenced this pull request Dec 3, 2024
fixes #14881

Our handlers for streaming logs don't read from the websocket. We don't allow the client to send us any data, but the websocket library we use requires reading from the websocket to properly handle pings and closing. Not doing so can [can cause the websocket to hang on write](coder/websocket#405), leaking go routines which were noticed in #14881.

This fixes the issue, and in process refactors our log streaming to a encoder/decoder package which provides generic types for sending JSON over websocket.

I'd also like for us to upgrade to the latest https://github.com/coder/websocket but we should also upgrade our tailscale fork before doing so to avoid including two copies of the websocket library.

(cherry picked from commit 148a5a3)
stirby pushed a commit that referenced this pull request Dec 11, 2024
fixes #14881

Our handlers for streaming logs don't read from the websocket. We don't allow the client to send us any data, but the websocket library we use requires reading from the websocket to properly handle pings and closing. Not doing so can [can cause the websocket to hang on write](coder/websocket#405), leaking go routines which were noticed in #14881.

This fixes the issue, and in process refactors our log streaming to a encoder/decoder package which provides generic types for sending JSON over websocket.

I'd also like for us to upgrade to the latest https://github.com/coder/websocket but we should also upgrade our tailscale fork before doing so to avoid including two copies of the websocket library.

(cherry picked from commit 148a5a3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Coder pods running out of memory

2 participants