mirror of
https://github.com/netbirdio/netbird.git
synced 2026-05-13 19:52:13 -04:00
netrelay: wait for endpoint close before Relay returns
The closer goroutine ran asynchronously on ctx cancellation, so the "fully closed when Relay returns" guarantee was racy: callers could see the function return before a and b were actually Close()d. Wait on a done channel in the defer so the guarantee holds.
This commit is contained in:
@@ -74,12 +74,17 @@ type Options struct {
|
||||
// returned because a relay always terminates on some kind of EOF/cancel.
|
||||
func Relay(ctx context.Context, a, b io.ReadWriteCloser, opts Options) (aToB, bToA int64) {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
closeDone := make(chan struct{})
|
||||
defer func() {
|
||||
cancel()
|
||||
<-closeDone
|
||||
}()
|
||||
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
_ = a.Close()
|
||||
_ = b.Close()
|
||||
close(closeDone)
|
||||
}()
|
||||
|
||||
// Both sides must support CloseWrite to propagate half-close. If either
|
||||
|
||||
Reference in New Issue
Block a user