Synchronous action error replaced by "Task <id> is in progress": FAILED callback fires before commit
Summary
A synchronous action (POST /action/trigger) that throws nango.ActionError intermittently returns HTTP 500 with
{"error":{"message":"The action script failed with an error: {}","code":"action_script_failure","payload":{"error":"Task <taskId> is in progress"}}}
instead of the ActionError. The task itself ends FAILED with the correct serialized ActionError in nango_scheduler.tasks.output; only the synchronous response loses it. Successful actions are not affected.
Seen on self-hosted 0.71.6. The same code path is unchanged on master (ae203a8).
Cause
Scheduler.fail()(packages/scheduler/lib/scheduler.ts) callsthis.onCallbacks[task.state](task)insidethis.db.transaction(...), i.e. before COMMIT.Scheduler.succeed()calls it after the transaction resolves.- The orchestrator's
FAILEDcallback (packages/orchestrator/lib/events.ts) emitstask:completed:<taskId>withpg_notifyon the dedicated LISTEN connection, which is delivered right away. - The
GET /v1/tasks/:taskId/outputlong poll (packages/orchestrator/lib/routes/v1/tasks/taskId/getOutput.ts) wakes up on that notification, reads the task withscheduler.get()from another pool connection, and answers with whatever is committed at that moment: stillSTARTED,output: null. OrchestratorClient.immediateAndWait()(packages/orchestrator/lib/clients/client.ts) only retries error responses, soSTARTEDbecomestask_in_progress_error("Taskis in progress"). triggerAction(packages/shared/lib/clients/orchestrator.ts) cannot deserialize a NangoError from a null payload, so it wraps the result asaction_script_failure, and the message renders as{}.
Whether the read lands before COMMIT depends on commit latency (WAL flush) versus the notify-then-read round trip, so it is intermittent. In our deployment, 3 of 4 failed actions over about 1.5 days returned the in-progress error. It happens for fast failures (under 1 s) and slow ones (about 48 s) alike, because the race is at the moment of failure, not in the long-poll timeout.
The expiring daemon (packages/scheduler/lib/daemons/expiring/expiring.daemon.ts) also calls onExpiring inside its transaction, so an expired synchronous action can surface the same way.
Evidence pattern
One failed action, times relative to the orchestrator's Task failed log line:
| t | component | log |
|---|---|---|
| +0 ms | orchestrator | Task failed: {"id":"<taskId>",...} (FAILED callback, still inside the transaction) |
| +4 ms | server | The action script failed with an error: {}, payload {"error":"Task <taskId> is in progress"} |
| +16 ms | runner | Task <taskId> completed (logged after its update call returns, i.e. after COMMIT) |
Afterwards the tasks row is FAILED and output holds the serialized ActionError.
Proposed fix
- Fire the
FAILEDcallback after the transaction commits, the same waysucceed()does. - Defense in depth:
immediateAndWaitre-polls while the state isCREATEDorSTARTEDandretryUntilhas not passed, so an early wake-up can no longer surface astask_in_progress_error.
A patch with regression tests is on a fork branch: https://github.com/NangoHQ/nango/compare/master...VivekMalipatel:nango:fix/orchestrator-fail-callback-after-commit
- Scheduler integration test: a deferred constraint trigger holds the
FAILEDcommit open for 0.5 s, and theFAILEDcallback reads the task from another pool connection, asgetOutputdoes. Onmasterit readsSTARTED; with the patch it readsFAILED. - Client unit test: the output endpoint answers
STARTED, thenFAILED;executeActionmust returntask_failed_errorcarrying the action's error payload.
Thanks for the detailed diagnosis. I've been studying the execution flow, and the transaction-ordering issue makes sense.
The key problem appears to be that Scheduler.fail() publishes the FAILED callback before its database transaction commits. Since the completion notification is sent through the event emitter's separate PostgreSQL connection, getOutput can wake up and read the previously committed STARTED state with output: null. This explains why the synchronous caller receives task_in_progress_error even though the task eventually reaches FAILED with the correct serialized ActionError.
I have a few questions while reviewing the proposed fix:
-
ExpiringDaemon.run()also invokesonExpiringinside its transaction. Does the new re-polling logic fully cover this path? Would an expiration-specific regression test be worthwhile? -
The proposed
immediateAndWaitretry predicate appears to retry all error responses untilretryUntil. Should permanent errors such astask_not_foundbe returned immediately, while only transient errors are retried? -
Would it be useful to test the notification-before-commit scenario for both FAILED and EXPIRED transitions?
I'm interested in independently validating these edge cases and understanding the intended fix.
I also noticed that the repository's CONTRIBUTING.md currently excludes external bug-fix PRs. Is there an approved way for an external contributor to help with reproduction or technical review on this issue?
Thanks!