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.
Dart airdrop-alternativeclipboardclipboard-sharingclipboard-synccopy-pastecross-platformend-to-end-encryptionfile-syncfile-transferfluttergolanglinuxmacosnat-traversalp2pself-hostedtailscaleuniversal-clipboardwebrtcwindows
fix(core): never let a stuck shutdown keep the daemon lock
baiyuze034fe5ceb253
diff --git a/client-core/cmd/pasteferryd/main.go b/client-core/cmd/pasteferryd/main.goindex 0757b6b..f96af3f 100644--- a/client-core/cmd/pasteferryd/main.go+++ b/client-core/cmd/pasteferryd/main.go@@ -540,9 +540,26 @@ func run(dataDir string) error {loop.Run(ctx)slog.Info("正在退出")+ exitWatchdog(shutdownTimeout, os.Exit)return nil}+// shutdownTimeout 是收到退出信号后收尾的最长时间,见 exitWatchdog。+const shutdownTimeout = 10 * time.Second++// exitWatchdog 在收尾超时时强制结束进程。收尾(停 gRPC、关引擎、关数据库……)某一步卡住时,+// 进程不能一直占着单实例锁:界面重启或更新后台服务时,新进程会因「已有后台服务在运行」而退出,+// 旧的一直跑到重启。超时先把所有协程的堆栈写进日志,诊断报告里能看到卡在哪里。+// 退出码用 0:本来就是要退出,守护进程见到 0 不会再拉起。+func exitWatchdog(timeout time.Duration, exit func(int)) *time.Timer {+ return time.AfterFunc(timeout, func() {+ buf := make([]byte, 1<<20)+ n := runtime.Stack(buf, true)+ slog.Error("退出超时,强制结束", "timeout", timeout, "stacks", string(buf[:n]))+ exit(0)+ })+}+func resolvePaths(dataDir string) (config.Paths, error) {if dataDir != "" {abs, err := filepath.Abs(dataDir)diff --git a/client-core/cmd/pasteferryd/watchdog_test.go b/client-core/cmd/pasteferryd/watchdog_test.gonew file mode 100644index 0000000..b51f34f--- /dev/null+++ b/client-core/cmd/pasteferryd/watchdog_test.go@@ -0,0 +1,31 @@+package main++import (+ "bytes"+ "log/slog"+ "strings"+ "testing"+ "time"+)++func TestExitWatchdogLogsStacksThenExits(t *testing.T) {+ var buf bytes.Buffer+ prev := slog.Default()+ slog.SetDefault(slog.New(slog.NewTextHandler(&buf, nil)))+ defer slog.SetDefault(prev)++ codes := make(chan int, 1)+ exitWatchdog(10*time.Millisecond, func(code int) { codes <- code })+ select {+ case code := <-codes:+ if code != 0 {+ t.Fatalf("exit code = %d, want 0 so the supervisor does not restart it", code)+ }+ case <-time.After(5 * time.Second):+ t.Fatal("watchdog never fired")+ }+ // 堆栈里能看到卡住的协程,比如正在等它的这个测试+ if log := buf.String(); !strings.Contains(log, "退出超时") || !strings.Contains(log, "TestExitWatchdogLogsStacksThenExits") {+ t.Fatalf("log lacks the stacks: %s", log)+ }+}diff --git a/ui/lib/background_service_linux.dart b/ui/lib/background_service_linux.dartindex 8136d2c..ee5f21e 100644--- a/ui/lib/background_service_linux.dart+++ b/ui/lib/background_service_linux.dart@@ -90,12 +90,21 @@ class LinuxBackgroundService implements BackgroundService {return enabled;}- /// 守护进程与后台服务一起结束:只结束后台服务的话,守护进程会把它重新拉起来+ /// 守护进程与后台服务一起结束:只结束后台服务的话,守护进程会把它重新拉起来。+ /// 5 秒还没退出就强制结束:旧进程占着单实例锁,新启动的会以为已有实例在运行而退出。Future<void> _stop([String image = _image]) async {await Process.run('pkill', [..._ownedBy, '-x', image]);- for (var i = 0; i < 20 && await _running(image); i++) {+ if (await _waitGone(image, 20)) return;+ await Process.run('pkill', ['-KILL', ..._ownedBy, '-x', image]);+ await _waitGone(image, 8);+ }++ Future<bool> _waitGone(String image, int checks) async {+ for (var i = 0; i < checks; i++) {+ if (!await _running(image)) return true;await Future<void>.delayed(const Duration(milliseconds: 250));}+ return !await _running(image);}Future<bool> _running([String image = _image]) async {