Fix memory leak in http-relay-server.#760
Merged
Merged
Conversation
When a request is stopped (e.g., if the user-client times out or closes the connection), StopRelayRequest was simply deleting the request from the internal map. However, it was not closing the associated responseStream and requestStream channels. This caused the goroutines ranging over these channels (started in Server.responseFilter and Server.bidirectionalStream) to block indefinitely, leading to a steady increase in goroutines and memory usage over time. This change refactores the cleanup logic into a common removeRequest helper that explicitly closes all channels and signals the goroutines to exit before removing the request from the map. It ensures that resources are always reclaimed regardless of whether the request timed out or was explicitly stopped. In addition this adds a new test to cover this and pprof support (so that we can easily identify similar cases in the future).
Contributor
Author
|
Tested pprof via: |
drigz
approved these changes
Jun 29, 2026
Contributor
|
Nice find, thank you! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
When a request is stopped (e.g., if the user-client times out or closes the connection), StopRelayRequest was simply deleting the request from the internal map. However, it was not closing the associated responseStream and requestStream channels. This caused the goroutines ranging over these channels (started in Server.responseFilter and Server.bidirectionalStream) to block indefinitely, leading to a steady increase in goroutines and memory usage over time.
This change refactores the cleanup logic into a common removeRequest helper that explicitly closes all channels and signals the goroutines to exit before removing the request from the map. It ensures that resources are always reclaimed regardless of whether the request timed out or was explicitly stopped.
In addition this adds a new test to cover this and pprof support (so that we can easily identify similar cases in the future).