Skip to content

7.9.4 Failure tests and integration

This part of Subprocess activities: the Go runner and the Python helper develops the worked mechanisms below.

Concepts from other sections (8)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

Learning objectives

  • Challenge retry, timeout, checkpoint, and process-cleanup behavior.

The cost is low. Each poll reads only the bytes appended since the last one, so tailing is O(new bytes)O(\text{new bytes}) per tick, not O(file)O(\text{file}), and the stderr ring is bounded at 4 KiB however much the child prints. The runner does a heartbeat RPC at most once per poll, and typically once per Thb/3T_{hb}/3.

Files beat RPC because a gRPC client inside Python would duplicate the durable protocol in a second language and couple the trainer to the platform. With files and exit codes, {tinyllm} train --spec ... --progress ... runs by hand with no cluster, and the protocol has one implementation. Polling beats inotify for a similar reason: inotify would cut latency from 200 ms to near zero, but it is Linux-only, misbehaves on network filesystems where /artifacts often lives, and still needs the partial-line logic. A 200 ms poll is far below any heartbeat timeout.

Process groups are chosen over cgroups knowingly. A child that calls setsid escapes its group; cgroups (systemd KillMode=control-group) would close that hole but need privileges a worker pod usually lacks. The group plus SIGKILL covers every well-behaved tree. Overriding the exit code with the stop reason is essential: trusting the child’s 130 would make every deploy cancel every training run for good. The runner knows why it sent SIGTERM; the child does not.

Some tradeoffs are accepted outright. Work since the last checkpoint is lost on SIGKILL. A duplicate delivery during a run is turned away with 75 and retried later rather than joined. And an orphan is noticed only at its next cancelled check.

A correct pair of implementations must handle every case below; each exists because some later workflow depends on it.

