process
process
¶
Process safety utilities.
Shared guards for process group operations that prevent PID-recycle or mock-object bugs from escalating into session-wide kills (F-490). Also hosts identity-bound descendant cleanup for CLI execution.
Classes¶
BoundedCommandResult
dataclass
¶
BoundedCommandResult(returncode, stdout, stderr, timed_out, drain_grace_fired, output_capped=False, supervisor_pid=None, reparented_orphans=0)
Outcome of :func:run_bounded_command.
Functions:¶
supervised_argv
¶
Wrap an instrument argv in the Linux instrument supervisor.
Returns command unchanged where the supervisor cannot run (not Linux,
or the subreaper prctl is unavailable). The supervisor runs by its absolute
file under -I so nothing in the workspace can shadow it (GH #445).
report_path is consumed by interactive attempts (GH #487): the
supervisor writes a JSON forensic report there (forwarded-signal
history, escalation decision), which the driver surfaces in the
session_lost detail to attribute the pane's death.
Source code in src/marianne/utils/process.py
safe_killpg
¶
Session-safe wrapper around os.killpg (F-490).
Refuses when pgid would target init, the caller's own process group,
or an invalid value. Prevents PID-recycle or mock-object bugs from
translating into kill(-1, SIGKILL) that nukes the user session.
In particular, os.killpg(1, sig) compiles to kill(-1, sig)
in the kernel, which sends the signal to every process the caller
owns except init — killing systemd --user, every terminal, pytest,
and the daemon.
Guards:
- pgid <= 1: init (1), own pgroup in killpg(0) idiom (0), or invalid
- pgid == os.getpgid(0): our own process group (would kill us plus
whatever shell/pytest/terminal shares the group)
Returns True if the signal was actually sent, False if blocked. Callers should treat False the same as a successful kill for cleanup purposes — the target is either unreachable or would have killed the caller.
Source code in src/marianne/utils/process.py
spawn_identity
¶
The two identities a caller records at spawn for a process it owns.
The kernel start tick (/proc/<pid>/stat field 22, boot-relative) is
the stable Linux identity; psutil's create_time() float drifts with
every wall-clock correction and is the non-Linux fallback only (GH #446:
Circuit measured it accumulating ~6 s/min under Hyper-V time sync, so no
tolerance makes it safe for a sheet that runs an hour).
Source code in src/marianne/utils/process.py
snapshot_descendant_trees
¶
Capture actual descendants while the parent still owns their ancestry.
Parent identity: a caller that recorded parent_start_ticks at spawn is
checked against the kernel start tick. A caller that only has the float
is checked against the float, which on Linux moves with every wall-clock
step (psutil re-reads btime per call; GH #446) — so the float check
is tolerant of the identity-preserving case: when the kernel tick is
readable and the process is still the same one psutil knew at spawn
(same PID, create_time within the step tolerance), the capture proceeds.
A recycled PID has a different tick AND a different float by far more
than any clock step, so it is still refused.
Source code in src/marianne/utils/process.py
snapshot_pipe_holders
¶
Capture owners of this invocation's pipes when ancestry was reparented.
The backend owns the read ends, so these inodes cannot be recycled while it scans. Only same-UID processes born after the direct parent qualify. The reaper still requires an unchanged PID, start time, UID and PGID.
Source code in src/marianne/utils/process.py
snapshot_file_holders
¶
Capture same-UID processes holding this invocation's unique log inode.
Source code in src/marianne/utils/process.py
snapshot_process_group_members
¶
Capture same-UID members of an action's private group, even with closed stdio.
Source code in src/marianne/utils/process.py
reap_descendant_trees
¶
Signal only surviving, unchanged leaders of captured descendant groups.
Source code in src/marianne/utils/process.py
run_bounded_command
async
¶
run_bounded_command(proc, *, timeout_seconds, pgid, context, post_exit_drain_grace_seconds=2.0, kill_grace_seconds=2.0, file_output_path=None, parent_create_time=None, file_output_cap_bytes=None, supervisor_state_path=None)
Wait for a spawned bash -c command the #406-safe way (GH #410).
proc.communicate() waits for pipe EOF, which a backgrounded grandchild
that inherited stdout/stderr can hold open long after the parent exited;
the caller then burns its whole timeout and misreports a clean exit as a
failure. This helper owns both piped conditions and file-redirected flow
actions (the CLI instrument path has its own richer variant in
cli_backend). Piped mode, used by skip_when and
command_succeeds, follows this sequence:
- drain both pipes concurrently while waiting for the PARENT's exit
(
proc.waitpolled on returncode, not pipe EOF), bounded bytimeout_seconds; - after the parent exits, give the drains
post_exit_drain_grace_secondsto reach EOF; a pipe still open after that belongs to a descendant; - on every exit path, SIGTERM -> grace -> SIGKILL the captured group
(
safe_killpgrefuses our own group) and reap identity-matched descendants, so nothing outlives the call.
File-output mode waits only for parent exit and captures same-group members,
detached descendants, and log holders by process identity. It terminates
survivors without waiting for a pipe drain. The returned stdout and
stderr are empty in that mode; the caller owns the log file.
Source code in src/marianne/utils/process.py
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 | |