Run-scoped control surface for cooperative draining.
Intended for a single graph run. Create a fresh RunControl per run; reusing a control after RunControl#requestDrain leaves it drained.
Safe to use from any concurrent context: the drain request is represented by a single field write, so no synchronization is needed for this signal. If more mutable state is added here, add synchronization.
The intended use is hooking SIGTERM (or any external supervisor signal) to RunControl#requestDrain so an in-flight graph run can stop cleanly at the next superstep boundary and be resumed later from the saved checkpoint.
class RunControlimport { RunControl, GraphDrained } from "@langchain/langgraph";
const control = new RunControl();
// In a signal handler, supervisor, etc.:
// control.requestDrain("sigterm");
try {
const result = await graph.invoke(input, { ...config, control });
if (control.drainRequested) {
// finished naturally on the same tick where drain was requested
}
} catch (e) {
if (e instanceof GraphDrained) {
// checkpoint saved; resume later with the same config
} else {
throw e;
}
}