From 06ca8941c7fd4c2436cff0266fdbcfeaa9c7f9a9 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Mon, 22 May 2023 23:10:44 +0100 Subject: [PATCH] Fix race condition between incoming and outgoing connection setup --- src/core/link.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/core/link.go b/src/core/link.go index 2bc11b2c..7fa1317f 100644 --- a/src/core/link.go +++ b/src/core/link.go @@ -268,8 +268,6 @@ func (l *links) add(u *url.URL, sintf string, linkType linkType) error { // Clear the error state. state.Lock() state._conn = lc - state._err = nil - state._errtime = time.Time{} state.Unlock() // Give the connection to the handler. The handler will block @@ -368,9 +366,17 @@ func (l *links) listen(u *url.URL, sintf string) (*Listener, error) { // connection. This prevents duplicate peerings. l.Lock() state, ok := l._links[info] - if ok && state != nil && state._conn != nil { - l.Unlock() - return + if ok && state != nil { + switch { + case state._conn != nil: + // We are already connected to something. + case state._conn == nil && state._errtime == time.Time{}: + // We aren't connected yet, but the fact that there + // is no last error suggests we haven't yet attempted + // an outbound connection at all. + l.Unlock() + return + } } if !ok || state == nil { state = &link{