fix(tasks): fail tasks whose running instance stopped renewing its lease #292
All Vouch review confidence checks passed
Residual risk 0.08 = Change Risk (0.56) × (1 − Review Confidence (0.86)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.56 · 655 lines diff across 11 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.82 · 2 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Real PR Conversation Timeline
4 items
failIfOwnerLost() checks local Date.now() before calling Redis’s authoritative expiry check. A reader five minutes behind Redis delays failure by five minutes, leaving tasks/result blocked. we should delegate expiry checking to the store, and have a slow-reader-clock regression test.
Removed the local Date.now() pre-check in 0d78c43: failIfOwnerLost() now skips the store only for terminal tasks or tasks with no leaseExpiresAt, and otherwise lets failExpiredLease decide (Redis server TIME for RedisTaskStore). Added a two-instance Redis test where the reader's Date.now runs five minutes behind; tasks/result and tasks/get still fail the orphaned tasks promptly (it timed out before the fix).
Approved
Approved these changes.
rozzilla
mcollina
Problem
Task-augmented
tools/calltasks recorded no lease or heartbeat. If the instance running a task died or closed, nothing noticed.tasks/getkept reportingworking, andtasks/resultblocked until the task's ttl ran out (an hour by default) and then answered "not found".Fix
leaseExpiresAton the task record, which is not sent over the wire). It renews the lease every third oftaskLeaseMswith an unref'd interval and stops when the task finishes or the app closes (TaskLeases, closed in theonClosehook).tasks/get,tasks/resultand thetasks/resultpoll loop ask the store (failExpiredLease) whether the lease has lapsed. The handler never compares the lease against its own clock, so a reader whose clock runs behind cannot delay the failure. When the lease has lapsed, the store marks the taskfailedwith the status message "Task owner lost" and the handler wakes any local waiters.TaskStoremethodsrenewLease/failExpiredLease, implemented in both stores as compare-and-set operations:-
MemoryTaskStore: the check and the write happen with no intervening await.-
RedisTaskStore: Lua scripts withKEEPTTL. Both the renewal and the expiry check use RedisTIME, so clock skew between instances cannot fail a task that is still running.taskLeaseMs(default 30000), documented in the README.A custom
TaskStorethat does not implement the two new methods keeps working as before, without leases.Tests
- a lapsed lease is failed
- a renewed lease is not failed
- tasks with no lease, terminal tasks and missing tasks are left alone
- retention is preserved
TaskLeasesrenews until stopped or closed.taskLeaseMskeeps its lease and completes.tasks/result, and A closes. B'stasks/resultreturns promptly with "Task owner lost", andtasks/getreportsfailed.tasks/getandtasks/result.Date.nowrunning five minutes behind Redis:tasks/resultandtasks/getstill fail the orphaned tasks promptly.This change touches
handleTasksGet/handleTasksResult/awaitTaskTerminalinhandlers.ts, adding one line at each read. Expect a trivial conflict with any paralleltasks/resultlifecycle work.Fixes #250
Orphaned tasks also stop holding the caller's quota: when a create hits
taskMaxRetainedPerPrincipal, tasks whose lease has lapsed are failed first and can then be evicted like any finished task. Before this, a caller whose instances all died could get "Task limit reached" until they polled every orphan or its ttl ran out.🤖 Generated with Claude Code
https://claude.ai/code/session_01UF4S3w7iA83V9DVsasLJzM