Telling servers and connections apart

LinuxCNC’s command serials and the server’s error sequence numbers start again after a restart, so a number alone cannot say which LinuxCNC or which server it came from. Two values can. Both are opaque strings: compare them, do not parse them. A server older than these fields sends them empty.

server_instance and connection_id

ServerInfo.server_instance is random per server, one per process for the linuxcnc-grpc command. It changes whenever the server is restarted.

connection_id is on ServerInfo, LinuxCNCStatus, ErrorMessage and CommandResponse. It names one connection of one server to LinuxCNC, and it changes on every new connection, also on a reconnect to the same LinuxCNC. It does not change when the server replaces a status or error channel it could not read (unresponsive task). Each message carries the id of the connection it came through. When LinuxCNC is restarted under a running server, the server normally finds it gone, connection_id becomes empty, and the server stops. The next server process has a new server_instance.

Compare serials only within one connection_id

Compare LinuxCNC serials, echo serials and error sequences only between messages with equal, non-empty connection_id. A different or empty connection_id means “not comparable”, not “not taken”.

For example, a stop’s linuxcnc_serial is confirmed by a status with the same connection_id whose task.echo_serial_number is at or past it. “At or past” is LinuxCNC’s own comparison: (echo - serial), wrapped to a signed 32-bit integer, is 0 or more. A plain echo >= serial is wrong once the counter wraps from 2147483647 to -2147483648. This confirms that LinuxCNC’s task read the stop, not that its effect is complete; read task.task_state or trajectory.current_vel for that.

Two answers are not covered by connection_id:

  • A targeted WaitComplete takes only the serial, so check the command’s connection_id before the wait, as WaitComplete describes. A reconnect between that check and the wait is not seen.
  • Treat acknowledged as meaningful only when the response carries a connection_id. A stop sent after LinuxCNC restarted can be judged against the old LinuxCNC’s status.

connection_id is only as good as the server’s checks that the LinuxCNC it reads is the one it writes to (LinuxCNC gone). Those checks are weaker off Linux, and in a container that does not share LinuxCNC’s IPC namespace.

Telling a 1.0 server apart

GetServerInfo reports the server and LinuxCNC versions and whether the server is connected. A 1.0 server does not have it and answers UNIMPLEMENTED. It also lacks the other RPCs and fields added in 1.1, such as ReadFile, ResolveFile, GetMachineConfig and linuxcnc_serial.

A server built from an unreleased commit can report an older or a pre-release version. So do not decide from the version number alone: treat an empty field as “not reported”, and an UNIMPLEMENTED from another RPC as the answer for that RPC.

import grpc

from linuxcnc_pb import linuxcnc_pb2, linuxcnc_pb2_grpc

channel = grpc.insecure_channel("127.0.0.1:50051")
stub = linuxcnc_pb2_grpc.LinuxCNCServiceStub(channel)

try:
    server_info = stub.GetServerInfo(linuxcnc_pb2.GetServerInfoRequest(), timeout=5)
except grpc.RpcError as error:
    if error.code() != grpc.StatusCode.UNIMPLEMENTED:
        raise
    print("linuxcnc-grpc 1.0 server")
else:
    print(f"linuxcnc-grpc {server_info.server_version}, "
          f"LinuxCNC {server_info.linuxcnc_version or 'version unknown'}, "
          f"{'connected' if server_info.linuxcnc_connected else 'waiting for LinuxCNC'}")