breadcrumbs
Per-run crash breadcrumbs and fault files for background DAG runs.
A native crash (an access violation, an abort) ends a run's process without any Python code running, so nothing in the run can report it. Each run therefore keeps two small files that outlive the process, and the pod reads them once it sees the process exit:
- The breadcrumb is a fixed-width JSON file recording where the run is: its phase, step, model, and file and wave indices. It is rewritten in place, so whatever was last written survives the process dying.
- The fault file is where
faulthandlerwrites the stacks of a fatal signal.
Both live in an auto-wiped location, hold no patient data, and are deleted when
the run ends or its crash has been reported. The fault file's text does hold the
site's source paths, so it is logged to the local log only (never under the
Datadog-shipped bitfount.telemetry namespace) before it is deleted.
This module is imported by low-level data-reading code, so it depends only on
the standard library, psutil and bitfount.config.
Module
Functions
breadcrumb_phase
def breadcrumb_phase(phase: BreadcrumbPhase, *, slot: Slot = 'main') ‑> Iterator[None]:Hold phase on slot for the body, then restore the phase it replaced.
The earlier phase comes back even when the body raises, and the body's
exception is re-raised. Only this scope's own errors are swallowed. Scopes
on one slot may overlap across threads: once the last of them ends, the slot
has the phase it had before the first began, or the phase of a note made
on the slot while they were open, whichever is later.
clear_breadcrumb_slot
def clear_breadcrumb_slot(slot: Slot) ‑> None:Empty slot on the current handle; does nothing without one.
For a line of execution that has finished a piece of work, so that a crash after it is not reported against what it last did.
mark_breadcrumb_closed
def mark_breadcrumb_closed(json_path: Path) ‑> None:Overwrite a breadcrumb in place with a tombstone. Never raises.
A tombstone says the run has ended or its crash has been reported, so a file whose deletion then fails is deleted by the next sweep and not reported again. The write keeps the file's width and does not truncate it.
Arguments
json_path: The breadcrumb file; one that is missing is left missing.
note
def note( phase: BreadcrumbPhase, *, slot: Slot = 'main', step: str | None | _Unset = _Unset.UNSET, model_ref: str | None | _Unset = _Unset.UNSET, file_seq: int | None | _Unset = _Unset.UNSET, files_total: int | None | _Unset = _Unset.UNSET, wave_index: int | None | _Unset = _Unset.UNSET,) ‑> None:Note phase on the current handle; does nothing without one.
The keywords are those of RunBreadcrumbs.note. The path with no open
handle takes no lock.
note_gpu
def note_gpu(gpu: GpuBreadcrumb) ‑> None:Record a device reading on the current handle; no-op without one.
The path with no open handle takes no lock.
Arguments
gpu: The reading to record.
open_run_breadcrumbs
def open_run_breadcrumbs( flow_run_id: str | None, run_id: str, *, event_context: Mapping[str, str], root: Path | None = None,) ‑> RunBreadcrumbs:Open the breadcrumb and fault files for a run. Never raises.
The handle is disabled when flow_run_id is None (there is no run to name
the files after), when the files cannot be created, and when another handle
is already open in this process. An enabled handle is the process-wide
current handle, which the module-level note and breadcrumb_phase write
to, until it is closed.
Arguments
flow_run_id: The flow run identifier, used to name the files.run_id: The run's identifier, recorded in the breadcrumb.event_context: The run'stask_hash,project_id,datasource_nameandtrigger, recorded so a crash found later can name its lineage.root: The directory for the files. Defaults to the resolved root.
read_breadcrumb
def read_breadcrumb( path: Path,) ‑> BreadcrumbSnapshot | None:Read a breadcrumb file.
Returns None when the file is missing or cannot be opened. A file that
does not parse as a version 1 breadcrumb, such as one read mid-write, gives
a snapshot whose fields are all None and whose main phase is
"unreadable".
read_fault_text
def read_fault_text(path: Path, max_bytes: int = 4194304) ‑> str | None:Return a fault file's text, or its last max_bytes if it is larger.
A fatal dump is tens of kilobytes, so the cap is far above any real file. A file over it is read from its end, where the last dump is, and the text starts with a marker line giving the bytes left out.
Arguments
path: The fault file.max_bytes: The most to read.
Returns
The text, or None when the file is missing, cannot be read or is empty.
Bytes that are not valid UTF-8 are replaced.
remove_run_files
def remove_run_files(flow_run_id: str, root: Path | None = None) ‑> None:Delete a run's fault file and then its breadcrumb. Never raises.
The breadcrumb goes last, so a run's breadcrumb always outlives its fault file. A file that is already gone counts as deleted, and one another process holds open is retried briefly (a Windows open file cannot be deleted).
Arguments
flow_run_id: The run's flow run identifier, which names its files.root: The directory holding the files. Defaults to the resolved root.
resolve_breadcrumb_root
def resolve_breadcrumb_root() ‑> pathlib.Path:Return the directory holding run breadcrumbs, creating nothing.
The setting is read at each call. Without it the root is a directory under the system temporary directory, which is cleared on its own schedule.
run_file_paths
def run_file_paths( flow_run_id: str, root: Path | None = None,) ‑> tuple[pathlib.Path, pathlib.Path]:Return a run's (breadcrumb path, fault path).
Arguments
flow_run_id: The run's flow run identifier, used as the file stem.root: The directory holding the files. Defaults to the resolved root.
Raises
ValueError: Ifflow_run_idis not a plain file name.
Classes
BreadcrumbSlot
class BreadcrumbSlot( phase: str | None = None, step: str | None = None, model_ref: str | None = None, file_seq: int | None = None, files_total: int | None = None, wave_index: int | None = None,):Where one line of execution in a run has got to.
Attributes
phase: What the line of execution is doing.step: The name of the DAG step.model_ref: The model being run.file_seq: The index of the file in the step's plan.files_total: How many files the step plans to process.wave_index: The index of the wave being run.
Variables
-
file_seq : int | None
-
files_total : int | None
-
model_ref : str | None
-
phase : str | None
-
step : str | None
-
wave_index : int | None
BreadcrumbSnapshot
class BreadcrumbSnapshot( pid: int | None = None, process_started_at: float | None = None, written_at: float | None = None, run_id: str | None = None, task_hash: str | None = None, project_id: str | None = None, datasource_name: str | None = None, trigger: str | None = None, main: BreadcrumbSlot = BreadcrumbSlot(phase=None, step=None, model_ref=None, file_seq=None, files_total=None, wave_index=None), read_ahead: BreadcrumbSlot = BreadcrumbSlot(phase=None, step=None, model_ref=None, file_seq=None, files_total=None, wave_index=None), gpu: GpuBreadcrumb | None = None,):The contents of a breadcrumb file, as read back.
Attributes
pid: The process the run was in.process_started_at: That process's creation time, in epoch seconds.written_at: When the breadcrumb was last written, in epoch seconds.run_id: The run's identifier.task_hash: The hash of the run's task.project_id: The run's project.datasource_name: The run's datasource.trigger: What started the run.main: The run's main line of execution.read_ahead: The line of execution reading files ahead of the main one.
Variables
-
datasource_name : str | None
-
gpu : GpuBreadcrumb | None
-
main : BreadcrumbSlot
-
pid : int | None
-
process_started_at : float | None
-
project_id : str | None
-
read_ahead : BreadcrumbSlot
-
run_id : str | None
-
task_hash : str | None
-
trigger : str | None
-
written_at : float | None
GpuBreadcrumb
class GpuBreadcrumb( utilisation_pct: float | None = None, memory_used_bytes: int | None = None, memory_total_bytes: int | None = None, samples: int | None = None,):The last device reading a run took, for a crash report to carry.
GpuUsageSampler emits its summary when a step ends, so a step that kills
the process reports nothing - the case where the figures matter most. The
sampler therefore also mirrors its latest sample here, into the breadcrumb,
which is the file that outlives the process. Every field is device-wide (from
NVML) rather than per-process, so a fault at the card's limit is
distinguishable from one in our own allocation.
Attributes
utilisation_pct: The GPU's compute utilisation at the last sample.memory_used_bytes: Device memory in use across all processes.memory_total_bytes: The device's total memory.samples: How many samples the sampler had taken when this was written.
Variables
-
memory_total_bytes : int | None
-
memory_used_bytes : int | None
-
samples : int | None
-
utilisation_pct : float | None
RunBreadcrumbs
class RunBreadcrumbs():The breadcrumb and fault files of one background run.
Obtain one from open_run_breadcrumbs. A handle that is not enabled
accepts every call and does nothing, so callers need no checks of their own.
Notes may arrive from several threads at once: the latest wins and the file
always holds one complete breadcrumb.
Create a disabled handle.
Variables
enabled : bool- Whether the handle is open and writing a breadcrumb.
Methods
clear
def clear(self, slot: Slot) ‑> None:Empty slot: its line of execution is between pieces of work.
Every field and the phase are cleared, as is the phase any
breadcrumb_phase scope open on slot would put back. Never raises.
close
def close(self) ‑> None:End the run's breadcrumbs, deleting both files. Idempotent.
Faulthandler is pointed away from the fault file before it closes. Fault text found in the file is logged to the local log, as a run that reaches its end after a handled fault is not a crash. The breadcrumb is overwritten with a tombstone before it is deleted, so a delete that fails is not reported as a crash. Never raises.
note
def note( self, phase: BreadcrumbPhase, *, slot: Slot = 'main', step: str | None | _Unset = _Unset.UNSET, model_ref: str | None | _Unset = _Unset.UNSET, file_seq: int | None | _Unset = _Unset.UNSET, files_total: int | None | _Unset = _Unset.UNSET, wave_index: int | None | _Unset = _Unset.UNSET,) ‑> None:Record the phase of slot and any fields passed, then write the file.
Fields not passed keep their value, and a field passed as None is
cleared. A note made while breadcrumb_phase scopes are open on slot
is also the phase those scopes put back when they end, so the latest
note wins over a phase noted before the scopes began (parallel steps
share the main slot). Never raises: a failed write leaves the earlier
breadcrumb in place.
note_gpu
def note_gpu(self, gpu: GpuBreadcrumb) ‑> None:Record the latest device reading and write the breadcrumb.
Overwrites the previous reading: only the last one before a fault is worth anything to a crash report. Never raises.
Arguments
gpu: The reading to record.
phase_of
def phase_of(self, slot: Slot = 'main') ‑> str | None:Return the phase last noted on slot, or None if none was.