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): never mistake our own clipboard write for a copy

baiyuze2ecb86c7cdff
diff --git a/client-core/internal/clipboard/watcher.go b/client-core/internal/clipboard/watcher.go
index 68df916..9d654f2 100644
--- a/client-core/internal/clipboard/watcher.go
+++ b/client-core/internal/clipboard/watcher.go
@@ -119,7 +119,27 @@ func (w *Watcher) poll(ctx context.Context) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
- snap, err := CallResult(ctx, w.loop, w.cb.Peek)
+ // 探测与比对要在同一个主线程任务里做:write 也在主线程上更新 lastChange、selfWrites。
+ // 分开做的话,本机写入正好插在探测与比对之间时,比对拿的是写入前的值,
+ // 会把自己写进去的内容当成用户的新复制,再记一条、再发一遍
+ type result struct {
+ snap Snapshot
+ changed bool
+ first, mine bool
+ }
+ r, err := CallResult(ctx, w.loop, func() (result, error) {
+ snap, err := w.cb.Peek()
+ if err != nil || snap.ChangeCount == w.lastChange {
+ return result{snap: snap}, err
+ }
+ r := result{snap: snap, changed: true, first: w.lastChange < 0}
+ w.lastChange = snap.ChangeCount
+ if _, mine := w.selfWrites[snap.ChangeCount]; mine {
+ delete(w.selfWrites, snap.ChangeCount)
+ r.mine = true
+ }
+ return r, nil
+ })
if err != nil {
w.log.Debug("探测剪贴板失败", "err", err)
return
@@ -135,25 +155,20 @@ func (w *Watcher) poll(ctx context.Context) {
}
}
- if snap.ChangeCount == w.lastChange {
+ snap := r.snap
+ switch {
+ case !r.changed:
return
- }
- // 首轮只记录基线,不把启动前就存在的内容当作新复制广播出去
- first := w.lastChange < 0
- w.lastChange = snap.ChangeCount
- if first {
+ case r.first:
+ // 首轮只记录基线,不把启动前就存在的内容当作新复制广播出去
w.log.Debug("剪贴板监听已就绪", "changeCount", snap.ChangeCount)
return
- }
-
- // 本机刚写进去的内容不该被当成用户的新复制再同步一轮
- if _, mine := w.selfWrites[snap.ChangeCount]; mine {
- delete(w.selfWrites, snap.ChangeCount)
+ case r.mine:
+ // 本机刚写进去的内容不该被当成用户的新复制再同步一轮
w.log.Debug("跳过本机写入触发的变化", "changeCount", snap.ChangeCount)
return
- }
- // 密码等敏感内容:不同步,也不进复制记录
- if snap.Sensitive {
+ case snap.Sensitive:
+ // 密码等敏感内容:不同步,也不进复制记录
w.log.Debug("跳过标记为敏感的内容", "changeCount", snap.ChangeCount)
return
}
diff --git a/client-core/internal/clipboard/watcher_test.go b/client-core/internal/clipboard/watcher_test.go
index 78b920e..b22c08f 100644
--- a/client-core/internal/clipboard/watcher_test.go
+++ b/client-core/internal/clipboard/watcher_test.go
@@ -3,6 +3,7 @@ package clipboard
import (
"context"
"errors"
+ "sync/atomic"
"testing"
"time"
)
@@ -67,6 +68,42 @@ func TestWatcherConditionalWriteAndErrors(t *testing.T) {
}
}
+// 本机写入正好插在一次轮询的探测与比对之间,也不能被当成用户的新复制
+func TestOwnWritesNeverReportedWhilePolling(t *testing.T) {
+ loop := NewMainLoop()
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+ done := make(chan struct{})
+ go func() {
+ defer close(done)
+ for {
+ select {
+ case fn := <-loop.tasks:
+ fn()
+ case <-ctx.Done():
+ return
+ }
+ }
+ }()
+ t.Cleanup(func() { cancel(); <-done })
+
+ cb := &testClipboard{revision: 1}
+ w := NewWatcher(WatcherOptions{Clipboard: cb, MainLoop: loop, Interval: time.Microsecond})
+ var reported atomic.Int32
+ w.SetOnChange(func(Snapshot) { reported.Add(1) })
+ go w.Run(ctx)
+ time.Sleep(20 * time.Millisecond) // 先让它记下基线
+ for range 3000 {
+ if err := w.Write(ctx, Content{Kind: KindText, Text: "x"}); err != nil {
+ t.Fatal(err)
+ }
+ }
+ time.Sleep(20 * time.Millisecond)
+ if n := reported.Load(); n != 0 {
+ t.Fatalf("%d of our own writes were reported as new copies", n)
+ }
+}
+
func TestSettleWaitsForNotificationsToStop(t *testing.T) {
notify := make(chan struct{}, 1)
// 同一次复制连着来的几个通知:最后一个之后再等 delay