[agent] Fix a goroutine leak that happens when a websocket connection is closed. #143
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.
Fix a bug where the agent would leak goroutines when a websocket connection is closed.
This was caused by the logic that coordinates reading and writing to the websocket connection to be too complicated, and using a channel for communicating when to exit instead of just cancelling the context.
The use of that (unbuffered) channel then caused a leak when two separate goroutines both tried to send a message to the channel, but only one read was ever performed, and as a result the second goroutine would always hang indefinitely.
This leak and the corresponding fix were confirmed using manual testing.
This change also fixes a bad coding pattern where we stored the context in a struct. Instead, we just store the methods from that context that are actually needed.