1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-22 03:20:30 +08:00

fix(recv): panic on expired fragment get

This commit is contained in:
源文雨
2026-04-09 01:44:24 +08:00
parent b86a65819c
commit 9e642f875a
8 changed files with 338 additions and 36 deletions

View File

@@ -5,7 +5,6 @@ import (
"errors"
"net"
"sync/atomic"
"time"
"github.com/fumiama/WireGold/config"
"github.com/fumiama/WireGold/gold/head"
@@ -46,8 +45,8 @@ type Link struct {
keys [32]cipher.AEAD
// 本机信息
me *Me
// 最后一次收到报文的时间
lastalive *time.Time
// 最后一次收到报文的时间 (UnixNano)
lastalive atomic.Int64
// 是否允许转发
allowtrans bool
// 是否对数据进行 zstd 压缩

View File

@@ -2,9 +2,7 @@ package link
import (
"encoding/json"
"sync/atomic"
"time"
"unsafe"
"github.com/sirupsen/logrus"
@@ -23,8 +21,8 @@ func (l *Link) keepAlive(dur int64) {
if l.me.connections == nil {
return
}
la := (*time.Time)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&l.lastalive))))
if la != nil && time.Since(*la) > 10*time.Second*time.Duration(dur) { // 可能已经被阻断, 断开重连
la := l.lastalive.Load()
if la != 0 && time.Since(time.Unix(0, la)) > 10*time.Second*time.Duration(dur) { // 可能已经被阻断, 断开重连
logrus.Warnln(file.Header(), "no response after 10 keep alive tries, re-connecting...")
err := l.me.Restart()
if err != nil {

View File

@@ -2,9 +2,7 @@ package link
import (
"net"
"sync/atomic"
"time"
"unsafe"
curve "github.com/fumiama/go-x25519"
"github.com/sirupsen/logrus"
@@ -148,7 +146,6 @@ func (m *Me) extractPeer(srcip, dstip net.IP, addr p2p.EndPoint) *Link {
p.endpoint = addr
}
}
now := time.Now()
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&p.lastalive)), unsafe.Pointer(&now))
p.lastalive.Store(time.Now().UnixNano())
return p
}

View File

@@ -152,10 +152,18 @@ func (m *Me) wait(data []byte, addr p2p.EndPoint) (h head.PacketBytes) {
h, got := m.recving.GetOrSet(uint16(seq), header)
if got {
if h == header {
if !h.HasInit() {
// GetOrSet found an expired entry: it deleted it and
// returned zero-value while reporting got=true, but did
// NOT store our header. Re-store it now.
m.recving.Set(uint16(seq), header)
h = header
got = false
} else if h == header {
panic("unexpected multi-put found")
} else {
header.ManualDestroy()
}
header.ManualDestroy()
}
if config.ShowDebugLog {
logrus.Debugln("[recv]", strconv.FormatUint(uint64(seq&0xffff), 16), "get frag part isnew:", !got)