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 resend a batch the receiver already finished

baiyuzef28ffcc80aca
diff --git a/client-core/internal/send/incoming.go b/client-core/internal/send/incoming.go
index 469b66e..b6c52ed 100644
--- a/client-core/internal/send/incoming.go
+++ b/client-core/internal/send/incoming.go
@@ -141,6 +141,12 @@ func (e *Engine) handleOffer(from string, offer *pb.SendOffer) {
switch {
case offer.GetResume():
+ if getErr == nil && existing.Direction == store.Incoming && existing.DeviceID == from &&
+ existing.Status == store.TransferDone {
+ // 已经收完了,只是结果没传到对方那里:告诉它,不要从头再发
+ answer(&pb.SendAnswer{Reason: "already_done"})
+ return
+ }
if getErr != nil || existing.Direction != store.Incoming || existing.DeviceID != from ||
store.TransferFinished(existing.Status) {
answer(&pb.SendAnswer{Reason: "unknown_transfer"})
diff --git a/client-core/internal/send/outgoing.go b/client-core/internal/send/outgoing.go
index 140f3db..f07c8dc 100644
--- a/client-core/internal/send/outgoing.go
+++ b/client-core/internal/send/outgoing.go
@@ -253,6 +253,13 @@ func (e *Engine) runOutgoing(t store.Transfer) {
return
}
if !answer.GetAccepted() {
+ if answer.GetReason() == "already_done" {
+ // 上次其实已经收完了,只是结果没传回来(比如走局域网快速通道时控制通道恰好断了)
+ t.DoneBytes = t.TotalBytes
+ e.finish(t, store.TransferDone, "")
+ e.log.Info("隔空传送已送达(对方上次已收完)", "transfer", t.ID, "to", t.DeviceID)
+ return
+ }
if answer.GetReason() == "unknown_transfer" && !fresh {
// 对方已经清掉了没收完的部分(或者换了电脑重装过),从头再发
fresh = true
@@ -299,7 +306,7 @@ func (e *Engine) runOutgoing(t store.Transfer) {
case <-ctx.Done():
e.stopped(t, context.Cause(ctx))
return
- case <-time.After(resultTimeout):
+ case <-time.After(e.opts.ResultTimeout):
e.interrupt(e.withProgress(t))
return
}
diff --git a/client-core/internal/send/pipe_test.go b/client-core/internal/send/pipe_test.go
index 38801f1..a8931f5 100644
--- a/client-core/internal/send/pipe_test.go
+++ b/client-core/internal/send/pipe_test.go
@@ -222,3 +222,30 @@ func TestNoLANAddrUsesDataChannel(t *testing.T) {
t.Fatalf("应当走数据通道:连上 %d 次,数据通道 %d 次", st.dials.Load(), st.channels.Load())
}
}
+
+// 文件走 TCP 收完了,「已收到」却没传回去(控制通道恰好断了):续传时对方说已经收完,
+// 发送方直接标为送达,不会从头再发一份
+func TestResultLostAfterLANTransfer(t *testing.T) {
+ p := newPair(t)
+ useLAN(t, p, false)
+ p.a.eng.opts.ResultTimeout = time.Second
+ p.b.mu.Lock()
+ p.b.dropResult = true
+ p.b.mu.Unlock()
+ src := t.TempDir()
+ data := randomBytes(t, 3<<20)
+ writeFile(t, filepath.Join(src, "a.bin"), data)
+ tr, err := p.a.eng.SendFiles("bob", []string{filepath.Join(src, "a.bin")}, false)
+ if err != nil {
+ t.Fatal(err)
+ }
+ waitStatus(t, p.b, tr.GetId(), store.TransferDone)
+ waitStatus(t, p.a, tr.GetId(), store.TransferInterrupted)
+
+ p.a.eng.PeerReady("bob")
+ waitStatus(t, p.a, tr.GetId(), store.TransferDone)
+ sameFile(t, data, filepath.Join(p.b.recvDir, "a.bin"))
+ if _, err := os.Stat(filepath.Join(p.b.recvDir, "a (2).bin")); !os.IsNotExist(err) {
+ t.Fatal("不该再收一份")
+ }
+}
diff --git a/client-core/internal/send/send.go b/client-core/internal/send/send.go
index 90c5277..df9fcf4 100644
--- a/client-core/internal/send/send.go
+++ b/client-core/internal/send/send.go
@@ -114,6 +114,7 @@ type Options struct {
// 测试用
AnswerTimeout time.Duration
+ ResultTimeout time.Duration
Now func() time.Time
}
@@ -145,6 +146,9 @@ func New(opts Options) *Engine {
if opts.AnswerTimeout <= 0 {
opts.AnswerTimeout = defaultAnswerTimeout
}
+ if opts.ResultTimeout <= 0 {
+ opts.ResultTimeout = resultTimeout
+ }
if opts.Now == nil {
opts.Now = time.Now
}
diff --git a/client-core/internal/send/send_test.go b/client-core/internal/send/send_test.go
index 7b9cd3f..da52141 100644
--- a/client-core/internal/send/send_test.go
+++ b/client-core/internal/send/send_test.go
@@ -26,11 +26,12 @@ type side struct {
db *store.Store
recvDir string
- mu sync.Mutex
- cfg config.Config
- offers []*pb.SendOffer // 发出去的 SendOffer,看续传位置用
- answers []*pb.SendAnswer
- blockOut bool // 模拟连接断了:控制消息发不出去
+ mu sync.Mutex
+ cfg config.Config
+ offers []*pb.SendOffer // 发出去的 SendOffer,看续传位置用
+ answers []*pb.SendAnswer
+ blockOut bool // 模拟连接断了:控制消息发不出去
+ dropResult bool // 模拟「已收到」的结果在路上丢了
}
func (s *side) config() config.Config {
@@ -121,6 +122,10 @@ func (p *pair) newSide(t *testing.T, id, peer string) *side {
Send: func(to string, msg *pb.PeerMessage) error {
s.mu.Lock()
blocked := s.blockOut
+ if msg.GetSendResult() != nil && s.dropResult {
+ s.mu.Unlock()
+ return nil
+ }
if o := msg.GetSendOffer(); o != nil {
s.offers = append(s.offers, o)
}
diff --git a/proto/gen/pasteferry/v1/wire.pb.go b/proto/gen/pasteferry/v1/wire.pb.go
index e54c1db..b94cefd 100644
--- a/proto/gen/pasteferry/v1/wire.pb.go
+++ b/proto/gen/pasteferry/v1/wire.pb.go
@@ -2981,7 +2981,7 @@ type SendAnswer struct {
Attempt uint32 `protobuf:"varint,2,opt,name=attempt,proto3" json:"attempt,omitempty"`
Accepted bool `protobuf:"varint,3,opt,name=accepted,proto3" json:"accepted,omitempty"`
// 回绝的原因,固定代码:disabled / no_space / dest_unavailable / file_too_large /
- // busy / unknown_transfer / cancelled
+ // busy / unknown_transfer / cancelled;already_done(2.5.1):续传时发现上次已经收完,发送方直接标为送达
Reason string `protobuf:"bytes,4,opt,name=reason,proto3" json:"reason,omitempty"`
ShortfallBytes int64 `protobuf:"varint,5,opt,name=shortfall_bytes,json=shortfallBytes,proto3" json:"shortfall_bytes,omitempty"` // no_space 时还差多少
ResumeIndex int64 `protobuf:"varint,6,opt,name=resume_index,json=resumeIndex,proto3" json:"resume_index,omitempty"` // 从第几个条目开始(全新传输为 0)
diff --git a/proto/pasteferry/v1/wire.proto b/proto/pasteferry/v1/wire.proto
index 9382940..bc10d80 100644
--- a/proto/pasteferry/v1/wire.proto
+++ b/proto/pasteferry/v1/wire.proto
@@ -371,7 +371,7 @@ message SendAnswer {
uint32 attempt = 2;
bool accepted = 3;
// 回绝的原因,固定代码:disabled / no_space / dest_unavailable / file_too_large /
- // busy / unknown_transfer / cancelled
+ // busy / unknown_transfer / cancelled;already_done(2.5.1):续传时发现上次已经收完,发送方直接标为送达
string reason = 4;
int64 shortfall_bytes = 5; // no_space 时还差多少
int64 resume_index = 6; // 从第几个条目开始(全新传输为 0)
diff --git a/ui/lib/gen/pasteferry/v1/wire.pb.dart b/ui/lib/gen/pasteferry/v1/wire.pb.dart
index 9659050..f63e594 100644
--- a/ui/lib/gen/pasteferry/v1/wire.pb.dart
+++ b/ui/lib/gen/pasteferry/v1/wire.pb.dart
@@ -3778,7 +3778,7 @@ class SendAnswer extends $pb.GeneratedMessage {
void clearAccepted() => $_clearField(3);
/// 回绝的原因,固定代码:disabled / no_space / dest_unavailable / file_too_large /
- /// busy / unknown_transfer / cancelled
+ /// busy / unknown_transfer / cancelled;already_done(2.5.1):续传时发现上次已经收完,发送方直接标为送达
@$pb.TagNumber(4)
$core.String get reason => $_getSZ(3);
@$pb.TagNumber(4)