Stops go first

A stop is a command that can only stop something: state STATE_ESTOP or STATE_OFF, program abort or pause, jog JOG_STOP and spindle SPINDLE_CMD_OFF. Nothing else counts. Brake engage is not a stop, because braking a turning spindle is not plainly harmless. An override set to zero is not one either.

LinuxCNC has one command queue for all its clients, and nothing overtakes a message once it is written there. The server decides only which of the messages it has not written yet goes next. It sends stops first and refuses the commands a stop overtakes.

A stop sent through the server is no substitute for the machine’s physical E-stop. See What the server cannot do.

A stop takes the next turn

A stop goes ahead of every command still waiting, from any client.

  • A stop is never refused over a field it does not use. A JOG_STOP with a bad velocity and a spindle off with a bad speed are both sent.
  • A stop’s index is checked only against fixed bounds: joints 0 to 15, axes 0 to 8 and spindles 0 to 7. A stop for a joint or spindle the machine does not have goes to LinuxCNC, which ignores a jog stop and posts an error message for a spindle off.
  • The message being sent when the stop arrives is not interrupted. The stop is written as soon as that call returns: when the task echoes the message, or after about 5 seconds while the task does not answer (twice that behind a program open).
  • Among waiting stops, E-stop and machine off go first and the others follow in the order they arrived. An E-stop or machine off passes a stop accepted before it only once, so a stream of E-stops cannot keep a jog stop waiting.
  • Nothing on a stop’s way to LinuxCNC waits for a status read, apart from the process-wide waits in What the server cannot do.

How a stop is answered

A stop is answered like any other command, once its LinuxCNC call has returned: RCS_DONE with linuxcnc_serial. acknowledged is true when the task echoed the stop. It is false when LinuxCNC gave up waiting for the echo after about 5 seconds: the stop is in its queue and not yet taken. It is unset when the status could not be read afterwards or belongs to another LinuxCNC. A stop’s call does not wait for a status read that is blocked, so its answer can come without acknowledged; the hold below is kept as usual.

Rely on acknowledged, and compare linuxcnc_serial with the status, only when the response’s connection_id is not empty and equals the status’s. A stop sent after LinuxCNC was found gone reaches whatever LinuxCNC runs now and has an empty one (see Telling servers and connections apart). Always confirm the stop’s effect in the status: task.task_state for an E-stop or machine off, the velocities for a jog stop.

Behind a command the task does not answer, a stop’s call takes that command’s wait and then its own: about 10 seconds behind a single message, about 15 behind a program open, and longer behind other stops. A client whose deadline is shorter gets DEADLINE_EXCEEDED, and the stop is still written. Once a stop waits for its turn, nothing its client does withdraws it, and a normal server shutdown does not either. That holds from the moment the server has started handling the call: a deadline that ends while gRPC still queues the call ends it unwritten.

The server refuses a stop only before it waits: a negative index or one beyond the fixed bounds gets RCS_ERROR with not_sent true, and while the server is not connected to LinuxCNC the call gets UNAVAILABLE. Once the server has found LinuxCNC gone, a stop is still sent, through a new command helper. A stop that had its turn can still fail: one LinuxCNC did not take, for example because it is not running, or one whose helper cannot be started is answered RCS_ERROR with not_sent true. With --in-process-commands a stop is refused like any command once LinuxCNC is gone, also after it has waited.

Commands refused because of a stop

Commands that are not stops can be refused because of a stop, on a healthy machine too and whichever client sent the stop. The answer is RCS_ERROR with one of these texts in error_message:

error_messageWhennot_sent
A stop was issued; the command was not sentThe command was waiting for its turn, or held, when a stop arrived.true
A stop was issued; the rest of the command was not sentA stop arrived between two messages of coolant, set_limit, override_config or program_options.false
A stop has not been taken by LinuxCNC yet; the command was not sentA stop was written and no status read has shown it taken (see the hold).true
A stop was lost with its helper process and may still be in LinuxCNC's queue; the command was not sent. Sending a stop again clears thisA stop’s helper was lost while writing it, or its send failed unexpectedly after the message may have gone out.true
Too many commands are waiting; the command was not sentMore commands were in progress than the command limit allows.true

A command written after a stop would undo it, so every command that was waiting when the stop arrived is answered at once and not sent. The command being sent at that moment is not refused. A command and a stop that arrive together are decided by which the server started handling first.

A command cut short between two messages has sent its first part, and that part is what WaitComplete follows. program open cannot be cut.

The server does not retry a refused command. Read the status before sending it again: the machine may have stopped in the meantime. not_sent true means nothing was written, so sending it again cannot run it twice.

The hold after a stop

