Skip to content

fix: handle 404 in waitForTaskActive when task is deleted by concurrent run#13

Open
johnstcn wants to merge 5 commits intomainfrom
fix/handle-404-in-waitForTaskActive
Open

fix: handle 404 in waitForTaskActive when task is deleted by concurrent run#13
johnstcn wants to merge 5 commits intomainfrom
fix/handle-404-in-waitForTaskActive

Conversation

@johnstcn
Copy link
Copy Markdown
Member

@johnstcn johnstcn commented Mar 26, 2026

Depends upon #14

Problem: tasks can be deleted due to github action concurrency. Action wasn't handling 404s properly.

Solution:

  • Add TaskDeletedError class for explicit error discrimination
  • Catch 404 in waitForTaskActive polling loop, throw TaskDeletedError
  • Wrap existing-task path in run() with try-catch; on TaskDeletedError, fall through to create a new task
  • Tests for both client-level 404 handling and action-level fallthrough

Closes #12

🤖 Created by Coder Agents, reviewed by me.

Copilot AI review requested due to automatic review settings March 26, 2026 09:33
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a race condition where a task can be deleted by a concurrent workflow run while waitForTaskActive is polling, causing an unhandled 404 and action failure. It introduces an explicit error type so the action can detect the condition and fall back to creating a new task.

Changes:

  • Add TaskDeletedError and throw it from waitForTaskActive when getTaskById returns a 404 during polling.
  • Update the existing-task path in CoderTaskAction.run() to catch TaskDeletedError and proceed with creating a new task.
  • Add unit tests covering both the client-side 404 handling and the action-level fallback behavior.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/coder-client.ts Converts polling-time 404s into a dedicated TaskDeletedError for explicit handling.
src/coder-client.test.ts Adds coverage ensuring polling throws TaskDeletedError when the task disappears mid-loop.
src/action.ts Catches TaskDeletedError on the existing-task path and falls through to task creation.
src/action.test.ts Adds a test verifying the action creates a new task when waitForTaskActive fails with TaskDeletedError.
dist/index.js Updates bundled output to include the new logic (but also includes unrelated bundler-helper changes).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@johnstcn johnstcn self-assigned this Mar 26, 2026
@johnstcn johnstcn marked this pull request as draft March 26, 2026 11:02
@johnstcn johnstcn force-pushed the fix/handle-404-in-waitForTaskActive branch from 2d50ca8 to cc40f97 Compare March 26, 2026 12:20
The CI workflow used bun-version: latest, which recently jumped to
1.3.11. Bun 1.3.10 changed the bundler preamble (WeakMap caching,
__accessProp, __exportSetter), so dist/index.js built locally with
1.2.x no longer matches what CI produces — instant check_unstaged
failure for anyone not running bleeding-edge bun.

ci: rebuild dist/index.js with bun 1.2.15
…nt run

When a concurrent workflow run deletes a task while waitForTaskActive
is polling getTaskById, the action now catches the 404 and falls
through to create a new task instead of crashing.

- Add TaskDeletedError class for explicit error discrimination
- Catch 404 in waitForTaskActive polling loop, throw TaskDeletedError
- Wrap existing-task path in action.ts run() with try-catch to
  handle TaskDeletedError and fall through to task creation

Closes #12
@johnstcn johnstcn force-pushed the fix/handle-404-in-waitForTaskActive branch from cc40f97 to 1ce13ac Compare March 26, 2026 12:25
@johnstcn johnstcn changed the base branch from main to ci/pin-bun-version March 26, 2026 12:25
@johnstcn johnstcn changed the title [DNM] fix: handle 404 in waitForTaskActive when task is deleted by concurrent run fix: handle 404 in waitForTaskActive when task is deleted by concurrent run Mar 26, 2026
@johnstcn johnstcn marked this pull request as ready for review March 26, 2026 12:37
@johnstcn johnstcn requested a review from mafredri March 26, 2026 12:38
Base automatically changed from ci/pin-bun-version to main March 27, 2026 09:38
Copy link
Copy Markdown
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean fix for the concurrent-deletion race. The error discrimination pattern (dedicated error class, instanceof check, fallthrough) is solid and the tests cover the primary scenario well.

A few things to consider: 2 P3s, 2 nits, 1 observation across 5 inline comments.

🤖 This review was automatically generated with Coder Agents.

Copy link
Copy Markdown
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two additional findings from a late-arriving reviewer that weren't in the first pass.

🤖 This review was automatically generated with Coder Agents.

Copy link
Copy Markdown
Member Author

Addressed all review feedback in b7eadbb:

Semantic accuracy (comments 1, 2):

  • Renamed TaskDeletedErrorTaskNotFoundError with message "Task {id} returned 404 during polling"
  • Warning now reads "Lost contact with task '{name}' (404 during polling). Creating a new task."

Robustness (comments 6, 7):

  • Catch block in action.ts now also handles CoderAPIError with statusCode === 404 from sendTaskInput
  • waitForTaskActive now requires 2 consecutive 404s before throwing (tolerates a single transient 404)

Test coverage (comments 3, 4, 5):

  • Fallthrough test now asserts taskUrl contains the new task's ID
  • Added test for re-throw branch (CoderAPIError("Request Timeout", 408) propagates, createTask not called)
  • Added test for non-404 error propagation in waitForTaskActive (500 → CoderAPIError, not TaskNotFoundError)
  • Added test for transient 404 tolerance (single 404 between successful polls → no error)

🤖 This response was generated by Coder Agents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle 404 in waitForTaskActive when task is deleted by concurrent run

3 participants