Dista · API

dista.signals

SIGTERM: the head exits, and a worker drains, even as a container's PID 1.

Kubernetes stops a pod with SIGTERM, and the kernel drops any signal sent to a container's PID 1, as dista's process is, unless it has a handler for it. The head's handler exits at once, as the signal's default action would. A worker's writes a byte to a pipe its event loop polls, so the worker can drain: finish what it's running, hand what it holds to other workers, and leave (see worker.mojo). A second SIGTERM, or one that arrives before the worker's loop runs, ends it at once.

A signal handler can't reach a Mojo variable, so the pipe's write end is duplicated onto a fixed descriptor, WAKE_FD, which the handler writes to.

Functions

exit_on_sigterm

def exit_on_sigterm()

Makes SIGTERM end this process, even where it would be dropped.

wake_on_sigterm

def wake_on_sigterm() -> c_int

Makes SIGTERM write a byte to a pipe, and returns its read end; or -1, with SIGTERM ending the process, if WAKE_FD is taken or the pipe can't be made. Both ends block (setting O_NONBLOCK needs a variadic fcntl, which external_call can't make on Apple's arm64), so read it only once poll says it's readable.

take_wakes

def take_wakes(fd: Int32) -> Int

Reads, once, the bytes SIGTERMs wrote to fd, which poll found readable; how many there were.

Aliases

comptime SIGTERM = Int32(15)
comptime WAKE_FD = Int32(250)

The descriptor a worker's handler writes to: below macOS's default limit of 256 open files, and above what a process has open when it starts.