<@ULVA73B9P> Is there a way to know the exit statu...
# ask-marvin
a
@Marvin Is there a way to know the exit status of the docker container that ran a flow?
m
thought for 332.7 seconds
Yes. In Prefect 3.x, the Docker worker captures the container’s exit code and surfaces it on the flow run. What you’ll see - Non‑zero exit code → flow run is marked Crashed and the exit code is included in the state message (e.g., “Flow run infrastructure exited with non‑zero status code 137.”). Worker logs also include a short explanation (e.g., 137 ≈ OOM kill). How to view it - CLI (state + message with exit code):
Copy code
prefect flow-run inspect <FLOW_RUN_ID>
- CLI (logs, including worker’s exit-code hint):
Copy code
prefect flow-run logs <FLOW_RUN_ID>
- Python API:
Copy code
from prefect import get_client

  async def show_status(flow_run_id):
      async with get_client() as client:
          fr = await client.read_flow_run(flow_run_id)
          print(fr.state.type)       # e.g. 'Crashed'
          print(fr.state.message)    # includes "non-zero status code <N>"
Notes - Exit codes are not stored as a separate field; they’re included in the flow run’s state message and worker logs. - Any non‑zero exit code is treated as infrastructure failure → Crashed. Common examples: 137 (OOM/SIGKILL), 143 (SIGTERM), 125/126/127 (container/command errors). - If you need a specific code, you can make your container/entrypoint return it; Prefect will record it in the message. Docs and source - States concept: States in Prefect - Docker worker guide: Run with Docker - CLI reference (inspect/logs): prefect flow-run commands - Docker worker capturing exit code (source): prefect-docker worker.py - Worker mapping non‑zero exit → Crashed: prefect workers/base.py - Exit code hints registry (explanations in logs): _infrastructure_exit_codes.py If you want, I can share a tiny helper to parse the numeric code out of the state message programmatically.
a
@Marvin We are using prefect=3.4.8. I dont think I can find
prefect/utilities/_infrastructure_exit_codes.py
Is there a way to find similar functionality for
get_infrastructure_exit_info
?
m
thought for 58.8 seconds
Great question — you’re right. In Prefect 3.4.8 there is no
prefect.utilities._infrastructure_exit_codes
module and no
get_infrastructure_exit_info
helper. That centralized “exit code → explanation” mapping was added later. What happens in 3.4.8 - Workers (including the Docker worker) capture the raw infrastructure exit code and, if it’s non‑zero, mark the flow run Crashed with a generic message: - “Flow run infrastructure exited with non-zero status code <N>.” - There’s no built‑in mapping to human‑readable hints (e.g., 137 = OOM) in 3.4.8. Where this is handled in 3.4.8 (source links) - Base worker: non‑zero exit → Crashed with generic message - base.py L1322-L1328 - Also in ad‑hoc path: base.py L895-L902 - Docker worker: extracts exit code from container and returns it - Extract exit code: prefect_docker/worker.py L471-L475 - Non‑zero handling path: prefect_docker/worker.py L597-L604 How to get similar functionality to “exit code hints” - Option A: Manually map common codes in your own post‑processing (e.g., parse
flow_run.state.message
and add hints in your observability/logging).
Copy code
COMMON_EXIT_HINTS = {
      137: "Likely OOM kill (SIGKILL)",
      143: "Graceful termination (SIGTERM)",
      125: "Container failed to run (Docker/OCI)",
      126: "Command not executable (permission issue)",
      127: "Command not found",
  }
- Option B: Upgrade to a newer Prefect 3.x where centralized exit‑code hints were added. Then worker logs/state messages include short explanations automatically. Quick ways to inspect the code/message today - See state + message (includes exit code):
Copy code
prefect flow-run inspect <FLOW_RUN_ID>
- See logs (worker logs will show the generic non‑zero message in 3.4.8):
Copy code
prefect flow-run logs <FLOW_RUN_ID>
- Via API:
Copy code
from prefect import get_client

  async def show_status(flow_run_id):
      async with get_client() as client:
          fr = await client.read_flow_run(flow_run_id)
          print(fr.state.type)     # 'Crashed' if non-zero
          print(fr.state.message)  # includes "non-zero status code <N>"
If you’d like, I can share a small utility that: - Pulls recent Crashed runs - Extracts the numeric exit code from
state.message
- Adds a human‑readable hint using a mapping like above - Emits a summary report in Slack or logs