Skip to content

fix: handle closed listener cleanly in accept loop - #2691

Open
yuhuayang-google wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
yuhuayang-google:fix-2372-accept-closed-err
Open

yuhuayang-google wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
yuhuayang-google:fix-2372-accept-closed-err

Conversation

@yuhuayang-google

Copy link
Copy Markdown

Fixes #2372

Problem

When the proxy receives a shutdown signal (e.g. SIGTERM/SIGINT) or socket listeners are closed during shutdown, s.Accept() returns net.ErrClosed (use of closed network connection). Previously, serveSocketMount treated this error as an unhandled failure and returned it. Serve() forwarded it to exitCh, which was then passed to shutdownCh <- err. This caused the proxy to log The proxy has encountered a terminal error: accept tcp ...: use of closed network connection and exit with error code 1 or 2 instead of the appropriate exit code 143 (or 0).

Solution

  1. In serveSocketMount, treat net.ErrClosed (and errors containing "use of closed network connection") as a clean termination and return nil.
  2. In Serve(), wait on all mount goroutines using a sync.WaitGroup and select on ctx.Done() and exitCh, ensuring clean shutdown when mounts close without deadlock.
  3. In cmd/root.go, only forward non-nil errors from p.Serve to shutdownCh, and avoid logging terminal error if err == nil.
  4. Added unit test TestServeExitsCleanlyOnClose in internal/proxy/proxy_test.go to ensure Serve returns nil when the client is closed.

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.

Accept loop has improper error handling, causing wrong exit code.

2 participants