Skip to content

Let restart tolerate a run closing its status file - #4545

Open
yohamta0 wants to merge 6 commits into
mainfrom
restart-waits-on-run-liveness
Open

yohamta0 wants to merge 6 commits into
mainfrom
restart-waits-on-run-liveness

Conversation

@yohamta0

@yohamta0 yohamta0 commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Summary

On Windows, dagu restart can fail right after it stops the run with failed to stop running DAG: failed to get current status: failed to find status by dag-run ID: no status data. The stop loop re-reads the run's status file until the status is no longer running. A run replaces that file when it compacts it, and it always compacts it on close. A read during the replace on Windows finds no file, so the run's newest attempt looks empty. If the run has no earlier attempt, the read fails. If it has one, the read returns that attempt's old status. CI run 47035121113 hit this in TestProjectSettingsSkipWorkspaceLabel/ProjectsOff.

Changes

  • Restart follows the attempt it stopped. A status counts only when it belongs to that attempt.
  • When a local attempt's own status cannot be read, the attempt counts as running while its own process record is fresh or its process still lives (saved PID and start time). This is the same check the stale-run repair uses. A newer retry's record does not count.
  • Restart sends the stop once, right after the attempt's own status read as running. It sends it again only when the stop did not reach the run, which happens when the status file went missing and the socket did not answer. A stop goes by run ID, so each extra one could reach an earlier attempt instead.
  • A run on a worker is handled as before.
  • Before: restart could fail on Windows with no status data after the run aborted. After: restart waits until the attempt releases its process record, then starts the new run.

Core change

A run releases its process record only after it closes its status file: withPreparedLocalExecution stops the proc handle after agent.Run returns. So while the stopped attempt's status cannot be read, a fresh record for that attempt means it is alive, and a released record means the file is closed. The saved PID covers a live run whose heartbeat went stale, as repairStaleLocalRunIfDead does.

internal/cmd/restart.go:348, internal/cmd/restart.go:395

if readOwn && !stopSent {
	err := cli.Stop(ctx, dag, running.DAGRunID)
	switch {
	case err == nil:
		stopSent = true
	case local && errors.Is(err, dagrun.ErrNoStatusData):
		// sent again once the attempt's status reads again
	default:
		return err
	}
}
...
func isLocalAttemptRunning(ctx context.Context, cli runtime.Manager, procs attemptLiveness, dag *ir.DAG, attempt *ir.DAGRunStatus) (running, readOwn bool, err error) {
	dagStatus, err := cli.GetCurrentStatus(ctx, dag, attempt.DAGRunID)
	if err == nil && dagStatus.AttemptID == attempt.AttemptID {
		return dagStatus.Status == ir.Running, true, nil
	}
	alive, aliveErr := procs.IsAttemptAlive(ctx, dag.ProcGroup(), attempt.DAGRun(), attempt.AttemptID)
	...
	if alive {
		return true, false, nil
	}
	matched, _, _ := procutil.MatchesStartTime(int(attempt.PID), attempt.PIDStartedAt)
	return matched, false, nil
}

Testing

  • TestStopDAGIfRunningUnreadableStatus fails on main with the same error as CI. The run answers Running on its socket until the stop arrives. Then it leaves the socket and releases its process record while its attempt has no status file.
  • TestStopDAGIfRunningHeldRecord removes the status file of a running attempt that holds a fresh process record. It runs once with no earlier attempt and once with an earlier attempt that failed with retries left. Restart must keep waiting until the record is released, and the earlier attempt must stay failed.
  • TestStopDAGIfRunningStaleHeartbeat covers a live run with no socket and a stale record, known only by its PID. Restart must ask it to stop, keep waiting while its status file is missing, and return once it reads as aborted.
  • TestStopDAGIfRunningNewerRetry starts a retry after the stopped attempt ends. Restart must return once the stopped attempt releases its record.
  • Each new case was checked to fail with its guard removed. go test ./internal/cmd/ passes. -race -count=3 on the restart tests passes.

