From 003db157a9c3fe425e4d3cd2673d516053c63ab4 Mon Sep 17 00:00:00 2001 From: Leo Date: Sun, 29 Jan 2023 21:32:33 +0800 Subject: [PATCH] try fix: #67 fatal error: concurrent map read and map write --- trace/icmp_ipv4.go | 2 ++ trace/icmp_ipv6.go | 2 ++ trace/tcp_ipv4.go | 6 +++++- trace/udp.go | 4 ++-- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/trace/icmp_ipv4.go b/trace/icmp_ipv4.go index efccd06..474512e 100644 --- a/trace/icmp_ipv4.go +++ b/trace/icmp_ipv4.go @@ -52,7 +52,9 @@ func (t *ICMPTracer) PrintFunc() { } func (t *ICMPTracer) Execute() (*Result, error) { + t.inflightRequestLock.Lock() t.inflightRequest = make(map[int]chan Hop) + t.inflightRequestLock.Unlock() if len(t.res.Hops) > 0 { return &t.res, ErrTracerouteExecuted diff --git a/trace/icmp_ipv6.go b/trace/icmp_ipv6.go index f6bfad3..c723e8c 100644 --- a/trace/icmp_ipv6.go +++ b/trace/icmp_ipv6.go @@ -53,7 +53,9 @@ func (t *ICMPTracerv6) PrintFunc() { } func (t *ICMPTracerv6) Execute() (*Result, error) { + t.inflightRequestLock.Lock() t.inflightRequest = make(map[int]chan Hop) + t.inflightRequestLock.Unlock() if len(t.res.Hops) > 0 { return &t.res, ErrTracerouteExecuted diff --git a/trace/tcp_ipv4.go b/trace/tcp_ipv4.go index fdf1af8..e1f386f 100644 --- a/trace/tcp_ipv4.go +++ b/trace/tcp_ipv4.go @@ -60,7 +60,10 @@ func (t *TCPTracer) Execute() (*Result, error) { var cancel context.CancelFunc t.ctx, cancel = context.WithCancel(context.Background()) defer cancel() + t.inflightRequestLock.Lock() t.inflightRequest = make(map[int]chan Hop) + t.inflightRequestLock.Unlock() + t.final = -1 go t.listenICMP() @@ -161,7 +164,7 @@ func (t *TCPTracer) listenTCP() { if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil { tcp, _ := tcpLayer.(*layers.TCP) // 取得目标主机的Sequence Number - + t.inflightRequestLock.Lock() if ch, ok := t.inflightRequest[int(tcp.Ack-1)]; ok { // 最后一跳 ch <- Hop{ @@ -169,6 +172,7 @@ func (t *TCPTracer) listenTCP() { Address: msg.Peer, } } + t.inflightRequestLock.Unlock() } } } diff --git a/trace/udp.go b/trace/udp.go index 7e1d5ca..6277466 100644 --- a/trace/udp.go +++ b/trace/udp.go @@ -121,8 +121,8 @@ func (t *UDPTracer) handleICMPMessage(msg ReceivedMessage, data []byte) { return } srcPort := util.GetUDPSrcPort(header) - //t.inflightRequestLock.Lock() - //defer t.inflightRequestLock.Unlock() + t.inflightRequestLock.Lock() + defer t.inflightRequestLock.Unlock() ch, ok := t.inflightRequest[int(srcPort)] if !ok { return