Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix goroutine leaks in CONNECT connection handling#66

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
blink-so wants to merge2 commits intomain
base:main
Choose a base branch
Loading
fromblink/fix-goroutine-leaks-connect

Conversation

blink-so[bot]
Copy link

@blink-soblink-sobot commentedOct 7, 2025
edited
Loading

Problem

The proxy was building up CPU and memory usage over time due to goroutine leaks in CONNECT connection handling. Three specific leaks were identified:

  1. streamRequestToTarget (line 684): Spawned goroutine for request body copying that never synchronized or cleaned up properly
  2. handleConnectStreaming (line 733): Spawned goroutine for bidirectional tunnel copy that could hang indefinitely
  3. Both functions had no synchronization between their bidirectional copy goroutines

Solution

  • Use errgroup for proper goroutine lifecycle management and synchronization
  • Close write side of connections after copy completes to properly signal EOF
  • Add context monitoring to force cleanup when one goroutine completes
  • Improved error handling to ignore expected EOF/ErrClosed errors (reduced log noise)

The errgroup ensures that:

  • All spawned goroutines are tracked and waited on before the function returns
  • When one direction closes (e.g., client disconnects), the context is canceled
  • The cleanup goroutine forces both connections closed, unblocking any hanging io.Copy
  • No goroutines are left running after the connection handling completes

Testing

✅ All existing tests pass
✅ Code compiles without errors

Changes

  • Addedgolang.org/x/sync/errgroup dependency
  • ModifiedstreamRequestToTarget to use errgroup with 3 goroutines (request copy, response copy, cleanup monitor)
  • ModifiedhandleConnectStreaming to use errgroup with 3 goroutines (client→target, target→client, cleanup monitor)
  • Both functions now properly clean up all goroutines before returning
  • Connections can remain open indefinitely for long-lived use cases

blink-sobotand others added2 commitsOctober 7, 2025 19:39
This fixes three goroutine leaks that were causing CPU and memory usage tobuild up over time:1. streamRequestToTarget: Orphaned bidirectional copy goroutines that never   terminated properly when connections closed early2. handleConnectStreaming: Similar issue with bidirectional tunnel goroutines   that could hang indefinitelyChanges made:- Use errgroup to properly manage and synchronize goroutine lifecycle- Add connection deadlines (5 min) to prevent indefinite blocking on io.Copy- Close write side of connections after copy completes to signal EOF- Add context cancellation monitoring to force cleanup if needed- Improved error handling to ignore expected EOF/ErrClosed errorsCo-authored-by: f0ssel <19379394+f0ssel@users.noreply.github.com>
Connections need to stay open indefinitely for valid use cases.The errgroup synchronization alone is sufficient to prevent goroutine leaks.Co-authored-by: f0ssel <19379394+f0ssel@users.noreply.github.com>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

0 participants

[8]ページ先頭

©2009-2025 Movatter.jp