feat(ops): Deadline and StopOnSignal end a run cleanly at the next step

Open
#385 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend

Research direction

Start with src/pipekit_ops/operators/_flow.py and the existing Timeout, RateLimit, and Until operators; read the P8/P9 work referenced by the issue and the P10 roadmap entry. Then check the operator exports, docs/design/api/operators.md, and the tests described as G10-1 through G10-6. Done means both operators are exported, documented, tested, and P10 is marked built in the roadmap.

Written by the indexing model from the issue text.

Description

area:code priority:p2 type:feature

Problem / Request

pipekit-ops can bound one call (Timeout) but not a whole run, and cannot stop a run cleanly when a batch scheduler sends a signal.
This issue adds two pass-through operators, Deadline(seconds) and StopOnSignal(signals), which end the loop around them by raising Stop(resumable=True) at the next call after the budget is spent or the signal arrived.
It is phase P10 of packages/pipekit-ops/docs/design/roadmap.md.

User Story

As someone running a long simulation on a preemptible node, I want the run to stop at the next chunk edge when its wall-clock budget runs out or the scheduler sends SIGTERM, so that the last good state is checkpointed and the run can resume.

Motivation

Two of sesmax-experiment's monitors read nothing of the model (monitor/builtins.py @ 12f169f6bf):

  • WatchdogMonitor(max_wall_s) starts a clock at the start of the run and stops the run, resumably, at the first chunk edge after max_wall_s seconds.
  • SignalMonitor(signals=(SIGTERM, SIGINT)) installs handlers at the start of the run that only note the signal, stops the run resumably at the next chunk edge, and puts the previous handlers back at the end. A handler can only be installed from the main thread, so it raises ValueError elsewhere.

They are the run-level partners of Timeout, which abandons one call that hangs.
Any loop on preemptible hardware needs both, so they belong next to Timeout and Until in pipekit_ops.operators.

Proposed API

# pipekit_ops/operators/_flow.py  (proposed, this issue)

class Deadline(Operator):
    """Pass x through; raise Stop(resumable=True) at the first call after `seconds`.

    The clock starts in open() (P9), or at the first call if never opened.
    """
    def __init__(self, seconds: float, clock: Callable[[], float] = time.monotonic) -> None: ...
    def _apply(self, x: Any) -> Any: ...

class StopOnSignal(Operator):
    """Pass x through; raise Stop(resumable=True) at the first call after a signal.

    open() installs handlers that only note the signal; close() restores the
    previous handlers. open() from any thread but the main one raises ValueError.
    """
    def __init__(self, signals: Sequence[signal.Signals] = (signal.SIGTERM, signal.SIGINT)) -> None: ...
    def _apply(self, x: Any) -> Any: ...

Stop is from P8 (pipekit-ops, #382): Stop(reason, carrier=None, *, resumable=False, complete=False), a BaseException.
opened and open() / close(ok) are from P9 (#384).

Design Snapshot

from pipekit_ops import Deadline, Sequential, StopOnSignal, opened
from pipekit_cycle import Cycle

step = Sequential([advance, Deadline(6 * 3600.0), StopOnSignal()])
cycle = Cycle(step_op=step, n_steps=10_000)
with opened(step):
    frame, chunk = cycle(frame0, 0)
if cycle.stop is not None and cycle.stop.resumable:
    write_checkpoint(frame, chunk)

The reason of the Stop names the cause: "deadline of 21600 s passed after 21604 s" or "received SIGTERM".
The carrier in the Stop is the guard's input, the frame as far as the step got; placed after advance, it is one step ahead of the state Cycle returns, which is that of the last finished step.
StopOnSignal refuses to run inside a Map thread or process worker, because handlers belong to the main thread; its docstring says to place it in the outer loop.

References & Existing Code

  • Roadmap: packages/pipekit-ops/docs/design/roadmap.md, phase P10 (merged in #374).
  • WatchdogMonitor and SignalMonitor in sesmax-experiment, linked above.
  • Timeout, RateLimit and Until in pipekit_ops/operators/_flow.py @ be1b64a900; RateLimit has the _clock hook to copy for tests.

Implementation Steps

  • Add Deadline and StopOnSignal in src/pipekit_ops/operators/_flow.py.
  • Re-export them from pipekit_ops.operators and pipekit_ops.
  • Neither stores its clock, handlers or received signal in get_config, so a loaded copy starts fresh.
  • Mark P10 as built in the roadmap.

Definition of Done

  • Code lands at the path above
  • Public API exported via pipekit_ops/__init__.py
  • Tests pass: make test
  • Lint and typecheck pass: make lint && make typecheck
  • Google-style docstrings; the StopOnSignal example sends SIGTERM to its own process with os.kill
  • pipekit-ops still imports only the standard library

Testing

  • G10-1: under opened, with a fake clock that reads 0 at open() and k s at call k, Deadline(2.5) returns its input unchanged (is) on calls 1 and 2 and raises Stop with resumable=True on call 3.
  • G10-2: under opened, after os.kill(os.getpid(), signal.SIGTERM), the next call of StopOnSignal() raises Stop whose reason names SIGTERM.
  • G10-3: after opened exits, normally or by an exception, the handler of each signal is the one that was there before.
  • G10-4: StopOnSignal().open() called from another thread raises ValueError.
  • G10-5: under opened and with the clock of G10-1, a Cycle of 10 steps whose step is Sequential([op, Deadline(2.5)]) returns the state after step 2 with cycle.stop.resumable true.
  • G10-6: both operators pass assert_round_trip.
  • Deadline never opened starts its clock at its first call.

Documentation

  • docs/design/api/operators.md lists both next to Timeout, with when to use which.

Relationships

  • Parent (theme epic): #379
  • Blocked by: #382, #384, and #383 for gate G10-5
  • Blocks: none
  • Related: #381
Dominant language
Python
Stars
0
Forks
0
Avg merge
3h 50m
Merged PRs (30d)
143

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from jejjohnson/pipekit

All issues in jejjohnson/pipekit

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.