From 3312a29dde094c4dc0749234fcc8313638890f72 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Sun, 19 Nov 2017 18:28:02 +0100 Subject: [PATCH] lib/connections: Fix race condition in parallel dial, minor cleanups (fixes #4526) --- lib/connections/service.go | 4 ++-- lib/connections/structs.go | 6 +++++- lib/connections/tcp_listen.go | 13 ++++++------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/connections/service.go b/lib/connections/service.go index c197abf42..72cd1e790 100644 --- a/lib/connections/service.go +++ b/lib/connections/service.go @@ -746,13 +746,13 @@ func dialParallel(deviceID protocol.DeviceID, dialTargets []dialTarget) (interna wg := sync.NewWaitGroup() for _, tgt := range tgts { wg.Add(1) - go func() { + go func(tgt dialTarget) { conn, err := tgt.Dial() if err == nil { res <- conn } wg.Done() - }() + }(tgt) } // Spawn a routine which will unblock main routine in case we fail diff --git a/lib/connections/structs.go b/lib/connections/structs.go index 633361343..0dfad5b0e 100644 --- a/lib/connections/structs.go +++ b/lib/connections/structs.go @@ -188,6 +188,10 @@ type dialTarget struct { func (t dialTarget) Dial() (internalConn, error) { l.Debugln("dialing", t.deviceID, t.uri, "prio", t.priority) conn, err := t.dialer.Dial(t.deviceID, t.uri) - l.Debugln("dialing", t.deviceID, t.uri, "outcome", conn, err) + if err != nil { + l.Debugln("dialing", t.deviceID, t.uri, "error:", err) + } else { + l.Debugln("dialing", t.deviceID, t.uri, "success:", conn) + } return conn, err } diff --git a/lib/connections/tcp_listen.go b/lib/connections/tcp_listen.go index 437dced36..1b9d48820 100644 --- a/lib/connections/tcp_listen.go +++ b/lib/connections/tcp_listen.go @@ -102,19 +102,18 @@ func (t *tcpListener) Serve() { l.Debugln("Listen (BEP/tcp): connect from", conn.RemoteAddr()) - err = dialer.SetTCPOptions(conn) - if err != nil { + if err := dialer.SetTCPOptions(conn); err != nil { l.Debugln("Listen (BEP/tcp): setting tcp options:", err) } - err = dialer.SetTrafficClass(conn, t.cfg.Options().TrafficClass) - if err != nil { - l.Debugln("Listen (BEP/tcp): setting traffic class:", err) + if tc := t.cfg.Options().TrafficClass; tc != 0 { + if err := dialer.SetTrafficClass(conn, tc); err != nil { + l.Debugln("Listen (BEP/tcp): setting traffic class:", err) + } } tc := tls.Server(conn, t.tlsCfg) - err = tlsTimedHandshake(tc) - if err != nil { + if err := tlsTimedHandshake(tc); err != nil { l.Infoln("Listen (BEP/tcp): TLS handshake:", err) tc.Close() continue