try fix: #67 fatal error: concurrent map read and map write

This commit is contained in:
Leo
2023-01-29 21:32:33 +08:00
parent 6e88706d62
commit 003db157a9
4 changed files with 11 additions and 3 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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()
}
}
}

View File

@@ -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