A LinuxCNC task that does not respond
LinuxCNC’s task process takes the commands that clients send. This page describes what clients see when it stops answering: it hangs, is stopped in a debugger, or does not get the CPU.
A task that does not answer commands
SendCommand answers after about 5 seconds with RCS_DONE, a linuxcnc_serial
and acknowledged false. LinuxCNC waits that long for the task to confirm each
message, then returns without an error. A command sent as two LinuxCNC messages,
such as coolant, takes about 10 seconds.
The command is not withdrawn. It stays in LinuxCNC’s queue and runs when the task runs again, unless that LinuxCNC is shut down or restarted first. A client deadline does not withdraw it either: a command that has been written is carried out even if its client has given up.
One command at a time, stops first
One command at a time has the turn. When it is free, a waiting stop takes it if there is one, and otherwise the command that has waited longest. So a command that arrives while another waits for the task waits for its turn first. Stops go first lists the stops and what one changes for the other commands.
Having the turn is not yet being sent. The server checks once more that the
client is still there, then hands the command’s messages to LinuxCNC one call at
a time. After that check a client that gives up changes nothing, but a stop that
arrives keeps back the messages not yet handed over (see Commands refused
because of a stop), and so does the server
stopping. program open is two messages in one call
and is never split.
A command waiting for its turn is answered RCS_ERROR with not_sent true,
without being sent, if its client goes away, the server stops or a stop arrives.
A stop waiting for its turn is never dropped. Every waiting command occupies one thread of the
SendCommand pool (16 by default, see Thread pool).
Do not make the server the machine’s only way to stop. A stop sent to a task that does not respond is not carried out until the task runs again. The E-stop chain of a machine belongs in its hardware and HAL configuration.
What acknowledged means
CommandResponse.acknowledged is an optional field. It is true or false only
when the command was dispatched and a status read after it succeeded. A 1.0
server never sets it.
true: a status read made after the send showed the command’s serial echoed. The task has taken the command and may still be executing it.false: the command was written to LinuxCNC’s queue, but the task had not taken it after LinuxCNC’s 5-second wait. The server logs a warning. Before you rely on the command or send it again, read the status:task.echo_serial_numberreacheslinuxcnc_serial, then the state you expect.- unset: the server cannot tell. Nothing was dispatched, or no good status read was made after the send. That covers a status that could not be read, a reconnect between the send and the read, and a status that belongs to another LinuxCNC (see When the server finds its LinuxCNC gone).
RCS_DONE keeps its meaning, “dispatched”, for stops too. For a command sent as
several LinuxCNC messages, acknowledged describes the last one.
The command helper keeps other RPCs answering
LinuxCNC’s send call lets no other Python thread of its process run while it
waits. The server therefore sends commands from a child process, its command
helper. With the helper, GetStatus, StreamStatus, GetServerInfo, health,
the file RPCs and the HAL service keep answering during a blocked send. An
untargeted WaitComplete made meanwhile answers RCS_EXEC at its timeout, as
for any command still in progress; one that names a linuxcnc_serial follows
that command (see WaitComplete).
The helper ignores SIGTERM and SIGINT, so a signal sent to a group of
processes cannot stop it in the middle of a write to LinuxCNC. It ends when the
server stops or dies, after finishing a send in progress.
--in-process-commands, or create_server(command_helper=False), sends
commands from the server’s own process, as versions up to 1.0 did. A command
sent to a task that does not respond then stalls every RPC of the server for as
long as the send waits. A LinuxCNCServiceServicer built directly in Python
sends from its own process unless it is given command_helper=True.
When the helper is lost
If the helper dies, does not start a call within a second, or does not answer
within 30 seconds, the server ends it and starts another before the next command. Health stays SERVING, because the server’s
connection to LinuxCNC is unchanged. A command the helper may already have
written is answered RCS_ERROR with the message that it “may have been sent to
LinuxCNC”: check the status before sending it again. A command the helper never
took, or one sent when no helper can be started, is answered RCS_ERROR with
not_sent true.
A helper that dies inside its call to LinuxCNC can, rarely, leave LinuxCNC’s command or status buffer taken. On Linux the server then logs an error that the channel “may be blocked”. Restart LinuxCNC to clear it; the server does not stop by itself over this.
When the server stops
The LinuxCNC message being written is finished, and the server process exits once it is done.
- The rest of a command sent as several messages is not sent. The command is
answered
RCS_ERRORwithnot_sentfalse, since part of it went out.program openis two messages written in one call, so it is never cut in two. With--in-process-commandsthe rest of a command is still sent. - Commands waiting for their turn are refused with
not_senttrue. A stop already waiting for its turn is still written. - gRPC gives calls 5 seconds to finish. A client whose call is still running
then gets
UNAVAILABLEinstead of an answer, although its message may still be written.
The server sets no overall bound on its own stop. Give it more than 10 seconds: a supervisor that kills it sooner can cut a write short. systemd’s default stop timeout is 90 seconds.
A task stopped while it holds the status
LinuxCNC’s status buffer is guarded by a lock that the task takes for a moment
in every cycle. If the task is stopped in that moment (a debugger, SIGSTOP,
Ctrl+Z in LinuxCNC’s terminal), nothing can read the status until it runs again.
Up to about 2 % of such stops land there.
The server waits 0.1 seconds for the lock, by default, and then answers that the
status is unreadable. It never sends a status it did not just read, and it does
not reconnect: connection_id, sent commands, the serials a WaitComplete can
name and health stay as they are, because LinuxCNC is not gone. On Linux the
status is readable again within milliseconds of the task running. Where the server cannot read the
lock’s state (not Linux, no permission), it retries at growing intervals of up
to 10 minutes, so the status can stay unreadable that much longer.
| RPC | While the task is stopped |
|---|---|
GetStatus, GetMachineConfig, ResolveFile | Fail within about 0.1 s with UNAVAILABLE and the trailing metadata linuxcnc-grpc-reason: status-unreadable. |
StreamStatus | Stays open and sends nothing, so a client that treats a silent stream as dead gives up. |
WaitComplete | Keeps waiting on its command and answers RCS_EXEC at its timeout, or up to one status read wait later. |
SendCommand | Still sends, with the exceptions below. |
GetServerInfo, health, file RPCs, HAL, StreamErrors | Not affected: linuxcnc_connected stays true and health SERVING. |
A command sent during the stop answers after about 5 seconds with RCS_DONE and
acknowledged unset, and is carried out when the task runs again. Two kinds are
refused instead, with RCS_ERROR, not_sent true and the message “LinuxCNC’s
status cannot be read right now”:
- Commands whose joint or spindle number is checked against the machine’s
counts: jog, home, unhome, spindle, spindle override, brake,
set_limitandoverride_config. - Home and unhome of all joints (
-1), which should not go out while nobody can see the machine.
A jog stop and spindle off are still sent, checked only against fixed
bounds. If the task instead
holds the command buffer, a command waits in the helper for as long as the NML
file says (10 seconds in the stock file) and is answered RCS_ERROR with
not_sent true.
A holder that has died
LinuxCNC’s locks are not released when the process holding one ends, so the
buffer stays unreadable until LinuxCNC is restarted. On Linux the server checks
whether the holder still exists. Once it finds the task dead, it treats LinuxCNC
as gone: linuxcnc-gone, health NOT_SERVING and exit status 1, a second or
two after the task died (see When the server finds its LinuxCNC
gone). A holder that is
alive, such as a task stopped in a debugger, never makes the server stop: it
keeps answering status-unreadable. Where the holder cannot be checked (not
Linux, no permission, a container that does not share LinuxCNC’s processes), the
server gives up after about 15 hours and exits the same way.
The error buffer’s lock is also taken by every program that reads LinuxCNC’s
error messages, the GUI included. If a GUI dies holding it, a server that reads
the error channel (always mode, or on-demand with a StreamErrors client)
treats LinuxCNC as gone although it still runs. A supervised server in always
mode, or in auto acting as always, then exits again after every restart.
Restart LinuxCNC in that case.
Changing the wait
--status-read-timeout SECONDS (status_read_timeout= in create_server and
serve) accepts 0.01 to 3600 seconds; the default is 0.1. Raise it if
status-unreadable appears in a client, or “could not be read within” in the
log, while the task is running normally. The server also logs a warning, with
that advice, each time it has had to reopen the status or error channel another
100 times; each reopening keeps some memory until the server exits. For each
read that gives up, LinuxCNC’s library prints SHMEM: Can't take semaphore to
the server’s standard error. Those lines are expected.
--status-read-timeout off (status_read_timeout=None) waits as 1.0 did. A
task stopped while it holds the status lock then stalls the whole server on
every read, for 10 seconds with LinuxCNC’s stock NML file. The server still does
not reconnect.
The server’s copy of the NML file
The wait is set in a private copy of LinuxCNC’s NML file,
linuxcnc-grpc-nml-XXXXXXXX/linuxcnc.nml in $XDG_RUNTIME_DIR or else the
system’s temporary directory, removed when the server exits. The server’s reads,
its command helper and its check for a running LinuxCNC use the copy, and a
program that embeds the server has linuxcnc.nmlfile pointed at it for the
whole process.
- The server copies the NML file named by the INI in
INI_FILE_NAME, which LinuxCNC sets for programs it starts, and otherwise the default of LinuxCNC’s Python module. If you start the server outside LinuxCNC and the machine’s INI names its ownNML_FILE(in[LINUXCNC]or[EMC]), setINI_FILE_NAMEto that INI. The server logs a warning when the two files differ. - Only a local reader’s entry gets the short wait. A
REMOTEorAUTOentry keeps its network timeout, and if the status buffer’s entry is one, no copy is made. - If no copy can be made, the server logs a warning and uses the NML file as it is, with that file’s waits (10 seconds in the stock file).