Skip to content

Commit

Permalink
Merge pull request #7 from drpcorg/fix-panic-on-close
Browse files Browse the repository at this point in the history
fix panic on net close
  • Loading branch information
Termina1 authored Aug 15, 2024
2 parents f477fce + cfddcda commit df59522
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion protocol/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ func (n *Net) Close() error {
n.listens.Clear()

n.conns.Range(func(_ string, p *Peer) bool {
p.Close()
// sometimes it can be nil when we started connecting, but haven't connected yet
if p != nil {
p.Close()
}
return true
})
n.conns.Clear()
Expand Down
18 changes: 18 additions & 0 deletions protocol/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,21 @@ func TestTCPDepot_Connect(t *testing.T) {
err = l.Close()
assert.Nil(t, err)
}

func TestTCPDepot_ConnectFailed(t *testing.T) {
loop := "tls://127.0.0.1:32000"

log := utils.NewDefaultLogger(slog.LevelDebug)

cCon := utils.NewFDQueue[Records](16, time.Millisecond)
c := NewNet(log, nil, func(_ string) FeedDrainCloser { return cCon }, func(_ string) { cCon.Close() })
c.TlsConfig = tlsConfig("b.chotki.local")

err := c.Connect(context.Background(), loop)
assert.Nil(t, err)
time.Sleep(time.Second) // Wait connection, todo use events

// cleanup
err = c.Close()
assert.Nil(t, err)
}

0 comments on commit df59522

Please sign in to comment.