posthog icon indicating copy to clipboard operation
posthog copied to clipboard

fix(tasks): close TaskRun startup race causing TaskNotFoundError

Open posthog[bot] opened this issue 5 months ago • 0 comments

Problem

A small number of Tasks agent runs fail at startup with a non-retryable TaskNotFoundError, aborting the run before any work begins.

Task.create_and_run starts the process-task Temporal workflow fire-and-forget immediately after TaskRun.objects.create(...). When that path runs inside an enclosing transaction.atomic() block (e.g. the signals auto-start lock), the row isn't committed yet — so the workflow's first activity, get_task_processing_context, can read the TaskRun before it's visible and raise a fatal TaskNotFoundError. Because the error is non-retryable, the workflow never recovers once the row commits a moment later.

Changes

  • Close the race at the source: defer the workflow start in Task.create_and_run to transaction.on_commit(...), so the run is committed before the activity reads it. In autocommit mode on_commit runs the callback immediately, so non-atomic callers are unchanged.
  • Defense in depth: treat a missing TaskRun in get_task_processing_context as a retryable TaskRunNotReadyError instead of the fatal TaskNotFoundError, letting the activity's existing retry policy recover from the transient visibility window. The new error intentionally does not capture to error tracking, since it's an expected transient condition the retry absorbs rather than a fault — this also removes the duplicate error-tracking noise.

The later, genuinely-fatal TaskNotFoundError usages (a Task disappearing mid-workflow) are left as-is.

How did you test this code?

I'm an agent. The local environment had no Python dependencies installed and no network to fetch them, so I could not execute the test suite here. I verified the changes with ruff check/ruff format (clean) and py_compile, and traced every production and test call path to create_and_run to confirm impact.

Test changes:

  • Updated test_get_task_processing_context_task_not_found_is_retryable to assert the missing-run path now raises a retryable TaskRunNotReadyError (non_retryable is False) — catches a regression to the fatal behavior that caused the bug.
  • Wrapped the 9 TestTask.test_create_and_run_* tests that assert the workflow mock fired in self.captureOnCommitCallbacks(execute=True), since the start is now deferred to commit. Other call paths (/run/, /start/, posthog_ai routing, slack followup) go through the synchronous _trigger_task_processing_workflow or pass start_workflow=False, so they're unaffected.

🤖 Agent context

Autonomy: Fully autonomous

Authored by PostHog Code (Claude) from an inbox report flagging two duplicate TaskRun.DoesNotExist error-tracking issues. No human drove the work, so the PR is left unassigned for the owning team to triage.

Approach: the fix has two layers — the on_commit deferral eliminates the race for callers that create the run inside a transaction, and the retryable error is a belt-and-suspenders recovery for any residual transient window. I considered keeping the transient error capturing (as siblings do) but chose not to, since capturing on every retry of an expected-transient condition is exactly the error-tracking noise the report called out.


Created with PostHog Code from an inbox report.

posthog[bot] avatar Jun 24 '26 23:06 posthog[bot]