@cryptotaxi247 / kubo / commits / d184433e6

p2p: hold lock when handling new streams

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Sep 5, 2018 at 22:13 UTC d184433e6ddf8f18514ede840ce2f825265a704d
1 file changed +6 -3
p2p/listener.go
+6 -3
@@ -50,6 +50,7 @@ func newListenerRegistry(id peer.ID, host p2phost.Host) *ListenerRegistry {
50 host.SetStreamHandlerMatch("/x/", func(p string) bool {
51 reg.RLock()
52 defer reg.RUnlock()
53 +
54 for _, l := range reg.Listeners {
55 if l.ListenAddress().Equal(addr) && string(l.Protocol()) == p {
56 return true
@@ -58,13 +59,15 @@ func newListenerRegistry(id peer.ID, host p2phost.Host) *ListenerRegistry {
59
60 return false
61 }, func(stream net.Stream) {
62 + reg.RLock()
63 + defer reg.RUnlock()
64 +
65 for _, l := range reg.Listeners {
66 if l.ListenAddress().Equal(addr) && l.Protocol() == stream.Protocol() {
63 - l.(*remoteListener).handleStream(stream)
67 + go l.(*remoteListener).handleStream(stream)
68 + return
69 }
70 }
66 -
67 - // panic?
71 })
72
73 return reg