Performance

Low risk. The loop keeps its 100 ms interval and runs only while one restart waits for one run to stop. Only while the stopped attempt's own status cannot be read does each check list the process records of the DAG's process group, one directory read. That lasts for the length of a status file replace, or until the attempt releases its record.

Out of scope

  • A stop goes through runtime.Manager.Stop by run ID. If the attempt's status file goes missing in the moment between restart's read and its one stop, the stop can still act on an earlier attempt. main has the same window. Closing it needs an attempt-scoped stop on the exported Manager API.

@yohamta0 yohamta0 added the windows-ci Run the Windows tests on this pull request, without conformance and E2E label Dec 3, 2026
@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Stop handling now distinguishes local runs from non-local runs when checking whether a DAG remains active. Local runs use cli.IsRunning; non-local runs use saved status. A new test covers a local run with no status file.

Changes: DAG stop handling

Layer / File(s) Summary
Run classification and stop-status checks
internal/cmd/restart.go, internal/cmd/restart_internal_test.go
stopDAGIfRunning identifies local runs and passes that classification to the polling logic. Local runs use cli.IsRunning; non-local runs read saved status. The test covers a local run with no status file.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: Minimal · up to a8f17

dagu restart now waits on local run liveness instead of repeatedly reading status, which avoids the transient "no status data" failure on Windows. Worker runs still follow saved status as before. I found no merge-blocking risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed The title "Let restart tolerate a run closing its status file" matches the main change. The PR changes how dagu restart waits for a stopped run. For local runs, the stop loop checks liveness through I…
Description check Passed The description follows the repository template closely. It has a Summary that states the Windows failure and its cause, a Changes list with before and after behavior, a Core change section with the m…
Full details: Title check

Explanation

The title "Let restart tolerate a run closing its status file" matches the main change. The PR changes how dagu restart waits for a stopped run. For local runs, the stop loop checks liveness through IsRunning instead of repeatedly reading the status file, so an unreadable status file during replacement no longer fails the restart. The title names the behavior from the user's perspective and does not list files. It is specific enough for a teammate scanning history to identify the primary change.

Full details: Description check

Explanation

The description follows the repository template closely. It has a Summary that states the Windows failure and its cause, a Changes list with before and after behavior, a Core change section with the mechanism and a code excerpt that gives file and line references, a Testing section listing the new and updated tests, and a Performance section with a risk assessment. It also includes an Out of scope section. The Checklist is missing from the description, and no Related Issues section is present. The Testing section does not say which check items were completed. The Compatibility section is not included, though the change does not appear to alter persisted data, API, CLI, or YAML, so it may not apply. Overall, the description is mostly complete and specific.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 600b3685-8e03-4445-ad75-1f6b40595693
Commits

Reviewing files that changed from the base of the PR and between d48aa99 and a8f17c9.

Files selected for processing: 2

  • internal/cmd/restart.go
  • internal/cmd/restart_internal_test.go

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

@greptile-apps

greptile-apps Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Confidence Score: 4/5RetriggerView in Greptile

[Medium impact] The effect stays within restart’s wait for an existing run.

Fix the skipped stop for live legacy runs before merging.

Findings

  1. P1 Old run keeps running ▶

Summary

Restart now checks local run liveness while waiting for a stop. Worker runs still use their saved status.

  • Restart waits for local runs without relying on every status-file read.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Read running status] --> B{Local run?}
  B -->|Yes: changed| C[Check IsRunning]
  B -->|No| D[Read current status]
  C --> E{Still running?}
  D --> E
  E -->|Yes| F[Send stop]
  F --> G[Wait 100 ms]
  G --> B
  E -->|No| H[Start replacement run]
Loading

Reviews (1) · Last reviewed commit: "Wait on local run liveness when restart ..." · Reviewed by Greptile