CaseKind of checkWhat must holdWhy it matters downstream
the worked example’s resume flowunitattempt 1 heartbeats its checkpoint and fails retryable; attempt 2 gets --resume, TL_ATTEMPT=2, the same directory, and returns DONE.jsonTrainRun resumes from the last checkpoint after a kill
work dir from the keyboundarykey segments become directories; .., ., empty, and odd characters are rejected, non-retryablea workflow id can never write outside /artifacts/activities
the environment contractunitTL_*, TRACEPARENT, OTEL variables set per task; the worker’s own copies never leakPython spans land in the right trace
the exit-code tableunitevery row of the exit-code table, signals includeda poison input fails fast; a transient one retries
success needs DONE.jsonboundaryexit 0 without DONE.json is a retryable MissingDone; over 2 MiB is non-retryable; exactly 2 MiB is fineresults fit in a gRPC message and a history event
partial progress linesunithalf a line waits for its newline; garbage lines are skippedheartbeats never carry half a path
new checkpoints are heartbeateduniteach new checkpoint is heartbeated while the child runs, even a line written in two piecesa kill one second after a checkpoint loses nothing
the resume point survivesfaulta retry’s heartbeats repeat the old resume point until a newer one existsa kill before the retry’s first checkpoint does not restart from step 0
heartbeat cadenceunitat least one heartbeat per Thb/3T_{hb}/3the server does not fence a long step
cancel_requested stops the childfaultcancel_requested sends SIGTERM; the run ends Canceled with the exit checkpoint heartbeateda workflow cancel reaches Python
grace, then SIGKILLfaulta child ignoring SIGTERM is SIGKILLed after the grace, grandchild includeda stuck child never holds a worker slot
own process groupunitthe child leads its own groupgroup signals reach grandchildren only
worker shutdown is retryablefaultctx ending gives a retryable WorkerShutdown with Details = the exit checkpointa deploy does not end a training run for good
shutdown flushes an unacknowledged checkpointfaulta failed checkpoint heartbeat is retried on shutdown even when the final tail read is emptyresume state reaches the server
cancel cause from the contextfaultwith Subprocess.Canceled set, a ctx cancelled with the SDK’s cancel cause ends Canceled; any other cause stays WorkerShutdownthe worker SDK’s cancel reaches the child as a cancel
atomic spec.jsonunitspec.json replaced by rename, never rewritten in placea retry never shows half a spec
bounded stderr tailboundarythe last 4 KiB of stderr, traceback includedfailures stay small in the history
the worked example’s progress linesunitthe three progress lines and DONE.json, byte for bytethe runner and the schema read these bytes
flags and environmentunit--spec, --progress, --resume (both spellings), TL_* defaults; missing flags are SpecErrorthe entry and the runner agree on argv
one flushed line per eventuniteach event is on disk as one line when emit returnsthe tail sees checkpoints at once
bad events rejectedboundaryunknown kinds and NaN or inf values raise, nothing writtena NaN loss never corrupts the progress file
checkpoint only when completeunitno event for an incomplete directory; paths relative to TL_ARTIFACTS; outside paths rejectedthe resume point is always loadable
publish never exposes a partial filefaultwhen the rename never happens, the final name does not existDONE.json exists only for finished work
outputs stay in the work dirboundary.., absolute, and empty names rejectedone activity cannot overwrite another’s files
run exit codesunitSpecError 65, RetryableError 75, Cancelled 130, crash 1, success 0 with DONE.jsonthe Go side’s table has the right inputs
spec validation is exit 65unitvalidation errors and non-object or bad JSON specs are 65, before any outputinvalid specs do not burn retries
rerun after done is a no-opregressiona second run finds DONE.json, skips main, re-emits donea duplicate delivery has one effect
SIGTERM checkpoints and exits 130faultSIGTERM, then a checkpoint as the last event, then exit 130 within 5 scancel and drain keep the work done so far
a second attempt is locked outfaultwhile one attempt holds the lock, another exits 75 without runningno two children write one directory
an orphaned child stopsfaulta child whose parent died sees cancelledan orphan does not train on beside its successor
the worked example’s span treeconformancethe example’s ids, parents, kinds, and JSON encoding at a real HTTP receivercontrol-plane observability finds train.step under activity train
parse_traceparentboundaryvalid, unsampled, future-version, and nine invalid headersinvalid input starts a new trace
format round tripunitformat_traceparent and parse_traceparent agreecontexts can be handed on
unsampled parent exports nothingboundaryflags 00: no spans, no gauges sentno orphan spans
no TRACEPARENT starts a rootboundaryno or bad TRACEPARENT: a fresh trace id and no parenthand runs still trace
Tracer.from_envunitendpoint, service name, resource attributes, parent from the environmentthe runner’s variables are the only input
a dead collector never fails the workfaultconnection refused: flush returns False within its timeout, queue droppedtraining survives a collector outage
a collector error status is Falsefaulta 500 answer is Falsedrops are visible
an error marks the spanunitan exception sets status ERROR and error.type, and propagatesfailed stages show in the trace
step samplingunitevery 50th stepspan volume stays bounded
gauges are pushedunitthe latest gauge value per name and attributes goes to /v1/metricstl.train.loss reaches Prometheus
shutdown stops exportunitnothing is queued after shutdownclean exit
body shape without a parentunita root span has no parentSpanId; attribute value typesthe collector accepts the body

The mistakes people make in the Go runner mostly lose the resume point. If checkpoints never reach a heartbeat, the retry after a kill starts from step 0; drain turns each new ckpt into last, and the loop heartbeats at once when fresh. If --resume is not passed, retries ignore the checkpoint; Run appends --resume <LastHeartbeat> whenever the task carries details. If half a progress line is parsed, a checkpoint event is lost forever; Tail keeps the bytes after the last newline in partial. And heartbeating only on checkpoints lets the server fence the activity mid-step, so the loop also beats when time.Since(lastBeat) >= T_hb/3.

Warning: A retry that starts last empty erases the resume point on its first periodic heartbeat, before the child has written any new checkpoint. Seed last with a copy of t.LastHeartbeat.

Other runner pitfalls concern isolation and verdicts. Unchecked key segments let a workflow id ../../x write outside /artifacts; WorkDir rejects ., .., empty, and non-[A-Za-z0-9._-] segments before joining. An inherited TRACEPARENT makes Python spans join an unrelated trace; childEnv always drops the trace variables from the base environment. A wrong row in the exit-code table makes a poison spec retry until the dead-letter queue, or a cancel restart; Classify and run implement the table row for row. Trusting exit 0 without DONE.json lets a broken entry “succeed” with no outputs, so exit 0 still reads DONE.json and its absence is MissingDone. Ignoring cancel_requested keeps a cancelled workflow training; beat calls stop("Canceled") when the answer says so. Without a process group or a SIGKILL, grandchildren and stuck children outlive the activity; the runner uses Setpgid, signals -pgid, and SIGKILLs after the grace and again after exit. Writing spec.json in place lets a reader see a truncated spec, which writeAtomic (temp file, fsync, rename) prevents. Keeping all of stderr lets a chatty child blow the 4 MiB message limit, so stderr is teed into a 4 KiB ring.

