From e4bae52358d146ccb548aab54059b86dce77e597 Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Thu, 12 Dec 2019 21:21:37 +0300 Subject: [PATCH] Fix RouteGroup closing logic --- pkg/router/route_group.go | 13 ++++++++----- pkg/router/route_group_test.go | 4 ++++ pkg/router/routerclient/wrappers.go | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/pkg/router/route_group.go b/pkg/router/route_group.go index b97a2b4b6d..88a7e4623b 100644 --- a/pkg/router/route_group.go +++ b/pkg/router/route_group.go @@ -80,10 +80,11 @@ type RouteGroup struct { // 'readCh' reads in incoming packets of this route group. // - Router should serve call '(*transport.Manager).ReadPacket' in a loop, // and push to the appropriate '(RouteGroup).readCh'. - readCh chan []byte // push reads from Router - readBuf bytes.Buffer // for read overflow - done chan struct{} - once sync.Once + readCh chan []byte // push reads from Router + readChMu sync.Mutex + readBuf bytes.Buffer // for read overflow + done chan struct{} + once sync.Once readDeadline deadline.PipeDeadline writeDeadline deadline.PipeDeadline @@ -256,7 +257,9 @@ func (r *RouteGroup) Close() error { r.once.Do(func() { close(r.done) - // close(r.readCh) // TODO(nkryuchkov): uncomment + r.readChMu.Lock() + close(r.readCh) + r.readChMu.Unlock() }) return nil diff --git a/pkg/router/route_group_test.go b/pkg/router/route_group_test.go index 139c95f1a0..46d9f3620c 100644 --- a/pkg/router/route_group_test.go +++ b/pkg/router/route_group_test.go @@ -465,12 +465,16 @@ func pushPackets(ctx context.Context, t *testing.T, from *transport.Manager, to panic("malformed packet") } + to.readChMu.Lock() select { case <-ctx.Done(): + to.readChMu.Unlock() return case <-to.done: + to.readChMu.Unlock() return case to.readCh <- payload: + to.readChMu.Unlock() } } } diff --git a/pkg/router/routerclient/wrappers.go b/pkg/router/routerclient/wrappers.go index 3c59a84179..f8f0d43773 100644 --- a/pkg/router/routerclient/wrappers.go +++ b/pkg/router/routerclient/wrappers.go @@ -12,7 +12,7 @@ import ( "github.com/SkycoinProject/skywire-mainnet/pkg/routing" ) -// TODO(nkryuchkov): remove this +// TODO: remove this // dmsgClientWrapper is a temporary workaround to make dmsg client implement `snet.Dialer`. // The only reason to use this is because client's `Dial` returns `*dmsg.Stream` instead of `net.Conn`, // so this stuff should be removed as soon as the func's signature changes