Skip to content

A stopped run is not a passing run, and the exit code will not say which (#103) - #115

Merged
feitreim merged 1 commit into
mainfrom
modal-run-completion-sentinel
Jul 29, 2026
Merged

feitreim merged 1 commit into
mainfrom
modal-run-completion-sentinel

Conversation

@feitreim

Copy link
Copy Markdown
Owner

Closes #103.

scripts/modal-run decided whether a run passed by asking modal run. That is
not a question the client can answer, and step 1 of the issue was to find out
what it answers instead.

1. What modal run actually returns when its app is stopped

A stall entry point (CPU-only, _run(["sleep", …])) plus modal app stop -y
from a second shell. Bare modal on purpose — the CLI's own behaviour is what
is under test, and this is the one place the wrapper is not the right tool.
Client 1.5.1, macOS, three runs.

Stopped while the function body is running — twice, independently:

$ sleep 180  (cwd=/)
Stopping app - user stopped from CLI.
  (14.1s)
Runner terminated.
╭───────────────────── Traceback (most recent call last) ──────────────────────╮
│ /…/modal/_utils/function_utils.py:550 in _process_result                     │
│ ❱ 550 │   │   raise RemoteError(result.exception)                            │
╰──────────────────────────────────────────────────────────────────────────────╯
RemoteError
BARE_EXIT=1

Stopped between app creation and the function call — the stop issued from
the same script so it wins the race against container start:

✓ Initialized. View run at https://modal.com/apps/feitreim/main/ap-rJ5pfTFLLlGQYthRWiDeQH
STOPPING ap-rJ5pfTFLLlGQYthRWiDeQH at 23:39:27
Stopping app - user stopped from CLI.
✓ Created objects.
…
[modal-client] 2026-07-28T23:39:42-0400 Loop attempt for _run_app.<locals>.heartbeat failed
modal.exception.ConflictError: App state is APP_STATE_STOPPED

That one never exits. It repeated that heartbeat every 15 s for 30
consecutive attempts (7½ minutes) before I killed it; the app was stopped with
0 tasks throughout, so nothing was billing — the client was simply waiting for a
result that was never coming.

So: I could not reproduce exit 0. The issue's observation stands as a
report; my two mid-function stops both gave 1. Stated plainly, because it
matters: the exact symptom #103 was opened on is unconfirmed on this client
version, and if the report was a misattribution — an agent stopping a
different duplicate app than the one its wrapper was running, which is exactly
the situation described — then the wrapper exited 0 correctly that day.

What the evidence does establish is the thing the fix rests on: the client's
exit code is not a completion signal.
One question, three answers — 1, an
indefinite hang, and a field report of 0 — none documented, all incidental to
which internal path the client happens to take. A gate cannot be built on that,
and "it returned 1 the two times I stopped it in the middle" is not a guarantee
for the eviction, the mid-run spend limit, or the next client release.

2. The fix

modal_app.py grows a sentinel and an eight-line decorator:

COMPLETED = "== modal_app: entry point completed =="

def completes(fn):
    @functools.wraps(fn)
    def wrapped(*args, **kwargs):
        fn(*args, **kwargs)
        print(COMPLETED, flush=True)
    return wrapped

applied under @app.function on all ten entry points, so it runs in the
container and the sentinel arrives on the stream the client is already reading.
An exception skips it, which is the point: only a finished body prints it.

scripts/modal-run returns 0 only having seen that line. The reasoning lives
next to the constant in both files, because the next reader's question is "why
not just trust the exit code" and the answer is the section above.

Exit 4, not an overload of 2 or 3. 2 and 3 are both diagnoses — the
workspace is out of money; this wrapper watched a container wedge and stopped it
itself. 4 is the absence of one: the run ended, it did not fail, and it did not
finish, and the wrapper does not know why. Stopped, evicted, or the client
returning on something other than a result all land here and want the same
response (look at modal app list and the log), which is a different response
than either 2 or 3 asks for.

Order matters too: a run that failed on its own returns its own code, not 4. A
failed run is incomplete by definition and its own code says more.

The duplicated string. scripts/modal-run cannot import COMPLETED —
modal ships as its own venv (~/.local/share/uv/tools/modal/…) and the
wrapper runs under whatever python3 is on PATH, where import modal is a
ModuleNotFoundError. I tried the import first; that is why it is not there. So
the literal is spelled twice, with a comment in each naming the other. It is
safe because it fails closed: change one and every run exits 4 with a message
naming both files (control 4d below), rather than quietly passing.

3. The controls

#99's four, extended in place rather than joined by a fifth — its 4 splits into
the clean finish it always covered plus the three cases it did not. Same
stub-modal-on-PATH harness #99 used, with MODAL_RUN_STARTUP_GRACE=5 MODAL_RUN_SILENCE_GRACE=4:

=== CONTROL 1: container never reaches Python
!! wedged: the run never reached Python in 5s. Stopping it rather than paying
   out the function timeout.
   stopped ap-StuBStuBStuBStuBStuB
exit=3  (expected 3)

=== CONTROL 2: run started, then went silent
!! wedged: the run said nothing for 4s. …
exit=3  (expected 3)

=== CONTROL 3: spend limit
!! the workspace spend limit is exhausted -- this is not your diff.
exit=2  (expected 2)

=== CONTROL 4a: clean finish, sentinel seen
    $ cargo test
    == modal_app: entry point completed ==
exit=0  (expected 0)

=== CONTROL 4b: the run's own exit code passes through
    test foo ... FAILED
exit=7  (expected 7)

=== CONTROL 4c: app stopped, client exits 0 anyway
    Stopping app - user stopped from CLI.
!! the run ended without printing `== modal_app: entry point completed ==`, so it did not
   reach the end of its entry point -- the app was stopped, evicted,
   or the client returned on something other than a result. This is
   NOT a pass. Check `modal app list` and the run's log.
   (If you just renamed `COMPLETED` in modal_app.py, rename it in
   scripts/modal-run too -- they are two copies of one string.)
exit=4  (expected 4)

=== CONTROL 4d: the sentinel drifted -- fails closed
    == modal_app: renamed sentinel ==
exit=4  (expected 4)

4c is the case the issue is named for, and it is the one branch that cannot be
exercised against the real client — precisely because I could not make the real
client exit 0. So it is stubbed, and the two that can be real are run for real:

Real clean finish — scripts/modal-run stall --seconds 5. This also proves
@completes survives Modal's CLI argument parsing (--seconds reaches the
function through functools.wraps), the one thing that could have gone wrong
with a decorator under @app.function:

