From 8b861716429a6d7699f0c9886c5729a7d82e5b17 Mon Sep 17 00:00:00 2001 From: greatroar <61184462+greatroar@users.noreply.github.com> Date: Wed, 27 Jan 2021 19:27:00 +0100 Subject: [PATCH] lib/stun: Inline util.OnDone, comment on its purpose (#7308) Co-authored-by: greatroar <@> --- lib/stun/stun.go | 7 ++++++- lib/util/utils.go | 8 -------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/stun/stun.go b/lib/stun/stun.go index e5bb54810..2a24f2f85 100644 --- a/lib/stun/stun.go +++ b/lib/stun/stun.go @@ -111,7 +111,12 @@ func (s *Service) Serve(ctx context.Context) error { s.setExternalAddress(nil, "") }() - util.OnDone(ctx, func() { _ = s.stunConn.Close() }) + // Closing s.stunConn unblocks operations that use the connection + // (Discover, Keepalive) and might otherwise block us from returning. + go func() { + <-ctx.Done() + _ = s.stunConn.Close() + }() timer := time.NewTimer(time.Millisecond) diff --git a/lib/util/utils.go b/lib/util/utils.go index d998a0244..427ace285 100644 --- a/lib/util/utils.go +++ b/lib/util/utils.go @@ -250,14 +250,6 @@ func AddressUnspecifiedLess(a, b net.Addr) bool { return aIsUnspecified } -// OnDone calls fn when ctx is cancelled. -func OnDone(ctx context.Context, fn func()) { - go func() { - <-ctx.Done() - fn() - }() -} - func CallWithContext(ctx context.Context, fn func() error) error { var err error done := make(chan struct{})