Warning: Reporting a drain as a cancel ends every training run for good on every deploy; reporting a cancel as a drain retries a run the operator stopped. Take stopReason from Subprocess.Canceled(context.Cause(ctx)) and let it override the child’s exit code.

On the Python side, the pitfalls break the file agreement. Unflushed progress means the runner sees nothing until exit; emit flushes after every line. Absolute paths in events or DONE.json are right on one pod and wrong on the next; _artifact_path makes every path relative to TL_ARTIFACTS. A NaN loss written as NaN makes the line not JSON; emit rejects non-finite floats and dumps with allow_nan=False. A ckpt event before the checkpoint is complete leaves the retry unable to load its resume point; checkpoint emits only when verify_step_dir returns no problems. Writing outputs under their final name lets a kill leave a truncated DONE.json that looks finished; publish writes name.tmp and renames. Unconfined output names let one activity overwrite another’s files; output rejects absolute names and anything outside the work dir. Ignoring spec validation errors trains on garbage, or retries; load_spec raises SpecError (exit 65) for bad JSON, non-objects, and validation errors. Skipping the DONE.json check on start makes a duplicate delivery train again; run re-emits done and exits 0 when the file exists. A SIGTERM handler that exits at once loses the work since the last checkpoint; the handler only sets a flag, and the loop checkpoints and then raises Cancelled. Without a work-dir lock, an orphan and its successor write one directory; run takes a non-blocking flock and exits 75 if it cannot. And undetected orphans train on after their worker died; cancelled compares os.getppid() with the pid recorded at start.

The exporter has its own set. Ignoring the remote parent makes Python spans a separate trace; Tracer takes the trace id and parent span id from TRACEPARENT. Encoding intValue as a JSON number makes collectors reject or round the attribute; _value encodes ints as strings. Lenient traceparent parsing lets all-zero ids glue unrelated runs together; parse_traceparent rejects all-zero ids, version ff, and extra fields on 00. Ignoring the sampled flag produces orphan spans whose parents were never recorded; self.sampled comes from the parent, and end_span and flush honour it. Ignoring OTEL_SERVICE_NAME makes every system’s Python report as tinyllm-python; from_env reads it and falls back only when unset. Raising export errors lets a collector outage kill training; _post catches network errors and returns False. Swallowing exceptions in the span makes a failing stage look successful; span marks the error and re-raises. Sampling off by one never traces step 0 but always traces step 1; should_sample_step is step % every == 0. Keeping the first gauge value makes dashboards show the loss of step 1; gauge overwrites the entry for (name, attributes). And queueing spans after shutdown grows memory at exit and half-sends spans; shutdown sets _closed, and end_span stops queueing.

This section stands on two earlier pieces. verify_step_dir from Safetensors for every dtype, atomic checkpoints, and the token stream gates every ckpt event, and --resume loads with load_checkpoint. The worker and activity SDK from the task protocol and workers and activities are what the composition root adapts to Task and Heartbeat.

Everything Python on the platform now runs through it. CorpusBuild runs each corpus stage as {corpus} run --stage <s> through the runner (CorpusBuild: the pipeline as a durable workflow). TrainRun and EvalSuite run {tinyllm} train and eval, and kills resume from the heartbeated checkpoint (Platform workflows: TrainRun and EvalSuite). The control-plane trace reaches train.step through this exporter (Observability for the control plane in Systems Engineering). The worker image carries the activity helper and the exporter (Durable and worker images and charts in Systems Engineering). And the export step of ModelRelease and the TinyStories capstone, whose run is a TrainRun, use the same contract.