Stopping app - local entrypoint completed.
  (5.0s)
== modal_app: entry point completed ==
✓ App completed. View run at https://modal.com/apps/feitreim/main/ap-MOoEHblbUGKc3UlY8BmKAc
WRAPPER_EXIT=0

Real stopped run — scripts/modal-run stall --seconds 180, stopped from a
second shell once it was under way:

$ sleep 180  (cwd=/)
>>> second shell: modal app stop -y ap-90H1NGTJvd4pGctrW3bjWI
Stopping app - user stopped from CLI.
Runner terminated.
…
RemoteError
WRAPPER_EXIT=1

Non-zero, no sentinel, no false pass. On this client version it lands on the
run's-own-code branch rather than on 4, which is the honest result and is why
the acceptance criterion is met twice over: the client happened to be non-zero
here, and the sentinel would have caught it had it not been.

stall is committed rather than left in a scratch file, because it is the only
way anyone watches this fire again, and it costs a fractional CPU and a minute.

Scope, and what is not done

Three files: modal_app.py, scripts/modal-run, CI.md. No Rust —
git diff --name-only matches nothing under src/, device-tests/,
examples/, or any manifest, and scripts/fmt --check is clean, so tier 1 is
untouched. No workflow runs a Python linter; py_compile passes on both files.

