提交记录修改internal/workbench/lifecycle.go+18 −1看文件
修改internal/workbench/monitor_timing_test.go+62 −0看文件
修改internal/workbench/safety.go+4 −0看文件
修改internal/workbench/server.go+1 −0看文件
| @@ -34,7 +34,7 @@ func (s *Server) startMonitors() { | ||
| 34 | 34 | return |
| 35 | 35 | } |
| 36 | 36 | ctx := s.lifetimeContextLocked() |
| 37 | 删除: s.monitors.Add(3) | |
| 37 | 新增: s.monitors.Add(4) | |
| 38 | 38 | s.mu.Unlock() |
| 39 | 39 | go func() { |
| 40 | 40 | defer s.monitors.Done() |
| @@ -55,6 +55,23 @@ func (s *Server) startMonitors() { | ||
| 55 | 55 | defer s.monitors.Done() |
| 56 | 56 | s.monitor(ctx) |
| 57 | 57 | }() |
| 58 | 新增: go func() { | |
| 59 | 新增: defer s.monitors.Done() | |
| 60 | 新增: ticker := time.NewTicker(5 * time.Second) | |
| 61 | 新增: defer ticker.Stop() | |
| 62 | 新增: for ctx.Err() == nil { | |
| 63 | 新增: // Long-running maintenance jobs own their own monitor and stop it | |
| 64 | 新增: // before changing guest components. Other jobs cancel observation. | |
| 65 | 新增: if !s.busy() { | |
| 66 | 新增: s.monitorSafety() | |
| 67 | 新增: } | |
| 68 | 新增: select { | |
| 69 | 新增: case <-ctx.Done(): | |
| 70 | 新增: return | |
| 71 | 新增: case <-ticker.C: | |
| 72 | 新增: } | |
| 73 | 新增: } | |
| 74 | 新增: }() | |
| 58 | 75 | go func() { |
| 59 | 76 | defer s.monitors.Done() |
| 60 | 77 | // macOS performs both probes from the same tick. Its periodic SSH |
| @@ -73,6 +73,68 @@ func TestNetworkMonitorRecordsRepeatedLockSkips(t *testing.T) { | ||
| 73 | 73 | } |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 新增:func TestSafetyMonitorRunsWhileOperationLockIsHeld(t *testing.T) { | |
| 77 | 新增: s := testServer(t) | |
| 78 | 新增: s.ready = true | |
| 79 | 新增: s.safety.VerifiedAt = time.Now() | |
| 80 | 新增: s.op.Lock() | |
| 81 | 新增: s.startMonitors() | |
| 82 | 新增: deadline := time.Now().Add(2 * time.Second) | |
| 83 | 新增: var found bool | |
| 84 | 新增: for time.Now().Before(deadline) { | |
| 85 | 新增: if _, err := os.Stat(filepath.Join(s.profile, "logs", "events.jsonl")); err == nil { | |
| 86 | 新增: for _, record := range monitorTimingRecords(t, s, "safety") { | |
| 87 | 新增: found = found || record["result"] == "not_due" | |
| 88 | 新增: } | |
| 89 | 新增: } | |
| 90 | 新增: if found { | |
| 91 | 新增: break | |
| 92 | 新增: } | |
| 93 | 新增: time.Sleep(10 * time.Millisecond) | |
| 94 | 新增: } | |
| 95 | 新增: s.op.Unlock() | |
| 96 | 新增: s.closeBackground() | |
| 97 | 新增: if !found { | |
| 98 | 新增: t.Fatal("safety monitoring was blocked by an unrelated operation") | |
| 99 | 新增: } | |
| 100 | 新增:} | |
| 101 | 新增: | |
| 102 | 新增:func TestConcurrentSafetyMonitorKeepsManualPause(t *testing.T) { | |
| 103 | 新增: s, gate := grantedToolUpdateServer(t) | |
| 104 | 新增: s.setJobForTest(Job{}) | |
| 105 | 新增: s.safety.VerifiedAt = time.Now().Add(-time.Minute) | |
| 106 | 新增: started := make(chan struct{}) | |
| 107 | 新增: finished := make(chan struct{}) | |
| 108 | 新增: go func() { | |
| 109 | 新增: defer close(finished) | |
| 110 | 新增: s.monitorSafetyWithProbe(func(ctx context.Context, _ cage.Config) (string, error) { | |
| 111 | 新增: close(started) | |
| 112 | 新增: <-ctx.Done() | |
| 113 | 新增: return "", ctx.Err() | |
| 114 | 新增: }) | |
| 115 | 新增: }() | |
| 116 | 新增: select { | |
| 117 | 新增: case <-started: | |
| 118 | 新增: case <-time.After(3 * time.Second): | |
| 119 | 新增: t.Fatal("monitor did not start") | |
| 120 | 新增: } | |
| 121 | 新增: s.monitorSafetyWithProbe(func(context.Context, cage.Config) (string, error) { | |
| 122 | 新增: t.Error("overlapping monitor started a second probe") | |
| 123 | 新增: return "", nil | |
| 124 | 新增: }) | |
| 125 | 新增: if err := s.pauseNetworkNow("user pause"); err != nil { | |
| 126 | 新增: t.Fatal(err) | |
| 127 | 新增: } | |
| 128 | 新增: select { | |
| 129 | 新增: case <-finished: | |
| 130 | 新增: case <-time.After(3 * time.Second): | |
| 131 | 新增: t.Fatal("paused monitor did not stop") | |
| 132 | 新增: } | |
| 133 | 新增: if gate.Status().Mode != cage.GateClosed { | |
| 134 | 新增: t.Fatal("monitor reopened networking after manual pause") | |
| 135 | 新增: } | |
| 136 | 新增:} | |
| 137 | 新增: | |
| 76 | 138 | func TestMonitorTimingAttributesSlowProbe(t *testing.T) { |
| 77 | 139 | s, gate := grantedToolUpdateServer(t) |
| 78 | 140 | s.setJobForTest(Job{}) |
| @@ -781,6 +781,10 @@ func (s *Server) monitorSafety() { | ||
| 781 | 781 | } |
| 782 | 782 | |
| 783 | 783 | func (s *Server) monitorSafetyWithProbe(probe func(context.Context, cage.Config) (string, error)) { |
| 784 | 新增: if !s.monitorSafetyOp.TryLock() { | |
| 785 | 新增: return | |
| 786 | 新增: } | |
| 787 | 新增: defer s.monitorSafetyOp.Unlock() | |
| 784 | 788 | timing := s.startMonitorTiming() |
| 785 | 789 | defer s.recordMonitorTiming("safety", timing) |
| 786 | 790 | s.mu.Lock() |
| @@ -110,6 +110,7 @@ type Server struct { | ||
| 110 | 110 | recovery networkRecovery |
| 111 | 111 | locations map[string]Location |
| 112 | 112 | monitorCancel context.CancelFunc |
| 113 | 新增: monitorSafetyOp sync.Mutex | |
| 113 | 114 | safetyOp sync.Mutex |
| 114 | 115 | networkPaused bool |
| 115 | 116 | proxyNetworkLost bool |

