Skip to content

Let exec steps start a helper that lives until the run ends - #169

Merged
bitomule merged 1 commit into
mainfrom
worktree-t-mav-149-exec-helper-process
Dec 3, 2026
Merged

bitomule merged 1 commit into
mainfrom
worktree-t-mav-149-exec-helper-process

Conversation

@bitomule

@bitomule bitomule commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Closes #149.

What changes

A flow can now start a helper process that stays up across later steps, and MAV owns its whole lifetime:

- exec:
    cmd: "mitmdump -p 8080 --server-replay mocks/search.flows"
    background: true
    name: mock
    port: 8080        # or `contains: "listening"`; waits until ready
    timeout: 20s      # readiness timeout (default 30s)
- open: {}
- tap: { id: SearchField }
- exec.stop: mock   # optional; `exec.stop: { name: mock, timeout: 5s }`
  • Start: the helper runs in a process group of its own, with stdout/stderr written straight to exec_NN_<name>.out/.err in the run directory (no pipes MAV has to drain). contains waits for text in its output, port for a TCP address that accepts. A helper that exits before it is ready (exec_background_exited, with its exit code) or never gets ready (exec_background_not_ready) fails the step and is stopped before the step returns.
  • Across steps: ${exec.mock.pid}, ${exec.mock.stdout}, ${exec.mock.stderr} are bound for later steps.
  • Stop: exec.stop sends SIGTERM to the whole group (so a wrapper's nohup … & children go too), then SIGKILL after the grace period. Stopping a helper that already died on its own fails with exec_background_exited, since the steps since then ran without it.
  • Always stopped: runFlow defers a cleanup that stops every helper still running, newest first, when the run passes, fails, or is interrupted (SIGINT/SIGTERM cancel the run context). Each is recorded in commands.jsonl as mav exec.stop <name> (run end).
  • MAV killed with SIGKILL: a small watchdog process, in its own process group, holds the read end of a pipe only MAV writes to and, on EOF, sends SIGTERM and then SIGKILL to the helper's group, the same death-pipe idea Stop an exec step's shell from outliving the mav that started it #121 uses for exec steps. It lives outside the helper's group so the group empties when the helper dies, which is how an early exit is detected.

Plain exec steps are unchanged: they still reap their whole group when the step ends (#121).

For #150 (network mocking)

The lifecycle is a small Go API in internal/mav/exec_background.go, not just a YAML feature: a network.mock step can build a backgroundSpec (command, log paths, ReadyAddr/ReadyContains) and call c.background.Start(ctx, spec). It gets the same readiness wait, the same graceful SIGTERM stop (which lets mitmdump flush a recording), and the same run-end, failure and SIGKILL cleanup with no extra code.

Footprint

cli.go changes by 15 lines: one CLI field, the deferred cleanup in runFlow, and three dispatch lines. Everything else is in the new exec_background.go, plus the payload flag and the exec.stop action in flow.go, two lint checks in flow_lint.go, and docs in README.md and skills/mav/SKILL.md.

Tests

internal/mav/exec_background_test.go:

  • the issue's reproduction (a wrapper that backgrounds the real process with nohup … & and exits) is still alive in later steps, and is gone once the run passes without an exec.stop;
  • exec.stop stops the whole group, and ${exec.NAME.pid} binds;
  • cleanup on failure: a later step failing stops the helper;
  • interrupted: cancelling the run context mid-step stops the helper;
  • MAV SIGKILLed: a real mav run child process is killed and the helper goes with it;
  • readiness by contains and by port; a helper that exits before it is ready fails fast with its exit code; a readiness timeout stops the helper; with no readiness check, a command that cannot start (exit 127) still fails the step;
  • a helper that ignores SIGTERM is SIGKILLed after the grace period;
  • closing the watchdog pipe alone kills the group, and the watchdog is retired once a helper's group empties on its own (so it never signals a reused pgid);
  • step errors (missing name, out with background, unknown name, name already running, helper already exited), allow_shell required, parsing (exec.stop: mock scalar form), and lint.

Proof the tests test the fix

With the deferred run-end cleanup removed from runFlow:

--- FAIL: TestBackgroundExecOutlivesItsStepAndStopsAtRunEnd (10.41s)
    exec_background_test.go:89: run passed without exec.stop: helper pid 78816 is still running
--- FAIL: TestBackgroundExecIsStoppedWhenALaterStepFails (10.05s)
    exec_background_test.go:131: run failed: helper pid 79598 is still running
--- FAIL: TestBackgroundExecIsStoppedWhenTheRunIsInterrupted (10.06s)
    exec_background_test.go:158: run interrupted: helper pid 80095 is still running

With the watchdog not started:

--- FAIL: TestBackgroundExecDoesNotSurviveMavBeingKilled (10.08s)
    exec_background_test.go:189: mav SIGKILLed: helper pid 80989 is still running

With the fix in place, all of them pass, go test -race passes on the new tests, and make check passes.

@bitomule
bitomule force-pushed the worktree-t-mav-149-exec-helper-process branch from 154dd41 to 10bc1c2 Compare December 2, 2026 23:50
@bitomule
bitomule force-pushed the worktree-t-mav-149-exec-helper-process branch 2 times, most recently from c767172 to cc2fb9d Compare December 3, 2026 10:02
A background exec (background: true, name) starts a helper in its own
process group that stays up across later steps. exec.stop stops it, and
the run stops whatever is left when it passes, fails or is interrupted.
A watchdog outside the group stops it if mav is SIGKILLed. contains and
port wait for readiness. The registry is a Go API network mocking (#150)
can start a mock server with.

Closes #149
@bitomule
bitomule force-pushed the worktree-t-mav-149-exec-helper-process branch from cc2fb9d to f20212a Compare December 3, 2026 10:28
@bitomule
bitomule merged commit 5edb027 into main Dec 3, 2026
1 check passed
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.

An exec step cannot start a helper process that outlives the step

1 participant