Просмотр исходного кода

fix(host): keep shutdown Safe writable and stop starving GET_TEMPERATURE

Mock Interrupt closed the UART-equivalent port, so race-timed Stop() could
skip the Safe SET_ALL. Temperature ticks also dropped while serial was busy
and heartbeat beat GET_TEMPERATURE, which left production RTL at GET_TEMP_AFTER=0.

Co-authored-by: Cursor <cursoragent@cursor.com>
root 3 месяцев назад
Родитель
Сommit
a4c9c0053a

+ 36 - 0
internal/backend/backend_test.go

@@ -102,3 +102,39 @@ func TestReplayReadsCapture(t *testing.T) {
 	}
 	_ = filepath.Separator
 }
+
+func TestMockInterruptDoesNotClosePort(t *testing.T) {
+	m := backend.NewMock(1, true)
+	ctx := context.Background()
+	if err := m.Open(ctx); err != nil {
+		t.Fatal(err)
+	}
+	m.SetHangGetOnce()
+	done := make(chan error, 1)
+	go func() {
+		_, err := m.GetTemperature(context.Background())
+		done <- err
+	}()
+	deadline := time.Now().Add(2 * time.Second)
+	for time.Now().Before(deadline) && !m.HangingGet() {
+		time.Sleep(5 * time.Millisecond)
+	}
+	if !m.HangingGet() {
+		t.Fatal("hung get never started")
+	}
+	m.Interrupt()
+	select {
+	case err := <-done:
+		if err == nil {
+			t.Fatal("hung get should unblock with an error")
+		}
+	case <-time.After(2 * time.Second):
+		t.Fatal("interrupt did not unblock hang")
+	}
+	if _, err := m.WriteSetAll(ctx, protocol.SetAll{Fan: [4]uint8{10, 10, 10, 10}}); err != nil {
+		t.Fatalf("interrupt must not close the port: %v", err)
+	}
+	if m.WriteCount() < 1 {
+		t.Fatal("expected SET_ALL after interrupt")
+	}
+}

+ 13 - 6
internal/backend/mock.go

@@ -77,16 +77,17 @@ func (m *Mock) Close() error {
 	return nil
 }
 
-// Interrupt unblocks a hung Get that ignores context. Close and Interrupt
-// must not wait for an in-flight protocol mutex.
+// Interrupt unblocks a hung Get that ignores context without closing the
+// port. Real serial Interrupt only kicks the reader so the next SET_ALL
+// (including shutdown Safe) can still be written. Close and Interrupt must
+// not wait for an in-flight protocol mutex.
 func (m *Mock) Interrupt() {
 	m.mu.Lock()
-	ignore := m.ignoreInterrupt
-	m.mu.Unlock()
-	if ignore {
+	defer m.mu.Unlock()
+	if m.ignoreInterrupt {
 		return
 	}
-	_ = m.Close()
+	m.hangGen++
 }
 
 func (m *Mock) SetIgnoreInterrupt(v bool) {
@@ -176,6 +177,12 @@ func (m *Mock) SetHangGetOnce() {
 	m.mu.Unlock()
 }
 
+func (m *Mock) HangingGet() bool {
+	m.mu.Lock()
+	defer m.mu.Unlock()
+	return m.hangingGet
+}
+
 func (m *Mock) Writes() []protocol.SetAll {
 	m.mu.Lock()
 	defer m.mu.Unlock()

+ 3 - 2
internal/device/device.go

@@ -353,8 +353,8 @@ func (d *Device) loop(ctx context.Context) {
 				d.enqueueSerial(serialJob{kind: jobGetMotor, timeout: d.cfg.GetMotorTimeout()})
 			}
 		case <-temp.C:
-			if d.backend.PollMode() == backend.PollUART && d.connected && d.startupDone && !d.serialInflight() {
-				if d.pendingSafeJob != nil || d.pendingResume != nil || len(d.pendingApply) > 0 || d.controlWindowOpen(d.clk.Now()) {
+			if d.backend.PollMode() == backend.PollUART && d.connected && d.startupDone {
+				if d.serialInflight() || d.pendingSafeJob != nil || d.pendingResume != nil || len(d.pendingApply) > 0 || d.controlWindowOpen(d.clk.Now()) {
 					d.pendingTemp = true
 					break
 				}
@@ -992,6 +992,7 @@ func (d *Device) applyMotorReport(rep *backend.MotorReport) {
 	}
 	if rep.Status.StartupComplete && !d.startupDone {
 		d.startupDone = true
+		d.pendingTemp = true
 		d.store.SetLifecycle(hthv1.DeviceLifecycleState_DEVICE_LIFECYCLE_STATE_READY)
 	}
 	if d.startupDone && d.store.Lifecycle() == hthv1.DeviceLifecycleState_DEVICE_LIFECYCLE_STATE_PROBING {

+ 19 - 2
internal/device/lifecycle.go

@@ -557,8 +557,25 @@ func (d *Device) runShutdown() {
 			ax.CommandID, ax.PeerUID, ax.PeerGID, ax.PeerPID = job.commandID, job.peerUID, job.peerGID, job.peerPID
 			ax.Origin = originShutdown
 		}
-		sj := d.serialJobFromAction(jobSetAllSafe, time.Until(deadline), ax)
-		if d.enqueueSerial(sj) {
+		to := time.Until(deadline)
+		if to < d.cfg.SetAllTimeout() {
+			to = d.cfg.SetAllTimeout()
+		}
+		sj := d.serialJobFromAction(jobSetAllSafe, to, ax)
+		enqueued := d.enqueueSerial(sj)
+		for !enqueued && d.connected && time.Now().Before(deadline) {
+			if d.serialInflight() {
+				select {
+				case sr := <-d.results:
+					d.applySerialResult(context.Background(), sr, &dummyB, &dummyN)
+				case <-time.After(20 * time.Millisecond):
+				}
+			} else {
+				time.Sleep(2 * time.Millisecond)
+			}
+			enqueued = d.enqueueSerial(sj)
+		}
+		if enqueued {
 			for d.serialInflight() && time.Now().Before(deadline) {
 				select {
 				case sr := <-d.results:

+ 6 - 5
internal/device/serial_worker.go

@@ -709,17 +709,18 @@ func (d *Device) kickPendingSerial(ctx context.Context) {
 		d.beginApply(ctx, r)
 		return
 	}
-	if d.pendingHB {
-		d.pendingHB = false
-		d.heartbeat(ctx)
-		return
-	}
 	if d.pendingTemp && d.startupDone {
 		if d.pendingSafeJob != nil || d.pendingResume != nil || len(d.pendingApply) > 0 || d.controlWindowOpen(d.clk.Now()) {
 			return
 		}
 		d.pendingTemp = false
 		d.enqueueSerial(serialJob{kind: jobGetTemp, timeout: d.cfg.GetTempTimeout(d.prof)})
+		return
+	}
+	if d.pendingHB {
+		d.pendingHB = false
+		d.heartbeat(ctx)
+		return
 	}
 }