From e9adf327b019482beb98eb2a86809a67999e8fa0 Mon Sep 17 00:00:00 2001 From: Arceliar Date: Wed, 7 Feb 2018 17:48:30 -0600 Subject: [PATCH] possibly fix deadlock from race in peer linkloop goroutine, add some related debug code to the admin --- src/yggdrasil/admin.go | 27 +++++++++++++++++++++++++-- src/yggdrasil/router.go | 37 ++++++++++++++----------------------- src/yggdrasil/tcp.go | 11 ++++------- src/yggdrasil/udp.go | 11 ++++------- 4 files changed, 47 insertions(+), 39 deletions(-) diff --git a/src/yggdrasil/admin.go b/src/yggdrasil/admin.go index 419d7f1e..63b6d60d 100644 --- a/src/yggdrasil/admin.go +++ b/src/yggdrasil/admin.go @@ -41,11 +41,14 @@ func (a *admin) init(c *Core, listenaddr string) { *out = a.getResponse_dot() }) a.addHandler("getSelf", nil, func(out *[]byte, _ ...string) { - *out = []byte(a.printInfos(a.getData_getPeers())) + *out = []byte(a.printInfos([]admin_nodeInfo{*a.getData_getSelf()})) }) a.addHandler("getPeers", nil, func(out *[]byte, _ ...string) { *out = []byte(a.printInfos(a.getData_getPeers())) }) + a.addHandler("getSwitchPeers", nil, func(out *[]byte, _ ...string) { + *out = []byte(a.printInfos(a.getData_getSwitchPeers())) + }) a.addHandler("getDHT", nil, func(out *[]byte, _ ...string) { *out = []byte(a.printInfos(a.getData_getDHT())) }) @@ -152,6 +155,26 @@ func (a *admin) getData_getSelf() *admin_nodeInfo { } func (a *admin) getData_getPeers() []admin_nodeInfo { + ports := a.core.peers.ports.Load().(map[switchPort]*peer) + var peerInfos []admin_nodeInfo + var ps []switchPort + for port := range ports { + ps = append(ps, port) + } + sort.Slice(ps, func(i, j int) bool { return ps[i] < ps[j] }) + for _, port := range ps { + p := ports[port] + addr := *address_addrForNodeID(getNodeID(&p.box)) + info := admin_nodeInfo{ + {"IP", net.IP(addr[:]).String()}, + {"port", fmt.Sprint(port)}, + } + peerInfos = append(peerInfos, info) + } + return peerInfos +} + +func (a *admin) getData_getSwitchPeers() []admin_nodeInfo { var peerInfos []admin_nodeInfo table := a.core.switchTable.table.Load().(lookupTable) peers := a.core.peers.ports.Load().(map[switchPort]*peer) @@ -211,7 +234,7 @@ func (a *admin) getData_getSessions() []admin_nodeInfo { func (a *admin) getResponse_dot() []byte { self := a.getData_getSelf().asMap() myAddr := self["IP"] - peers := a.getData_getPeers() + peers := a.getData_getSwitchPeers() dht := a.getData_getDHT() sessions := a.getData_getSessions() // Map of coords onto IP diff --git a/src/yggdrasil/router.go b/src/yggdrasil/router.go index 5c02943c..15e2ff3c 100644 --- a/src/yggdrasil/router.go +++ b/src/yggdrasil/router.go @@ -41,17 +41,15 @@ type router struct { func (r *router) init(core *Core) { r.core = core r.addr = *address_addrForNodeID(&r.core.dht.nodeID) - in := make(chan []byte, 32) // TODO something better than this... + in := make(chan []byte, 32) // TODO something better than this... p := r.core.peers.newPeer(&r.core.boxPub, &r.core.sigPub) //, out, in) p.out = func(packet []byte) { // This is to make very sure it never blocks - for { - select { - case in <- packet: - return - default: - util_putBytes(<-in) - } + select { + case in <- packet: + return + default: + util_putBytes(packet) } } r.in = in @@ -147,13 +145,10 @@ func (r *router) sendPacket(bs []byte) { fallthrough //default: go func() { sinfo.send<-bs }() default: - for { - select { - case sinfo.send <- bs: - return - default: - util_putBytes(<-sinfo.send) - } + select { + case sinfo.send <- bs: + default: + util_putBytes(bs) } } } @@ -191,7 +186,6 @@ func (r *router) handleIn(packet []byte) { case wire_ProtocolTraffic: r.handleProto(packet) default: /*panic("Should not happen in testing") ;*/ - return } } @@ -206,13 +200,10 @@ func (r *router) handleTraffic(packet []byte) { return } //go func () { sinfo.recv<-&p }() - for { - select { - case sinfo.recv <- &p: - return - default: - util_putBytes((<-sinfo.recv).payload) - } + select { + case sinfo.recv <- &p: + default: + util_putBytes(p.payload) } } diff --git a/src/yggdrasil/tcp.go b/src/yggdrasil/tcp.go index ad9139a6..a190679f 100644 --- a/src/yggdrasil/tcp.go +++ b/src/yggdrasil/tcp.go @@ -176,13 +176,10 @@ func (iface *tcpInterface) handler(sock *net.TCPConn) { }() p.out = func(msg []byte) { defer func() { recover() }() - for { - select { - case out <- msg: - return - default: - util_putBytes(<-out) - } + select { + case out <- msg: + default: + util_putBytes(msg) } } sock.SetNoDelay(true) diff --git a/src/yggdrasil/udp.go b/src/yggdrasil/udp.go index 8d88df1d..81ff142e 100644 --- a/src/yggdrasil/udp.go +++ b/src/yggdrasil/udp.go @@ -224,13 +224,10 @@ func (iface *udpInterface) handleKeys(msg []byte, addr connAddr) { } conn.peer.out = func(msg []byte) { defer func() { recover() }() - for { - select { - case conn.out <- msg: - return - default: - util_putBytes(<-conn.out) - } + select { + case conn.out <- msg: + default: + util_putBytes(msg) } } go func() {