Comment thread internal/cmd/restart.go Outdated
Comment on lines +351 to +352
if local {
return cli.IsRunning(ctx, dag, dagRunID), nil

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Old run keeps running

If a run started by an older binary still answers on its legacy socket but its heartbeat has gone stale, GetCurrentStatus reports Running while IsRunning returns false. The former checks legacy socket addresses; the latter checks only the current address unless the process record is fresh. Heartbeat write failures can leave this state while the old run continues.

This makes the loop exit before calling Stop. Restart then launches another copy without even writing the old run's abort flag. Preserve the legacy-aware check, or distinguish an uncertain liveness result from a confirmed stop.

@yohamta0 yohamta0 changed the title Wait on local run liveness when restart stops it Read status while restart stops a run, and tolerate a closing one Dec 3, 2026
@yohamta0 yohamta0 changed the title Read status while restart stops a run, and tolerate a closing one Let restart tolerate a run closing its status file Dec 3, 2026
@yohamta0
yohamta0 force-pushed the restart-waits-on-run-liveness branch 2 times, most recently from 353dca7 to b0f8e53 Compare December 3, 2026 05:28
Restart stopped a run by reading its status file until the status was
no longer running. A local run saves its final status and leaves its
socket before it closes the status file, and the close replaces the
file. On Windows a read at that moment finds no status, so restart
failed with "no status data" right after the run aborted.

For a local run, the stop loop now asks Manager.IsRunning, which counts
the run as running only while its socket answers or its process record
is fresh with a running status. A run on a worker is still followed
through its status.
Following only Manager.IsRunning missed a live run that reads as
running through its process identity alone, with no socket and a stale
process record: restart skipped the stop and started the new run next
to it.

The stop loop reads the run's status again, and only a local run whose
status cannot be read counts as stopped once IsRunning says so. The
loop also sends the stop before its first check, so a run that read as
running always gets one.
IsRunning counted a local run as stopped when its status could not be
read, even while its process record was fresh. A running run that
compacts its status file on Windows can then look stopped, and restart
started the new run beside it. A stop sent while the status file was
missing also failed restart outright.

While a local run's status cannot be read, restart now asks whether its
process record is fresh. The run releases the record only after it
closes its status file. A stop that fails because the status is missing
no longer ends restart; the next check decides.
A retry's missing status file made the run read as its earlier,
finished attempt, which restart took as stopped while the retry still
held its process record. A run known only by its PID was also taken as
stopped once its status file went missing.

Restart now follows the attempt it stopped: a status counts only when
it is that attempt's. Otherwise the attempt runs while its process
record is fresh or its process still lives, as the stale-run repair
decides. The stop's error check is also written the way lint asks.
A stop goes by run ID and acts on the run's latest status. While a
retry's status file was missing, restart kept sending stops, and one
could find the earlier attempt failed with retries left and abort it.

Restart now sends a stop only after the attempt it stopped read as
running, and otherwise only waits. The process record check is behind
a small interface so a test can count the checks.
@yohamta0
yohamta0 force-pushed the restart-waits-on-run-liveness branch from 50db333 to bb9b017 Compare December 3, 2026 05:55
@yohamta0 yohamta0 added perf:disk-io May read run or DAG files on a request or tick path perf:cpu May add a ticker, watcher, scan, or hot-path work, or idle cost labels Dec 3, 2026
Each stop restart sent looked up the run's latest attempt again, so a
status file that went missing between a read and a stop could let the
stop abort an earlier failed attempt. The process record check also
accepted any attempt of the run, so a retry started after the stopped
attempt ended kept restart waiting on a run it never stopped.

Restart now sends the stop once, and again only when it did not reach
the run, and checks the process record of the stopped attempt alone.

This branch has not been deployed

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

Labels

perf:cpu May add a ticker, watcher, scan, or hot-path work, or idle cost perf:disk-io May read run or DAG files on a request or tick path windows-ci Run the Windows tests on this pull request, without conformance and E2E

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant