Stop admitting work before the drain and replay each outbox row once.

The listener closes before Wait, and a shutdown refuses new task operations. Wait also stays until detached runs finish. The activity insert and the outbox delete commit together, and a row that keeps failing is dropped after five attempts so it cannot block the rest.
This commit is contained in:
protonmatter
2026-11-17 15:41:56 -05:00
parent 0234025d28
commit a1c6103cbf
8 changed files with 224 additions and 32 deletions
+19 -2
View File
@@ -46,14 +46,20 @@ func (e *Engine) inflightCounter(taskID string) *int64 {
}
// beginTaskOperation atomically registers a task-owned operation unless deletion
// has already installed its barrier. The delete handler can therefore wait for
// inflight==0 without a check-then-start race recreating files after cleanup.
// has already installed its barrier, or the engine is draining for shutdown.
// The delete handler and Wait can therefore wait for inflight==0 without a
// check-then-start race admitting work that outlives the drain.
func (e *Engine) beginTaskOperation(taskID string) bool {
e.deleteMu.RLock()
defer e.deleteMu.RUnlock()
if e.IsDeleting(taskID) {
return false
}
e.runtimeMu.Lock()
defer e.runtimeMu.Unlock()
if e.draining {
return false
}
atomic.AddInt64(e.inflightCounter(taskID), 1)
return true
}
@@ -63,6 +69,17 @@ func (e *Engine) inflightCount(taskID string) int64 {
return atomic.LoadInt64(e.inflightCounter(taskID))
}
// inflightTotal is the sum of every task's admitted operations, including
// detached chat runs that are not members of a task WaitGroup.
func (e *Engine) inflightTotal() int64 {
var n int64
e.inflight.Range(func(_, v any) bool {
n += atomic.LoadInt64(v.(*int64))
return true
})
return n
}
// ---------- deadline ----------
// taskDeadline returns the task's absolute deadline (unix). Prefers the in-process