Skip to content

PasteFerry (隔空贴): clipboard and file sync across macOS, Windows and Linux. Direct-first, end-to-end encrypted. Optional official access: no server to host. Independent self-hosting remains available.

fix(core): don't crash when a received stream is closed mid-transfer

baiyuzeff5fa2ec1bf2
diff --git a/client-core/internal/transport/p2p/stream.go b/client-core/internal/transport/p2p/stream.go
index 41f352b..82a4de4 100644
--- a/client-core/internal/transport/p2p/stream.go
+++ b/client-core/internal/transport/p2p/stream.go
@@ -23,10 +23,17 @@ const streamQueueDepth = 256
//
// 这里用一个队列同时解决两者:OnMessage 在 OnDataChannel 回调中当场挂上,
// 只负责入队;消费者从队列按序取用。入队顺序即到达顺序,不存在插队。
+//
+// 本端主动关闭(Close)时,pion 可能正在回调里往队列送新到的分片:
+// 先关 stop 让卡在队列上的回调退出,再在写锁下关队列;回调在读锁下、确认没关过才送,
+// 不会往已关闭的队列里写(那会 panic,整个后台服务跟着退出)。
type Stream struct {
dc *webrtc.DataChannel
queue chan []byte
+ stop chan struct{}
+ mu sync.RWMutex
+ closed bool
closeOnce sync.Once
}
@@ -35,13 +42,22 @@ func NewStream(dc *webrtc.DataChannel) *Stream {
s := &Stream{
dc: dc,
queue: make(chan []byte, streamQueueDepth),
+ stop: make(chan struct{}),
}
dc.OnMessage(func(msg webrtc.DataChannelMessage) {
// 必须复制:pion 复用底层缓冲,留着原切片会被后续消息覆盖
chunk := make([]byte, len(msg.Data))
copy(chunk, msg.Data)
- s.queue <- chunk
+ s.mu.RLock()
+ defer s.mu.RUnlock()
+ if s.closed {
+ return
+ }
+ select {
+ case s.queue <- chunk:
+ case <-s.stop: // 本端已经不要了
+ }
})
dc.OnClose(func() { s.finish() })
@@ -87,5 +103,11 @@ func (s *Stream) Close() error {
}
func (s *Stream) finish() {
- s.closeOnce.Do(func() { close(s.queue) })
+ s.closeOnce.Do(func() {
+ close(s.stop)
+ s.mu.Lock()
+ s.closed = true
+ close(s.queue)
+ s.mu.Unlock()
+ })
}
diff --git a/client-core/internal/transport/p2p/stream_test.go b/client-core/internal/transport/p2p/stream_test.go
index e88c41b..9c9ebb2 100644
--- a/client-core/internal/transport/p2p/stream_test.go
+++ b/client-core/internal/transport/p2p/stream_test.go
@@ -166,3 +166,66 @@ func TestStreamBuffersEarlyMessages(t *testing.T) {
}
}
}
+
+// TestCloseStreamWhileReceiving 锁住一次真实的崩溃:接收方在数据还在进来时主动关闭数据流
+// (隔空传送里取消或判定中断时都会这样),pion 的回调随后往已关闭的队列里送分片,
+// 整个后台服务 panic 退出。Windows 的 CI 上先暴露出来。
+func TestCloseStreamWhileReceiving(t *testing.T) {
+ r := newRelay()
+ alice := newHarness(t, "alice", r)
+ bob := newHarness(t, "bob", r)
+ ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
+ defer cancel()
+ if err := alice.manager.Connect(ctx, "bob"); err != nil {
+ t.Fatalf("建立连接: %v", err)
+ }
+ waitConnected(t, alice, "bob")
+ waitConnected(t, bob, "alice")
+
+ chunk := make([]byte, 60*1024)
+ for round := 0; round < 5; round++ {
+ dc, err := alice.manager.OpenStream("bob", "flood")
+ if err != nil {
+ t.Fatal(err)
+ }
+ opened := make(chan struct{})
+ dc.OnOpen(func() { close(opened) })
+ select {
+ case <-opened:
+ case <-time.After(20 * time.Second):
+ t.Fatal("数据流未能打开")
+ }
+ stop := make(chan struct{})
+ go func() {
+ for {
+ select {
+ case <-stop:
+ return
+ default:
+ }
+ if dc.BufferedAmount() > 4<<20 {
+ time.Sleep(time.Millisecond)
+ continue
+ }
+ if dc.Send(chunk) != nil {
+ return
+ }
+ }
+ }()
+ st := <-bob.streams
+ // 先不读,等队列塞满:这时 pion 的回调卡在往队列写的那一步,再关掉
+ deadline := time.Now().Add(20 * time.Second)
+ for len(st.Chunks()) < cap(st.Chunks()) {
+ if time.Now().After(deadline) {
+ t.Fatal("队列一直没有塞满")
+ }
+ time.Sleep(5 * time.Millisecond)
+ }
+ time.Sleep(20 * time.Millisecond)
+ st.Close()
+ for range st.Chunks() {
+ }
+ close(stop)
+ dc.Close()
+ }
+}