diff --git a/go/internal/storage/recall_write_buffer.go b/go/internal/storage/recall_write_buffer.go index f67940d..d4ac4ce 100644 --- a/go/internal/storage/recall_write_buffer.go +++ b/go/internal/storage/recall_write_buffer.go @@ -45,6 +45,12 @@ type BatchRecallUpdater interface { const ( defaultRecallFlushInterval = 300 * time.Second defaultRecallFlushMaxItems = 256 + // 优雅停止窗口(t_fa797edd):Stop 内的「等后台协程退出 + 最终落盘」都必须有界。 + // 否则 Rust/LanceDB 批量更新在活 IPC 流量下阻塞时,Stop 永远不返回 → 信号协程 + // 到不了 srv.Shutdown → 进程跑满 systemd 90s TimeoutStopSec 被 SIGKILL + // (丢写风险 + 每次重启都变强杀 + 40s IPC 中断)。 + defaultRecallStopLoopWait = 5 * time.Second // 等后台 flush 协程退出的上限 + defaultRecallStopFlushWait = 8 * time.Second // 最终落盘的上限(召回统计,丢失可接受) ) type pendingRecall struct { @@ -249,6 +255,10 @@ func (b *RecallWriteBuffer) Flush() (int, error) { } // Stop 停止后台 flush 协程,并把最后一个窗口落盘(进程优雅退出时调用)。 +// 全程有界(t_fa797edd):等后台协程 ≤ defaultRecallStopLoopWait,最终落盘 +// ≤ defaultRecallStopFlushWait。Rust/LanceDB 批量更新在活 IPC 流量下可能阻塞 +// 数十秒;召回统计丢了可接受(源码注释「进程崩溃会丢最后一个窗口」), +// 因此超时即放弃落盘、放行退出,保证进程总在有限秒内到 srv.Shutdown。 func (b *RecallWriteBuffer) Stop() { b.mu.Lock() if b.stopped { @@ -261,10 +271,23 @@ func (b *RecallWriteBuffer) Stop() { close(b.stopCh) select { case <-b.doneCh: - case <-time.After(10 * time.Second): - log.Printf("[recall-buffer] 停止超时,直接落盘剩余窗口") + case <-time.After(defaultRecallStopLoopWait): + log.Printf("[recall-buffer] 后台 flush 协程停止超时(%s),直接进入最终落盘", defaultRecallStopLoopWait) + } + + // 最终落盘有界:放到 goroutine 跑,到点不再等。若后台 ticker/阈值 flush + // 正持锁阻塞在 Rust IPC 上,本 goroutine 的 Flush 会先等 b.mu —— 同样被 + // defaultRecallStopFlushWait 兜住,超时即放行进程退出。 + flushDone := make(chan struct{}) + go func() { + b.Flush() + close(flushDone) + }() + select { + case <-flushDone: + case <-time.After(defaultRecallStopFlushWait): + log.Printf("[recall-buffer] 最终落盘超时(%s),放弃剩余窗口(仅丢召回统计,进程照常退出)", defaultRecallStopFlushWait) } - b.Flush() } // Stats 观测用(flush 频率 / 合并率 / 版本写入行数)。 diff --git a/go/internal/storage/recall_write_buffer_test.go b/go/internal/storage/recall_write_buffer_test.go index a0031d6..7ae763b 100644 --- a/go/internal/storage/recall_write_buffer_test.go +++ b/go/internal/storage/recall_write_buffer_test.go @@ -240,3 +240,31 @@ func TestRecallWriteBufferStopFlushes(t *testing.T) { t.Fatalf("Stop 应落盘最后一个窗口,batchCalls=%d", fake.batchCalls) } } + +// hangingLDB:UpdateRecallBatch 永不返回(模拟 Rust/LanceDB 批量更新在活 IPC +// 流量下被卡死,t_fa797edd 生产实测 80s+)。用于验证 Stop 有界:不会因 flush +// 挂起而无限阻塞进程退出。 +type hangingLDB struct{ fakeLDB } + +func (h *hangingLDB) UpdateRecallBatch(table string, items []RecallWriteItem, lastRecalledAt string) (int64, error) { + select {} // 永不返回 +} + +// t_fa797edd 反向用例:最终落盘挂起时,Stop 必须在有界时间内返回(不许无限阻塞)。 +// 原实现 b.Flush() 同步调用 → Rust 卡死则 Stop 永不返回 → 进程到不了 srv.Shutdown +// → 跑满 systemd 90s 被 SIGKILL。修复后 Stop 在 defaultRecallStopFlushWait 内放行。 +func TestRecallWriteBufferStopBoundedWhenFlushHangs(t *testing.T) { + h := &hangingLDB{} + b := NewRecallWriteBuffer(h, 0, 1000) + b.Record([]string{"mem_hang"}) + + start := time.Now() + b.Stop() + elapsed := time.Since(start) + + // 有界上限:后台协程等待(5s) + 最终落盘等待(8s) + 裕量。50s 远小于 systemd 90s 超时。 + if elapsed > 50*time.Second { + t.Fatalf("Stop 应是有界的,实际阻塞了 %s(>50s,会触发 systemd 90s SIGKILL)", elapsed) + } + t.Logf("Stop 在 %s 内放行(flush 挂起时也不阻塞进程退出)", elapsed) +}