Server lifecycle

Starting before LinuxCNC

The server does not need LinuxCNC to be running when it starts. It looks for LinuxCNC at once, then after 1, 2 and 4 seconds, then every 5 seconds, and connects within a few seconds of LinuxCNC coming up. Pass --require-linuxcnc to make it exit with status 1 instead when LinuxCNC is not running.

The server looks for LinuxCNC from a separate process, so waiting does no harm. If LinuxCNC goes away between that check and the connection, the server process cannot connect again. After 3 such failures, or 10 failed starts in a row of its command helper, it logs an error and exits with status 1. Run it under a supervisor that starts it again (see systemd service). A program that embeds the server gets a callback instead (see Embedding).

What clients see while it waits

  • Health is NOT_SERVING for "" and linuxcnc.LinuxCNCService, and SERVING for hal.HalService.
  • GetStatus, SendCommand, WaitComplete, StreamStatus, StreamErrors, ResolveFile and GetMachineConfig fail with UNAVAILABLE and the trailing metadata entry linuxcnc-grpc-reason: not-connected (see Errors). Two check something else first: StreamErrors with --error-channel off fails with FAILED_PRECONDITION, and ResolveFile with an invalid file name fails with INVALID_ARGUMENT.
  • UploadFile, ListFiles, ReadFile and DeleteFile work, so programs can be uploaded before LinuxCNC is up. GetServerInfo works too and reports linuxcnc_connected = false.
  • The HAL service answers, with no pins until LinuxCNC has loaded its HAL configuration.

When LinuxCNC stops

A normal shutdown stops the server

LinuxCNC unloads every HAL component when it shuts down. The server owns one, so it is signaled and exits with status 0. A server meant to outlive LinuxCNC sessions needs a supervisor that restarts it after a clean exit too: Restart=always, not Restart=on-failure.

A crashed LinuxCNC is often not detected

When LinuxCNC’s task is killed, its last status stays readable. The server keeps running and returning that stale status, and health stays SERVING. A frozen task (one stopped in a debugger, say) looks the same.

A command sent then waits about 5 seconds and answers RCS_DONE with acknowledged false (see A LinuxCNC task that does not respond). A WaitComplete on it returns RCS_EXEC at its timeout. Treat a WaitComplete that times out on a command that should be quick as a warning sign. The next start of LinuxCNC after a crash normally stops the server, as a normal shutdown does. The one crash the server does detect is case 4 below.

When the server finds its LinuxCNC gone

The server does not reconnect in the background to a LinuxCNC that has shut down or been restarted (for the one reconnect an RPC can make, see Reconnection). It logs an error, turns every health service NOT_SERVING and exits with status 1, so a supervisor starts a fresh process that connects to the LinuxCNC that is running.

The server counts its LinuxCNC as gone when any of these holds:

  1. The shared memory it is attached to was removed, as LinuxCNC does when it shuts down. The server checks only on Linux, and only when it shares LinuxCNC’s IPC namespace: once after connecting, before every command that is not a stop, and every 2 seconds. If it cannot read LinuxCNC’s NML file it logs a warning and checks only the shared memory of HAL and the realtime system.
  2. Five status reads in a row, over at least 2 seconds, fail with an error other than LinuxCNC’s own linuxcnc.error.
  3. After a command that is not a stop, the status turns out not to belong to the LinuxCNC that took the command.
  4. A process died while it held the status buffer, or the error buffer while the server reads the error channel (see A task stopped while it holds the status).

An idle server, with no client connected, notices removed memory within about 2 seconds and a status that fails for good within about 10 seconds. Until the server has stopped, clients see this:

  • The RPCs that need LinuxCNC, and every HAL RPC, fail with UNAVAILABLE and the trailing metadata entry linuxcnc-grpc-reason: linuxcnc-gone. Open StreamStatus, StreamErrors and HAL streams end the same way at once.
  • A WaitComplete already waiting answers RCS_ERROR.
  • A command part-way through its messages sends no more of them and answers RCS_ERROR with not_sent false, because part of it went out.
  • A stop is still sent, through a new command helper that attaches to the LinuxCNC running now. Its acknowledged is unset, and it gets RCS_ERROR with not_sent true if none is running. With --in-process-commands a stop is refused like every other command.
  • GetServerInfo reports linuxcnc_connected = false, and the file RPCs keep working.

LinuxCNC’s serials and the server’s error sequences start again from 1 after a restart. Compare them only between messages with the same, non-empty connection_id (see Telling servers apart).

Stopping a server that is still attached to an old LinuxCNC does not harm the restarted one. A program of your own that uses LinuxCNC’s Python module and outlives a LinuxCNC restart can: when it frees a command, status or error channel object, or exits normally while it holds one, no program can use that channel of the new LinuxCNC.

Reconnection

The server connects once and does not reconnect in the background. The one exception is a status read in GetStatus or StreamStatus that raises LinuxCNC’s own error (linuxcnc.error) other than the read timeout, which is rare. That RPC then reconnects, unless the server has already found its LinuxCNC gone. If LinuxCNC was restarted and no check has found that yet, the reconnect can reach the new LinuxCNC.

Health service

The server answers the standard gRPC health service (grpc.health.v1.Health) for three names. "" (the server as a whole) and linuxcnc.LinuxCNCService are NOT_SERVING while the server waits for LinuxCNC and SERVING once it has connected. hal.HalService is SERVING from the start. All three turn NOT_SERVING when the server finds its LinuxCNC gone, and when it stops. Check on any other name fails with NOT_FOUND. Check answers at once and holds nothing on the server, so use it for probes.

Watch is limited by the thread pool, 4 watchers by default. Its first message is the current status, or SERVICE_UNKNOWN for a name the server does not know. After that it sends the status only when it changes. When the server stops, each open Watch ends with status OK. A Watch on one of the three names first sends NOT_SERVING, unless that was its last message. A stop without a grace period can cut that message off (see Embedding).

A Watch over the limit ends at once with UNIMPLEMENTED. A client that uses gRPC’s client-side health checking (a channel with a healthCheckConfig) then treats the server as healthy and stops health checking on that connection. It does not start again when a slot frees, so it will not see a later NOT_SERVING. A client that must follow the status should poll Check.