Skip to content

Commit 43968a2

Browse files
committedAug 15, 2022
Close connection when handshake timeout occurs
Fixes #483
1 parent b8ebc62 commit 43968a2

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed
 

‎conn.go

+11
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,10 @@ func (c *Conn) handshake(ctx context.Context, cfg *handshakeConfig, initialFligh
896896
_ = c.close(false) //nolint:contextcheck
897897
}
898898
}
899+
if !c.isConnectionClosed() && errors.Is(err, context.Canceled) {
900+
c.log.Trace("handshake timeouts - closing underline connection")
901+
_ = c.close(false) //nolint:contextcheck
902+
}
899903
return
900904
}
901905
}
@@ -905,10 +909,12 @@ func (c *Conn) handshake(ctx context.Context, cfg *handshakeConfig, initialFligh
905909
case err := <-firstErr:
906910
cancelRead()
907911
cancel()
912+
c.handshakeLoopsFinished.Wait()
908913
return c.translateHandshakeCtxError(err)
909914
case <-ctx.Done():
910915
cancelRead()
911916
cancel()
917+
c.handshakeLoopsFinished.Wait()
912918
return c.translateHandshakeCtxError(ctx.Err())
913919
case <-done:
914920
return nil
@@ -941,13 +947,18 @@ func (c *Conn) close(byUser bool) error {
941947
if byUser {
942948
c.connectionClosedByUser = true
943949
}
950+
isClosed := c.isConnectionClosed()
944951
c.closed.Close()
945952
c.closeLock.Unlock()
946953

947954
if closedByUser {
948955
return ErrConnClosed
949956
}
950957

958+
if isClosed {
959+
return nil
960+
}
961+
951962
return c.nextConn.Close()
952963
}
953964

‎conn_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ func pipeConn(ca, cb net.Conn) (*Conn, *Conn, error) {
274274
// Receive client
275275
res := <-c
276276
if res.err != nil {
277+
_ = server.Close()
277278
return nil, nil, res.err
278279
}
279280

@@ -1349,6 +1350,7 @@ func TestCipherSuiteConfiguration(t *testing.T) {
13491350
res := <-c
13501351
if res.err == nil {
13511352
_ = server.Close()
1353+
_ = res.c.Close()
13521354
}
13531355
if !errors.Is(res.err, test.WantClientError) {
13541356
t.Errorf("TestSRTPConfiguration: Client Error Mismatch '%s': expected(%v) actual(%v)", test.Name, test.WantClientError, res.err)
@@ -1423,6 +1425,7 @@ func TestCertificateAndPSKServer(t *testing.T) {
14231425
res := <-c
14241426
if res.err == nil {
14251427
_ = server.Close()
1428+
_ = res.c.Close()
14261429
} else {
14271430
t.Errorf("TestCertificateAndPSKServer: Client Error Mismatch '%s': expected(%v) actual(%v)", test.Name, nil, res.err)
14281431
}

0 commit comments

Comments
 (0)
Please sign in to comment.