Skip to content

fix(relay): prevent panic on double Stop() of UDPProxyServer (#51) - #105

Open
Adityakk9031 wants to merge 1 commit into
fosrl:mainfrom
Adityakk9031:fix/issue-51
Open

fix(relay): prevent panic on double Stop() of UDPProxyServer (#51)#105
Adityakk9031 wants to merge 1 commit into
fosrl:mainfrom
Adityakk9031:fix/issue-51

Conversation

@Adityakk9031

Copy link
Copy Markdown

Summary

Fixes #51

Gerbil was panicking with panic: close of closed channel on every clean
shutdown (and sometimes randomly during operation). The process would then
restart via Docker's restart policy, re-register with Pangolin, and resume —
but left some resources in a broken state requiring manual intervention.


Root Cause

UDPProxyServer.Stop() was being called twice on every shutdown:

Call site File Trigger
defer proxyRelay.Stop() main.go:533 Always, when main() returns
proxyRelay.Stop() main.go:606 When groupCtx is cancelled (shutdown signal)

Both fire on every clean exit. The second call panicked because
close(s.packetChan) was executed twice.

The existing guard was ineffective:

// OLD — broken guard
select {
case <-s.ctx.Done():  // ctx is ALREADY done on the 2nd call too
default:
}
close(s.packetChan)   // executes BOTH times → panic

The UDPProxyServer.Stop() method was being called twice on every clean
shutdown — once via defer proxyRelay.Stop() in main() and once from
the shutdown errgroup goroutine — causing a 'close of closed channel'
panic.

The old guard using a bare select on s.ctx.Done() was ineffective: by
the second call the context was already cancelled, so both code paths
fell through unconditionally to close(s.packetChan), panicking.

Fix: wrap the entire Stop() body in a sync.Once so that all cleanup
(cancel, conn close, packetChan close) runs at most once regardless of
how many times or from how many goroutines Stop() is called.

Fixes fosrl#51
@Adityakk9031

Copy link
Copy Markdown
Author

@oschwartz10612 and @miloschwartz have a look

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.

Gerbil having several identical panics then proceeds to start

1 participant