From 47622fad36bb16a1002bd3f2156c4c195ac79b48 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sat, 2 Dec 2017 09:58:08 +1100 Subject: [PATCH] Fix the Stats panic in #214 --- client_test.go | 5 ++++- torrent.go | 11 ++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/client_test.go b/client_test.go index e06601f4..49b4d1ec 100644 --- a/client_test.go +++ b/client_test.go @@ -370,11 +370,14 @@ func testClientTransfer(t *testing.T, ps testClientTransferParams) { } seeder, err := NewClient(cfg) require.NoError(t, err) - defer seeder.Close() if ps.ExportClientStatus { testutil.ExportStatusWriter(seeder, "s") } seederTorrent, _, _ := seeder.AddTorrentSpec(TorrentSpecFromMetaInfo(mi)) + // Run a Stats right after Closing the Client. This will trigger the Stats + // panic in #214 caused by RemoteAddr on Closed uTP sockets. + defer seederTorrent.Stats() + defer seeder.Close() seederTorrent.VerifyData() // Create leecher and a Torrent. leecherDataDir, err := ioutil.TempDir("", "") diff --git a/torrent.go b/torrent.go index 8f93281c..4c5fd153 100644 --- a/torrent.go +++ b/torrent.go @@ -1345,7 +1345,12 @@ func (t *Torrent) Stats() TorrentStats { func (t *Torrent) numTotalPeers() int { peers := make(map[string]struct{}) for conn := range t.conns { - peers[conn.conn.RemoteAddr().String()] = struct{}{} + ra := conn.conn.RemoteAddr() + if ra == nil { + // It's been closed and doesn't support RemoteAddr. + continue + } + peers[ra.String()] = struct{}{} } for addr := range t.halfOpen { peers[addr] = struct{}{} @@ -1374,8 +1379,8 @@ func (t *Torrent) addConnection(c *connection, outgoing bool) bool { if outgoing == lower { // Close the other one. c0.Close() - // Is it safe to delete from the map while we're iterating - // over it? + // TODO: Is it safe to delete from the map while we're + // iterating over it? t.deleteConnection(c0) } else { // Abandon this one. -- 2.48.1