From 38d28c3f4ab47ddcb8a1c0069964b723c86abc8f Mon Sep 17 00:00:00 2001 From: Audrius Butkevicius Date: Fri, 11 Nov 2016 22:23:48 +0000 Subject: [PATCH] lib/relay: Close invitation channel in all error cases (fixes #3726) --- cmd/syncthing/main.go | 2 ++ lib/connections/relay_listen.go | 3 ++- lib/relay/client/dynamic.go | 3 +-- lib/relay/client/static.go | 14 ++++++++++---- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index efee5101b..f61f90bcb 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -47,6 +47,8 @@ import ( "github.com/syncthing/syncthing/lib/upgrade" "github.com/thejerf/suture" + + _ "net/http/pprof" // Need to import this to support STPROFILER. ) var ( diff --git a/lib/connections/relay_listen.go b/lib/connections/relay_listen.go index fd8e6faaf..7fc67b62b 100644 --- a/lib/connections/relay_listen.go +++ b/lib/connections/relay_listen.go @@ -44,6 +44,7 @@ func (t *relayListener) Serve() { t.mut.Unlock() clnt, err := client.NewClient(t.uri, t.tlsCfg.Certificates, nil, 10*time.Second) + invitations := clnt.Invitations() if err != nil { t.mut.Lock() t.err = err @@ -62,7 +63,7 @@ func (t *relayListener) Serve() { for { select { - case inv, ok := <-t.client.Invitations(): + case inv, ok := <-invitations: if !ok { return } diff --git a/lib/relay/client/dynamic.go b/lib/relay/client/dynamic.go index 7100feb02..5e83c5144 100644 --- a/lib/relay/client/dynamic.go +++ b/lib/relay/client/dynamic.go @@ -48,6 +48,7 @@ func newDynamicClient(uri *url.URL, certs []tls.Certificate, invitations chan pr } func (c *dynamicClient) Serve() { + defer c.cleanup() c.mut.Lock() c.stop = make(chan struct{}) c.mut.Unlock() @@ -75,8 +76,6 @@ func (c *dynamicClient) Serve() { return } - defer c.cleanup() - var addrs []string for _, relayAnn := range ann.Relays { ruri, err := url.Parse(relayAnn.URL) diff --git a/lib/relay/client/static.go b/lib/relay/client/static.go index 123a6339a..07ca26554 100644 --- a/lib/relay/client/static.go +++ b/lib/relay/client/static.go @@ -63,6 +63,7 @@ func newStaticClient(uri *url.URL, certs []tls.Certificate, invitations chan pro } func (c *staticClient) Serve() { + defer c.cleanup() c.stop = make(chan struct{}) c.stopped = make(chan struct{}) defer close(c.stopped) @@ -156,10 +157,6 @@ func (c *staticClient) Serve() { } else { c.err = nil } - if c.closeInvitationsOnFinish { - close(c.invitations) - c.invitations = make(chan protocol.SessionInvitation) - } c.mut.Unlock() return @@ -209,6 +206,15 @@ func (c *staticClient) Invitations() chan protocol.SessionInvitation { return inv } +func (c *staticClient) cleanup() { + c.mut.Lock() + if c.closeInvitationsOnFinish { + close(c.invitations) + c.invitations = make(chan protocol.SessionInvitation) + } + c.mut.Unlock() +} + func (c *staticClient) connect() error { if c.uri.Scheme != "relay" { return fmt.Errorf("Unsupported relay schema: %v", c.uri.Scheme)