Skip to content

fix(taskservice): fence stale SQL task completions - #25662

Merged
XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
LeftHandCold:agent/fence-stale-sql-task-completion
Jul 13, 2026
Merged

XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
LeftHandCold:agent/fence-stale-sql-task-completion

Conversation

@LeftHandCold

Copy link
Copy Markdown
Collaborator

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #25604

What this PR does / why we need it:

A timed-out SQL task run can be reaped while its original worker is still finishing. The old completion path unconditionally updated the row by run_id, allowing the late worker to overwrite the terminal TIMEOUT state with SUCCESS or FAILED.

This change makes completion a fenced state transition. MySQL only updates a row that is still RUNNING and still belongs to the same runner and attempt; the memory storage follows the same contract. Reaper updates remain unconditional so they can continue to mark stale runs atomically under the existing task lock.

The shared storage regression test reaps a stale run, verifies that its late completion affects zero rows and preserves TIMEOUT, then verifies that the current attempt still completes normally.

Tested with:

go test -count=1 ./pkg/taskservice

go test -race -count=10 ./pkg/taskservice -run '^(TestCompleteSQLTaskRunRejectsReapedRun|TestAcquireAndCompleteSQLTaskRun|TestAcquireAndCompleteSQLTaskRunInSqlMock|TestSQLTaskMySQLStorageErrorBranches)$'

go vet ./pkg/taskservice

@mergify mergify Bot added kind/bug Something isn't working kind/test-ci labels Jul 13, 2026
@matrix-meow matrix-meow added the size/S Denotes a PR that changes [10,99] lines label Jul 13, 2026
@LeftHandCold
LeftHandCold marked this pull request as ready for review July 13, 2026 10:02
@LeftHandCold
LeftHandCold requested a review from XuPeng-SH as a code owner July 13, 2026 10:02
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM after reviewing this from the state-machine, concurrency, and unhappy-path angles.

  • correctness: completion is now fenced to the same RUNNING row plus the same runner/attempt, so a late stale worker can no longer overwrite a reaped terminal state
  • symmetry: MySQL and memory storage now follow the same completion contract, while reaper updates remain intentionally unconditional under the existing task lock
  • unhappy path coverage: the new regression proves stale completion preserves TIMEOUT and does not block the current attempt from completing normally

I did not find a remaining correctness, hang, or state-corruption issue in the latest diff.

@mergify mergify Bot added the queued label Jul 13, 2026
@mergify

mergify Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-07-13 10:34 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-07-13 10:39 UTC · at 9ec66ae926fcc9cbf53558a8de05bce7a13f8998

This pull request spent 5 minutes 38 seconds in the queue, with no time running CI.

Reason

The pull request can't be updated

This pull request seems to come from a fork, and Mergify needs the author's permission to update its branch.
The author needs to enable "Allow edits from maintainers" on this pull request, or update the branch manually.

Hint

You should update or rebase your pull request manually. If you do, this pull request will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

@XuPeng-SH
XuPeng-SH merged commit 00d53bb into matrixorigin:main Jul 13, 2026
23 of 24 checks passed
@mergify mergify Bot added dequeued and removed queued labels Jul 13, 2026
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [x] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#25604

## What this PR does / why we need it:

A timed-out SQL task run can be reaped while its original worker is
still finishing. The old completion path unconditionally updated the row
by `run_id`, allowing the late worker to overwrite the terminal
`TIMEOUT` state with `SUCCESS` or `FAILED`.

This change makes completion a fenced state transition. MySQL only
updates a row that is still `RUNNING` and still belongs to the same
runner and attempt; the memory storage follows the same contract. Reaper
updates remain unconditional so they can continue to mark stale runs
atomically under the existing task lock.

The shared storage regression test reaps a stale run, verifies that its
late completion affects zero rows and preserves `TIMEOUT`, then verifies
that the current attempt still completes normally.

Tested with:

`go test -count=1 ./pkg/taskservice`

`go test -race -count=10 ./pkg/taskservice -run
'^(TestCompleteSQLTaskRunRejectsReapedRun|TestAcquireAndCompleteSQLTaskRun|TestAcquireAndCompleteSQLTaskRunInSqlMock|TestSQLTaskMySQLStorageErrorBranches)$'`

`go vet ./pkg/taskservice`

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [x] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#25604

## What this PR does / why we need it:

A timed-out SQL task run can be reaped while its original worker is
still finishing. The old completion path unconditionally updated the row
by `run_id`, allowing the late worker to overwrite the terminal
`TIMEOUT` state with `SUCCESS` or `FAILED`.

This change makes completion a fenced state transition. MySQL only
updates a row that is still `RUNNING` and still belongs to the same
runner and attempt; the memory storage follows the same contract. Reaper
updates remain unconditional so they can continue to mark stale runs
atomically under the existing task lock.

The shared storage regression test reaps a stale run, verifies that its
late completion affects zero rows and preserves `TIMEOUT`, then verifies
that the current attempt still completes normally.

Tested with:

`go test -count=1 ./pkg/taskservice`

`go test -race -count=10 ./pkg/taskservice -run
'^(TestCompleteSQLTaskRunRejectsReapedRun|TestAcquireAndCompleteSQLTaskRun|TestAcquireAndCompleteSQLTaskRunInSqlMock|TestSQLTaskMySQLStorageErrorBranches)$'`

`go vet ./pkg/taskservice`

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [x] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#25604

## What this PR does / why we need it:

A timed-out SQL task run can be reaped while its original worker is
still finishing. The old completion path unconditionally updated the row
by `run_id`, allowing the late worker to overwrite the terminal
`TIMEOUT` state with `SUCCESS` or `FAILED`.

This change makes completion a fenced state transition. MySQL only
updates a row that is still `RUNNING` and still belongs to the same
runner and attempt; the memory storage follows the same contract. Reaper
updates remain unconditional so they can continue to mark stale runs
atomically under the existing task lock.

The shared storage regression test reaps a stale run, verifies that its
late completion affects zero rows and preserves `TIMEOUT`, then verifies
that the current attempt still completes normally.

Tested with:

`go test -count=1 ./pkg/taskservice`

`go test -race -count=10 ./pkg/taskservice -run
'^(TestCompleteSQLTaskRunRejectsReapedRun|TestAcquireAndCompleteSQLTaskRun|TestAcquireAndCompleteSQLTaskRunInSqlMock|TestSQLTaskMySQLStorageErrorBranches)$'`

`go vet ./pkg/taskservice`

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

This branch was previously deployed

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

Labels

kind/bug Something isn't working kind/test-ci size/S Denotes a PR that changes [10,99] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants