nango icon indicating copy to clipboard operation
nango copied to clipboard

Synchronous action error replaced by "Task <id> is in progress": FAILED callback fires before commit

Open VivekMalipatel opened this issue 1 month ago • 2 comments

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

  1. Scheduler.fail() (packages/scheduler/lib/scheduler.ts) calls this.onCallbacks[task.state](task) inside this.db.transaction(...), i.e. before COMMIT. Scheduler.succeed() calls it after the transaction resolves.
  2. The orchestrator's FAILED callback (packages/orchestrator/lib/events.ts) emits task:completed:<taskId> with pg_notify on the dedicated LISTEN connection, which is delivered right away.
  3. The GET /v1/tasks/:taskId/output long poll (packages/orchestrator/lib/routes/v1/tasks/taskId/getOutput.ts) wakes up on that notification, reads the task with scheduler.get() from another pool connection, and answers with whatever is committed at that moment: still STARTED, output: null.
  4. OrchestratorClient.immediateAndWait() (packages/orchestrator/lib/clients/client.ts) only retries error responses, so STARTED becomes task_in_progress_error ("Task is in progress").
  5. triggerAction (packages/shared/lib/clients/orchestrator.ts) cannot deserialize a NangoError from a null payload, so it wraps the result as action_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 FAILED callback after the transaction commits, the same way succeed() does.
  • Defense in depth: immediateAndWait re-polls while the state is CREATED or STARTED and retryUntil has not passed, so an early wake-up can no longer surface as task_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 FAILED commit open for 0.5 s, and the FAILED callback reads the task from another pool connection, as getOutput does. On master it reads STARTED; with the patch it reads FAILED.
  • Client unit test: the output endpoint answers STARTED, then FAILED; executeAction must return task_failed_error carrying the action's error payload.

VivekMalipatel avatar Oct 30 '26 16:10 VivekMalipatel

NAN-7929

linear-code[bot] avatar Oct 30 '26 16:10 linear-code[bot]

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:

  1. ExpiringDaemon.run() also invokes onExpiring inside its transaction. Does the new re-polling logic fully cover this path? Would an expiration-specific regression test be worthwhile?

  2. The proposed immediateAndWait retry predicate appears to retry all error responses until retryUntil. Should permanent errors such as task_not_found be returned immediately, while only transient errors are retried?

  3. 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!

Shyam525 avatar Oct 31 '26 10:10 Shyam525