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): stop the supervisor first when replacing a folder install

baiyuze25702440081f
diff --git a/client-core/internal/update/apply.go b/client-core/internal/update/apply.go
index f38a775..68d5a9a 100644
--- a/client-core/internal/update/apply.go
+++ b/client-core/internal/update/apply.go
@@ -143,16 +143,17 @@ func RunHelper(planPath string, detach bool) int {
return 0
}
-// ReadResult 读出并删掉上一次安装的结果。没有就返回 nil。
+// ReadResult 读出上一次安装的结果,没有或已过时就返回 nil。
+// 读了不删:安装后后台服务可能被界面再重启一两次,每一次都要能告诉界面结果;过时的才删掉。
func ReadResult(dataDir string) *Result {
path := filepath.Join(dataDir, "updates", "result.json")
raw, err := os.ReadFile(path)
if err != nil {
return nil
}
- _ = os.Remove(path)
var r Result
- if json.Unmarshal(raw, &r) != nil || r.To == "" {
+ if json.Unmarshal(raw, &r) != nil || r.To == "" || time.Since(time.Unix(r.At, 0)) > resultFresh {
+ _ = os.Remove(path)
return nil
}
return &r
@@ -179,7 +180,15 @@ func copyFile(src, dst string, mode os.FileMode) error {
// swapDir 把 staged 换到 app 的位置,旧的改名为 old。失败时尽量还原。
func swapDir(app, staged, old string) error {
_ = os.RemoveAll(old)
- if err := os.Rename(app, old); err != nil {
+ // Windows 上刚结束的进程可能还占着文件一会儿,杀毒软件也可能正在扫描,多等几次
+ var err error
+ for deadline := time.Now().Add(15 * time.Second); ; {
+ if err = os.Rename(app, old); err == nil || time.Now().After(deadline) {
+ break
+ }
+ time.Sleep(500 * time.Millisecond)
+ }
+ if err != nil {
return fmt.Errorf("挪开旧版本: %w", err)
}
if err := os.Rename(staged, app); err != nil {
@@ -192,8 +201,10 @@ func swapDir(app, staged, old string) error {
}
// stopDaemons 结束计划里位于 App 里的后台服务与守护进程,并等它们退出。
+// Daemons 是 [后台服务, 它的父进程]:倒着结束,先结束守护进程,否则它会把刚结束的后台服务重新拉起来。
func stopDaemons(p *Plan, log *slog.Logger) {
- for _, pid := range p.Daemons {
+ for i := len(p.Daemons) - 1; i >= 0; i-- {
+ pid := p.Daemons[i]
if pid <= 0 || !processInDir(pid, p.App) {
continue
}
diff --git a/client-core/internal/update/service.go b/client-core/internal/update/service.go
index 9f32e24..88f56b6 100644
--- a/client-core/internal/update/service.go
+++ b/client-core/internal/update/service.go
@@ -41,7 +41,6 @@ type Service struct {
lastAuto time.Time
files map[string]string // 版本 → 已下载并校验过的安装包
result *Result
- resultAt time.Time
}
// NewService 读出上一次安装的结果(如果有)。
@@ -56,9 +55,7 @@ func NewService(current, root string, log *slog.Logger) (*Service, error) {
Checker: &Checker{HTTP: client, Sources: LoadSources(root), Keys: PublicKeys()},
files: map[string]string{},
}
- if r := ReadResult(root); r != nil {
- s.result, s.resultAt = r, time.Now()
- }
+ s.result = ReadResult(root)
return s, nil
}
@@ -135,7 +132,7 @@ func (s *Service) Notice() string {
func (s *Service) LastResult() *Result {
s.mu.Lock()
defer s.mu.Unlock()
- if s.result == nil || time.Since(s.resultAt) > resultFresh {
+ if s.result == nil || time.Since(time.Unix(s.result.At, 0)) > resultFresh {
return nil
}
return s.result
diff --git a/client-core/internal/update/update_test.go b/client-core/internal/update/update_test.go
index 5d8eeba..b90f114 100644
--- a/client-core/internal/update/update_test.go
+++ b/client-core/internal/update/update_test.go
@@ -331,16 +331,25 @@ func TestServiceNoticeRespectsSettings(t *testing.T) {
}
}
-func TestReadResultOnce(t *testing.T) {
+func TestReadResultUntilStale(t *testing.T) {
root := t.TempDir()
os.MkdirAll(filepath.Join(root, "updates"), 0o700)
raw, _ := json.Marshal(Result{From: "2.3.0", To: "2.3.1", OK: true, At: time.Now().Unix()})
os.WriteFile(filepath.Join(root, "updates", "result.json"), raw, 0o600)
- if r := ReadResult(root); r == nil || !r.OK || r.To != "2.3.1" {
- t.Fatalf("result: %+v", r)
+ // 后台服务可能被重启几次,每次都要读得到
+ for i := 0; i < 2; i++ {
+ if r := ReadResult(root); r == nil || !r.OK || r.To != "2.3.1" {
+ t.Fatalf("read %d: %+v", i, r)
+ }
}
+ // 过时的结果不再提,文件也删掉
+ raw, _ = json.Marshal(Result{From: "2.3.0", To: "2.3.1", OK: true, At: time.Now().Add(-time.Hour).Unix()})
+ os.WriteFile(filepath.Join(root, "updates", "result.json"), raw, 0o600)
if ReadResult(root) != nil {
- t.Fatal("result read twice")
+ t.Fatal("stale result shown")
+ }
+ if _, err := os.Stat(filepath.Join(root, "updates", "result.json")); !os.IsNotExist(err) {
+ t.Fatal("stale result kept")
}
}