CI.md §"Running Modal by hand" is now the three ways it costs you money,
with the measurements above, the full exit-code table, and stall.

Not done, and it is the obvious next thing: cuda.yml and gpu.yml still
call modal run directly, so neither this guard nor #99's watchdog covers CI at
all — a wedged CI container still rides its timeout=, and a stopped one is
still whatever the client decides to return. That is #99's unfinished business
rather than this issue's, and it is now written down in CI.md instead of being
implied.

…ich (#103)

`scripts/modal-run` returned whatever `modal run` returned, which is not a
completion signal. Measured on client 1.5.1 against a `stall` entry point and a
`modal app stop` from a second shell: stopping an app mid-function raises
RemoteError and exits 1, stopping one between app creation and the function call
hangs the client on AppHeartbeat indefinitely, and the field report that opened
this issue saw 0. Three answers to one question.

So `modal_app.py` now prints a completion sentinel after each entry point's last
line -- `completes`, one print -- and the wrapper returns 0 only having seen it,
and 4 otherwise. Absence of failure is not evidence of completion.

`stall` is the control, and it is committed rather than scratch: one `$ ` line
then a sleep, so stopping it reproduces the whole thing for a fractional CPU.
@feitreim
feitreim merged commit ae37c57 into main Jul 29, 2026
3 checks passed
feitreim added a commit that referenced this pull request Jul 29, 2026
#118)

* Route CI through scripts/modal-run, so the two guards cover the checks

`cuda.yml` and `gpu.yml` called `modal run` directly, so #99's wedge watchdog
and #115's completion sentinel protected only runs a human started by hand.
Every performance number in this repo's recent history came through green
checks on the unguarded path.

The three jobs now call `scripts/modal-run` as the only command in their step,
so its exit code is the step's: 2 (spend limit), 3 (a wedge it stopped) and 4
(a run that ended without finishing) are each red, and nothing remaps them.

The wrapper also annotates those three when GITHUB_ACTIONS is set. They share a
property that makes them worth naming on the check rather than burying in a
log: none of them is the entry point failing, and all of them read from the
check list like a broken diff. The spend limit most of all -- it fails every
entry point at once, in seconds. No retry.

The graces keep their defaults. They budget *silence*, not elapsed time --
every client line resets the clock -- so a cold start costs wall time without
consuming one. `build` is 155-221 s warm and ~7.5 min cold (#99), and all of
that cold build is after Python is alive, printing `Compiling`, under the
1200 s silence budget; 300 s of startup silence is sized against the failure
itself, a container that said nothing for twenty-six minutes.

CI.md moves the wrapper out from under "running it by hand" -- it is now how
every Modal invocation here happens -- and tiers 2 and 3 say the guard covers
them.

* CI.md: the graces, justified from the runner's own timestamps

The first run of this branch: build reaches its first `$ ` line 6.5 s in
having never been silent for more than 3.1 s, against a 300 s startup budget;
the longest quiet stretch anywhere in either job is 58.9 s of sm_100a codegen
against 1200 s. And in tier 2 the silence budget cannot bind at all -- the
entry point's own timeout=900 is shorter. Tier 3 is where a tighter value would
buy something and is the tier with no measurement here, so it keeps #99's.

Also: CI installs modal unpinned and drew 1.5.3, where #103's three exit-code
behaviours were measured on 1.5.1. That is the argument for the sentinel in one
line.

* CI.md: the device_tests numbers, and what the graces actually cover

A dispatched gpu.yml run on this branch: first alive line at +9.2 s after 4.4 s
of silence, sentinel at +95 s, 97 s in the step. With that, all three entry
points CI invokes are measured, and the shape is clear -- SILENCE_GRACE (1200)
is at or above each one's own timeout= (900/900/1200), so it dead-heats with
Modal's clock and STARTUP_GRACE is what covers the failure timeout= cannot see.
A lower silence budget is what these numbers argue for; not on one warm sample
each.
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.

scripts/modal-run returns 0 for a run that was stopped, so a killed gate reads as a passing one

1 participant