Production systems extend each piece. Temporal’s activity heartbeats with details (Go SDK activity.RecordHeartbeat, activity.GetHeartbeatDetails) add heartbeat throttling, details as typed payloads, and async completion for work finished elsewhere. Kubernetes Job podFailurePolicy generalizes the exit-code table into rules that fail the Job, ignore the failure, or count it (Kubernetes docs, “Handling retriable and non-retriable pod failures”). systemd KillMode=control-group and Kubernetes terminationGracePeriodSeconds use cgroups instead of process groups, so no child can escape by calling setsid (systemd.kill(5)). Linux PR_SET_PDEATHSIG replaces orphan detection by parent pid: the kernel signals the child when the parent thread dies (prctl(2)). And the OpenTelemetry Python SDK with the OTLP/HTTP exporter (opentelemetry-exporter-otlp-proto-http) adds a batching span processor, retries with backoff, and protobuf encoding.

  • Subprocess activity: one attempt at one activity, run as a child process of a Go worker, whose interface is files, environment variables, signals, and an exit code.
  • Work directory: <TL_ARTIFACTS>/activities/<key>/, shared by every attempt of one activity so that a retry resumes.
  • Heartbeat details: the bytes sent with each heartbeat and returned to the next attempt; here, the last complete checkpoint path.
  • Publish by rename: writing to a temp file, fsyncing, and renaming, so a reader sees the old file or the whole new one.
  • Process group: the set of processes led by the child, signalled together with kill(-pgid, sig).
  • Worker shutdown: a retryable stop caused by the worker going away, as opposed to a non-retryable cancel.
  • TRACEPARENT: the W3C header that carries the parent span’s trace id, span id, and sampled flag into the child.
  • Python never speaks gRPC to the platform. A Go worker runs it as a child process, and the whole interface is files, environment variables, signals, and an exit code.
  • The exit code is the verdict: 65 never retries, 75 and crashes retry, 130 is a cancellation. A worker drain is not a cancellation, so the runner reports it as retryable even when the child exits 130.
  • A checkpoint becomes a heartbeat detail the moment it is complete, and the next attempt gets it as --resume. Until a newer one exists, every heartbeat keeps sending the old one.
  • Every attempt shares one work directory named by the idempotency key. Outputs are published by rename, DONE.json last, and an exclusive lock keeps an orphaned child from writing beside its successor.
  • The Python spans continue the activity’s trace from TRACEPARENT, honour its sampled flag, and a dead collector never fails training.
  1. Why does the runner seed last from t.LastHeartbeat instead of starting it empty?
    AnswerEvery heartbeat sends `last` as the details, and the server stores whatever it receives. If a retry started with empty details, its first periodic heartbeat (before any new checkpoint) would erase the resume point, and a kill at that moment would restart from step 0.
  2. The child exits 130 after a worker drain. Why does Run return a retryable WorkerShutdown and not Canceled?
    Answer130 only says "I was asked to stop". The runner knows who asked: `ctx` ended without the SDK's cancel cause, so the worker is going away and the work is unfinished. Reporting `Canceled` (non-retryable) would end the run for good on every deploy; `WorkerShutdown` lets another worker resume from `Details`.
  3. Why is there a final heartbeat on context.WithoutCancel(ctx) after the loop, and why compare against acknowledged rather than only checking fresh?
    AnswerThe child writes its exit checkpoint after SIGTERM, when `ctx` may already be done, so a heartbeat on `ctx` would fail immediately. `fresh` covers a checkpoint read in the final drain; `acknowledged` covers one read earlier whose heartbeat failed or was skipped because `stopReason` was set. Either way the server must learn the latest resume point.
  4. Why does run take the lock before checking DONE.json, not after?
    AnswerChecked outside the lock, the answer can be stale: a new attempt sees no `DONE.json` while an orphan is still finishing, then gets the lock the moment the orphan publishes `DONE.json` and exits, and trains a second time. Under the lock, the check and the work form one critical section, so whoever holds the lock sees the final state of the directory.
  5. Why is tl.train.step = 50 encoded as {"intValue": "50"}?
    AnswerOTLP's JSON mapping follows protobuf's, where int64 is a string: JSON numbers are doubles in most parsers and lose exactness past $2^{53}$. A collector may reject or round a bare number.
  6. Why does the tail of a new attempt start at the current end of progress.jsonl rather than at byte 0?
    AnswerEarlier attempts' events are history. Their resume point already arrived through `LastHeartbeat`, so rereading them would only replay old checkpoints; starting at the file's current size means each attempt reads only its own events.

Contents