crash_report
Turning a crashed background run's leftovers into a telemetry event.
The pod finds a crashed run's breadcrumb, its fault file's text and, when the
runner reported the exit, its exit code. This module classifies the exit code,
reduces the fault text to a kind and a few package-relative frames, and builds
the BackgroundDAGRunCrashedEvent from those.
The fault text is untrusted input: it holds the site's file paths. Nothing from
it reaches the event except a code for the fatal-exception line (from a fixed
set), frames reduced to module/function:line whose path resolves to installed
code, and two fixed markers. The whole text goes to this module's logger, which
reaches the local log only, before the file is deleted.
Module
Functions
build_crash_event
def build_crash_event( *, flow_run_id: str | None, detected_by: DetectedBy, detected_at: float, exit_code: int | None, parameters: Mapping[str, Any], breadcrumb: BreadcrumbSnapshot | None, fault: FaultSummary,) ‑> BackgroundDAGRunCrashedEvent:Build the crash event for one run.
Arguments
flow_run_id: The crashed flow run.detected_by: Which path found the crash.detected_at: When it was found, in epoch seconds.exit_code: The runner-reported exit code, orNoneif there is none.parameters: The flow run's parameters. The run's keys are taken from here when present, otherwise from the breadcrumb.breadcrumb: The run's breadcrumb, orNoneif there is none.fault: The summary of the run's fault file.
Returns
The event. Its native_crash is true when the exit kind is a native
one. Otherwise, with an exit code, it is true only if the fault text has
a Fatal Python error line: a Windows exception line alone may be a
handled first-chance exception in a run that was then killed. Without an
exit code it is true for a Fatal Python error line, None (unknown)
for other fault text and false when there is none.
classify_exit_code
def classify_exit_code(exit_code: int) ‑> tuple[str, str | None]:Classify a process exit code.
A signed Windows status, such as -1073741819 for 0xC0000005, is
normalised to its unsigned 32-bit value before it is looked up.
Arguments
exit_code: The exit code the runner reported.
Returns
(exit_kind, exit_code_hex). The kind is access_violation,
illegal_instruction, stack_overflow, fail_fast,
heap_corruption (0xC0000374), terminated (0xC000013A, how a
Prefect kill shows on Windows), one of segmentation_fault,
bus_error, floating_point_exception, illegal_instruction or
aborted for the matching fatal POSIX signal (a code from -64 to -1),
signal_<n> for any other signal in that range, or other. The hex is
0xXXXXXXXX, or None for a signal. A shell-style 128 + n code is
other. The kinds in NATIVE_EXIT_KINDS are the native crashes.
terminated, signal_<n> and other say only that the process was
stopped or exited.
parse_runner_exit_code
def parse_runner_exit_code(message: str | None) ‑> int | None:Read the exit code out of the runner's crashed-state message.
Arguments
message: The crashed state's message, if it has one.
Returns
The exit code, or None if the message is not the runner's report of a
process exit.
report_crash
def report_crash( flow_run_id: str, *, detected_by: DetectedBy, exit_code: int | None, parameters: Mapping[str, Any], root: Path | None = None,) ‑> BackgroundDAGRunCrashedEvent | None:Report one crashed run from the files it left, then delete them.
The raw fault text is logged at ERROR on this module's logger before
anything else. That logger reaches the local log only, so the desktop log
keeps the whole stack; the event carries only the reduced kind and frames
(see summarise_fault), because the text holds the site's file paths. The
event is flushed to Datadog (waiting at most ten seconds) before the files
are deleted, as an event still in the pod's buffer is lost if the pod dies.
A run whose report failed, or whose flush did not finish in time, keeps its
files for the start-up sweep, and one whose deletion failed leaves a
tombstone the sweep deletes without reporting. Never raises.
Arguments
flow_run_id: The crashed flow run, which names its files.detected_by: Which path found the crash.exit_code: The runner-reported exit code, orNoneif there is none.parameters: The flow run's parameters.root: The directory holding the run's files. Defaults to the resolved breadcrumb root.
Returns
The emitted event, or None if it could not be built or emitted.
report_crashed_run
def report_crashed_run(flow: Flow[..., Any], flow_run: FlowRun, state: State) ‑> None:Report a background run whose process the runner saw exit non-zero.
The Prefect on_crashed hook of the background flow. Both the runner, in
the pod and in a thread of its own after a process exit, and the child's own
engine, for a crash state the child built for itself, call it. Only the
runner's message carries an exit code, so the message check is what skips
the child's calls. A message that is not the runner's report of an exit
leaves the run's files in place for the start-up sweep. Never raises.
Arguments
flow: The flow that crashed. Unused.flow_run: The crashed flow run.state: Its crashed state.
start_breadcrumb_sweep_thread
def start_breadcrumb_sweep_thread() ‑> threading.Thread | None:Run the start-up breadcrumb sweep once on a daemon thread.
Returns
The started thread, or None if it could not be started. Never raises.
summarise_fault
def summarise_fault(text: str | None) ‑> FaultSummary:Reduce fault-file text to its fatal-exception line and top frames.
The kind is a code for the last Fatal Python error or Windows fatal exception line.
The frames are those of the first Current thread block after it, up to the
first blank or unindented line, and the top FAULT_FRAMES_KEPT are kept;
the rest are not resolved. No other text from the file is returned.
Arguments
text: The fault file's text, if any.
Returns
The summary; fault_kind and fault_frames are None when the text
has no fatal line, and has_text says whether there was any text.
sweep_orphaned_breadcrumbs
def sweep_orphaned_breadcrumbs(root: Path | None = None) ‑> int:Report the runs whose process died without the pod reporting them.
Each <uuid>.json whose process is no longer running (judged by pid and
create time) is reported through report_crash and its files are removed.
An unreadable breadcrumb is read again once, then reported as unreadable
unless it was modified in the last few seconds. A tombstone (a breadcrumb of
a run that ended or was reported) is deleted without a report. A leftover
from a run that died tearing down is reported with its teardown phase. A
<uuid>.fault with no breadcrumb beside it is deleted.
Files not named for a run id are left alone, and a root that is not private
to this user is left untouched. Never raises.
Arguments
root: The directory holding the files. Defaults to the resolved root.
Returns The number of crashes reported.
Classes
FaultSummary
class FaultSummary( fault_kind: str | None = None, fault_frames: tuple[str, ...] | None = None, has_text: bool = False, python_fatal: bool = False,):What the event keeps of a fault file.
Attributes
fault_kind: A code for the last fatal-exception line (access_violation,illegal_instruction,stack_overflow,windows_exception,segmentation_fault,bus_error,aborted,floating_point_exceptionorother), orNoneif there is none.fault_frames: The faulting thread's top frames asmodule/function:line, orNoneif there are none.has_text: Whether the fault file held any text, fatal line or not.python_fatal: Whether the text holds aFatal Python errorline, which faulthandler writes only from a fatal-signal handler. AWindows fatal exceptionline does not count: faulthandler also writes one for a first-chance exception that is then handled.
Variables
-
fault_frames : tuple[str, ...] | None
-
fault_kind : str | None
-
has_text : bool
-
python_fatal : bool