A command written after a stop that the task has not taken yet would run right after that stop. So once a stop’s call has returned without a status read showing it taken, the other commands are held, from every client:

  • A command that arrives then waits for at most 1 second. It reads the status every 50 ms while it waits, and is sent as soon as the stop is shown taken. If the stop is still not taken after that second, the command is refused.
  • Once the oldest stop not yet taken was written more than 1 second ago, every further command is refused at once, after one status read.
  • A held command is overtaken, and refused, by any further stop.

Stops that the task takes quickly only delay commands, however many there are. While stops keep arriving faster than LinuxCNC takes them, every waiting command is refused in turn: a client that streams stops starves the other clients’ commands. Stops themselves are never held or refused.

The hold ends only on evidence, a status read that shows the task has taken the newest stop the server wrote. It never ends because time has passed.

  • A lost stop (the last row of the table above) may still be in LinuxCNC’s queue. It holds until a stop written after it has been taken, or until a reconnect finds a LinuxCNC that started again. Sending a stop again clears this.
  • While the status cannot be read the hold stays. Held commands get the hold’s text, not the status-unreadable refusal.
  • A reconnect to the same LinuxCNC keeps the hold. One that finds a LinuxCNC that started again ends it. The server tells the two apart by the echo serial alone, which cannot prove it: a restarted LinuxCNC whose serial is already past the old value keeps the hold until a stop is taken.
  • A stop that was refused, that LinuxCNC did not take, or that the status cannot judge holds nothing.

An untargeted WaitComplete answers RCS_ERROR after a stop lost with its helper, until the server has sent another command, and after a reconnect while the hold lasts; see WaitComplete.

stops_not_taken

GetServerInfo returns stops_not_taken: the number of stops the server wrote that no status read has shown taken, plus one for each lost stop. While it is above 0, commands that are not stops are held and then refused. The call answers while a send is blocked. It needs a free thread of the general pool, and like every call it waits while a status read waits (see What the server cannot do).

A stop still waiting for its turn is not counted, and neither is one whose call has not returned. The count falls only on a status read. A server started by linuxcnc-grpc or create_server reads the status at least every 2 seconds; a servicer used directly reads only for its clients’ status calls and held commands.

The command limit

Commands that are not stops may use all but two of SendCommand’s own threads, 14 by default (see Thread pool). One more is answered RCS_ERROR with the Too many commands are waiting text above. That is not a gRPC error: RESOURCE_EXHAUSTED is what one stream or WaitComplete too many gets.

Stops are not counted or refused for this limit, but each holds its thread until it is answered, so many stops sent while the task does not answer can fill the pool. If the installed gRPC does not run SendCommand on those threads, the server logs an error once and stops share the general pool.

What the server cannot do

  • LinuxCNC takes a stop only after every message written before it, by this server or by any other program such as a GUI or a pendant.
  • A task that does not run acts on nothing. An E-stop sent to a frozen or hung task takes effect only when the task runs again. Use the machine’s physical E-stop.
  • A command LinuxCNC refuses with an operator message, such as a jog stop sent in E-stop, can stall the task for 5 seconds once 25 error messages are unread. Such a stop holds the other commands for those 5 seconds. A GUI, a StreamErrors client or the server in always mode keeps the queue from filling (see Error messages).
  • A stop is not faster than a status read that waits. While the server reads the status of a task that stopped holding the status buffer, the whole server process waits for up to the status read timeout, and a stop is handed over when the read ends. The default is 0.1 s; keep it. See A task stopped while it holds the status.
  • A stop is not faster than the INI file read either: every call waits while it runs. The server does not start one while the last status shows the machine in cycle, but a cycle that starts just before the read is not excluded. Keep the INI file on local storage (see What the server reads from the INI file).
  • A flood of SendCommand calls delays a stop before the server can tell it from the rest; see the warning in Thread pool.
  • A command helper that stops in the middle of a call delays a stop by the helper’s reply timeout, about 30 seconds. A helper that does not report within a second that it started a call is ended instead. The command is then answered not_sent only when the helper certainly never started the call, and a stop that certainly did not reach it is sent once more through a new helper. A stall while the helper process is being created is not bounded.
  • With --in-process-commands stops still go first, at the cost described in A task that does not answer commands.
  • A stop waiting for its turn is lost if the server process is killed outright (SIGKILL, a crash, power loss). On a normal shutdown (SIGTERM, server.stop()) a stop already waiting is still written, and commands that are not stops are refused. The shutdown waits at most 15 seconds for gRPC’s transport to finish and refuses calls that arrive later, but no bound is given for the shutdown as a whole.
  • A program that embeds the server must keep gRPC’s fork support off; see Fork support.