Skip to content

Operations: readiness, shutdown, the queue cap and upgrades

This page says how a running qix server tells you it is ready, what it does when it is stopped, how to put a rank to sleep and wake it, what it does when too many requests wait, and what a package upgrade keeps. Every statement names the code that does it.

Terms used on this page:

  • Rank: one python -m tnd.serve process with its own HTTP port.
  • Copy: one rank (or one lockstep group of ranks) that serves requests on its own.
  • Node router: tnd-node-router, the front end qix-serve starts when it runs more than one rank on a host. Clients talk to the router; the router talks to the copies.
  • Supervisor: qix-serve itself, when it starts several ranks and the router and waits on them (csrc/serve_auto/launch.cpp).
  • EP convoy: the expert-parallel mode, where several ranks run each step together.
  • Packaged build: an install made from a qix .deb package, with its systemd units. That packaging is not in the open source tree. A passage marked "packaged build only" does not apply to a build from source or to a pip install.

Readiness

A single rank

A rank opens its HTTP port only after the model is loaded. python -m tnd.serve builds the server after the weights load (python/tnd/serve.py:5747) and the server binds and listens when it starts serving (csrc/server/py_module.cpp:350). Before that, a connection to the port is refused.

Once the port is open, GET /health answers 200 {"status":"ok","model":"<id>"} (csrc/server/http_server.cpp:1035).

Several copies behind the node router

The router listens at once, before the copies are up. Until every copy has passed one health probe, the router refuses work:

  • GET /health answers 503 with "status":"starting", a detail line that says how many copies are up, and Retry-After: 2 (csrc/distributed/node_router.cpp:1872, csrc/distributed/node_router.cpp:1875).
  • POST /v1/chat/completions and /v1/completions answer 503 with Retry-After: 2 (csrc/distributed/node_router.cpp:1461, csrc/distributed/node_router.cpp:1342).
  • GET /v1/models answers the same way (csrc/distributed/node_router.cpp:1882).
  • GET /metrics is served during start-up. The router's own gauges say how many copies answered (see metrics.md).

The router stops waiting for a copy whose process has exited, or after 3600 s (--ready-deadline, csrc/distributed/node_router.h:140). It then serves with the copies it has. The gate applies only when there are two or more copies.

After start-up, the router probes each copy's /health every 2 s (csrc/distributed/node_router.h:338). A copy that does not answer 200 is marked dead (csrc/distributed/node_router.cpp:1966) and gets no new requests. A later probe that gets 200 marks it alive again (csrc/distributed/node_router.cpp:1956-1958).

Use the router's /health as the load balancer's readiness check. Wait for 200.

Shutdown: SIGTERM and the drain

What a rank does on SIGTERM

On SIGTERM a rank drains before it stops. On SIGINT (Ctrl-C) it stops at once (drain_then_stop, python/tnd/serve.py:644-683).

The drain, in order:

  1. /health answers 503 {"status":"draining"} (csrc/server/http_server.cpp:1046-1049). A router or load balancer that probes it stops sending new work to this rank.
  2. A new inference request is refused with 503 and the error engine unavailable. The request is not queued (csrc/server/serve_loop.cpp:1206, csrc/server/http_server.cpp:1393-1410).
  3. The listener stays open. Every request already accepted keeps running until its last byte, a stream's final chunk included, is written (csrc/server/http_server.cpp:64, the DrainGate).
  4. The rank waits until no inference request is in flight, or until the grace cap passes (csrc/server/py_module.cpp:362-371).
  5. The rank then stops as it did before the drain existed: the HTTP server closes, and the requests still in flight are aborted (python/tnd/serve.py:682).

The rank logs the result: either every request finished, with the time it took, or the number of requests still in flight when the cap passed.

How long the grace cap is

The cap is computed, not set. Its inputs are the two stop deadlines a rank must beat (csrc/serve_auto/stop_deadlines.h):

Value How it is computed Result Line
supervisor stop wait 300 polls x 100 ms, then SIGKILL 30 s csrc/serve_auto/stop_deadlines.h:23
router stop deadline the router exits by itself after this 20 s csrc/serve_auto/stop_deadlines.h:27
outer bound the earlier of the two 20 s csrc/serve_auto/stop_deadlines.h:32
teardown margin the gap between the two 10 s csrc/serve_auto/stop_deadlines.h:41
drain grace cap outer bound minus teardown margin 10 s csrc/serve_auto/stop_deadlines.h:48

The teardown margin is not measured. It is time kept back for the rank to stop HTTP, join its driver thread and leave the interpreter after the drain. A rank prints the cap and its inputs once at start-up (csrc/server/py_module.cpp:326-334).

Packaged build only. The systemd unit tnd-server allows 30 s between SIGTERM and SIGKILL (TimeoutStopSec=30). The unit tnd-server-tp allows 90 s.

When there is no drain

  • EP convoy. Under the expert-parallel convoy the ranks leave together, so a rank does not drain. SIGTERM aborts the requests in flight (csrc/server/serve_loop.cpp:3386-3390). The rank logs shutdown drain: off under the EP convoy at start-up (csrc/server/py_module.cpp:336).
  • Behind the node router. When qix-serve runs several copies, it sends SIGTERM to every process at the same moment, the router included (csrc/serve_auto/launch.cpp:331). On SIGTERM the router closes its listener and cuts every request it is forwarding (csrc/distributed/node_router_main.cpp:255, csrc/distributed/node_router.cpp:2063). The copies drain, but a client that came through the router has already lost its connection. In this release, a client of a multi-copy launch is not protected by the drain.
  • SIGINT. Ctrl-C stops at once.

To take one copy out of service, send SIGTERM to that copy's process, not to qix-serve. This works only in the replicas layout. There the supervisor logs exited; the router reroutes its traffic and keeps the other copies serving (csrc/serve_auto/launch.cpp:401). In the ep layout a rank that exits makes the supervisor log exited while serving -- tearing the launch down and stop every process on the node (csrc/serve_auto/launch.cpp:404). Under the EP convoy a rank does not drain at all (csrc/server/serve_loop.cpp:3386-3390).

In the replicas layout, the copy's /health turns 503 and the copy finishes what it has for up to 10 s. Three things still hold:

  • The supervisor never restarts the copy. The node serves with one copy fewer until qix-serve is restarted.
  • From the SIGTERM until the router's next probe (up to 2 s), the router still sends new requests to that copy. The copy refuses them with 503 engine unavailable, and the router passes that answer to the client without trying another copy (csrc/distributed/node_router.cpp:1643). This answer carries no Retry-After header.
  • After the probe, the router marks the copy dead and sends new requests to the other copies.

Sleep and wake

A rank can free its GPU memory without exiting, and take it back later. Three routes do this, on the rank's own HTTP port (csrc/server/http_server.cpp:1081-1119; the state machine is csrc/server/sleep_control.h). The node router does not forward them.

Route What it does
POST /sleep?level=1 Level 1, the default: copies the weights to host memory, frees their GPU storage, and drops the KV cache.
POST /sleep?level=2 Level 2: drops the weights and the KV cache. Waking reloads the weights from the model source.
POST /wake_up Restores what the sleep released, at the level it slept with, and takes requests again. A ?tags= parameter is ignored: qix wakes weights and KV cache together.
GET /is_sleeping {"is_sleeping": <bool>, "state": "<state>"}, plus level while asleep. is_sleeping is true only in the state sleeping.

What POST /sleep does, in order (csrc/server/sleep_control.cpp:104-166):

  1. It refuses new inference requests, the same way the shutdown drain does.
  2. It waits for the open responses to finish and for the step loop to stop, for at most the drain grace cap (10 s, above). If they do not finish in time, the rank stays awake, keeps serving them and answers 409.
  3. It drops the prefix cache.
  4. It releases the GPU memory at the level asked for.

The states are awake, draining, quiesced, sleeping and waking. In every state but awake:

  • an inference request gets 503 with "code":"engine_sleeping" and a Retry-After header (csrc/server/http_server.cpp:243-250);
  • GET /health answers 503 with the state name as status and a Retry-After header (csrc/server/http_server.cpp:1037-1045). Behind the node router, the next probe marks the copy dead, and the router sends new requests to the other copies until the copy answers 200 again.

Retry-After is the time the last wake took, and 1 s before any wake was measured (csrc/server/sleep_control.cpp:95-98).

Each call blocks until the transition is done. The answer is JSON: status, seconds, level when one applies, and error with a message when the call failed.

Status When
200 The transition ran. status is sleeping or awake. A call that changes nothing also answers 200, with already_sleeping or already_awake.
400 level is not 1 or 2.
403 The peer is not a loopback address and the rank runs without --sleep-remote.
409 The rank cannot stop taking requests now (it runs under the EP convoy, or a shutdown drain is under way), or the open requests did not finish within the cap. The rank stays awake.
500 The release or the restore failed. A failed release is undone and the rank stays awake; if the undo fails too, the rank stays asleep and refuses requests until a wake succeeds. A failed restore leaves the rank asleep.
501 This rank cannot sleep. The body says why.

A rank answers 501 when one of these holds (sleep_unavailable_reason, python/tnd/sleep_mode.py:267-287):

  • it is part of a tensor-parallel or expert-parallel group;
  • its KV host tier is on;
  • its window KV ring is on;
  • its model keeps per-request state slots;
  • its model holds state outside its parameters and buffers.

The rank logs at start-up whether sleep is on, and the reason when it is off (python/tnd/serve.py:616-641).

The sleep routes answer loopback peers only: 127.0.0.0/8, ::1 and ::ffff:127.x.y.z. The check reads the socket's peer address, never a header. --sleep-remote on tnd.serve lets any peer call them (python/tnd/serve.py:1675-1681). This is a peer-address check, not authentication; see trusted-network.md.

The queue cap

A rank holds at most a fixed number of waiting requests. A waiting request is one that was accepted but has no place in a batch yet. A request that arrives when the waiting set is full is refused, not queued.

How the cap is derived

At start-up, and again each time the KV pool grows, the rank computes (csrc/server/serve_loop.cpp:1275-1280):

queue cap = max(max_num_seqs, (GPU KV blocks + host-tier block slots) / max_blocks_per_seq)

The second term is the number of sessions the KV pool holds when each one is at the per-sequence cap. Every input comes from auto-config. There is no setting for the cap. The rank logs the cap and each input on a line that starts with [tnd] auto-config: queue cap = (csrc/server/serve_loop.cpp:1289).

What a refused request gets

HTTP 503 with a Retry-After header and a JSON error (csrc/server/http_server.cpp:226-239):

{"error": {"message": "...", "type": "service_unavailable", "code": "queue_full",
           "requests_ahead": 512, "queue_cap": 512, "retry_after_ms": 4200,
           "deadline_ms": 0, "waited_ms": 0}}
  • Retry-After is retry_after_ms rounded up to whole seconds (csrc/server/http_server.cpp:237).
  • retry_after_ms is the requests ahead times the measured mean time per admission. Before the first admission nothing is measured, and it is 1000 ms (csrc/server/serve_loop.cpp:1227-1229).
  • Nothing is queued. Retrying after Retry-After is safe.

The node router passes a copy's 503 and its Retry-After to the client (csrc/distributed/node_router.cpp:1646).

Requests with a deadline

A client may send X-Request-Deadline-Ms: <ms> (csrc/server/http_server.cpp:194). Two more refusals use it:

  • At submit, when the predicted wait is past the deadline: 503, code: "queue_full", Retry-After (csrc/server/serve_loop.cpp:1232-1247).
  • While waiting, when the deadline passes before the request is placed: 503, code: "queue_timeout", Retry-After (csrc/server/http_server.cpp:253-260).

How these refusals are counted is in metrics.md.

Request priority

A request may carry priority, an integer; lower runs first, and the default is 0 (openai-server.md). A negative value from a client is clamped to 0 (csrc/server/openai_schema.cpp:401-417).

Upgrades and the listen address

Packaged build only. This whole section describes the .deb package and its systemd units. A build from source has no package scripts and no unit; its listen address is the --host you pass, default 127.0.0.1.

Older packages listened on 0.0.0.0 by default. This release listens on 127.0.0.1 (TND_HOST=127.0.0.1 in the shipped /etc/tnd-server/tnd-server.conf). An upgrade does not silently move a server off its network address:

  1. Before the new files unpack, the package saves the TND_HOST the host was running with. When tnd-server.conf set no TND_HOST, it saves the default the old launchers used.
  2. After the unpack, when the saved value differs from the new file's value, the package writes it to /etc/tnd-server/tnd-server.conf.d/host.conf and prints a NOTICE that names the value and the file.
  3. The units read host.conf after tnd-server.conf, so the kept value wins. tnd-serve-tp-launch reads it too when run by hand.
  4. tnd-server.conf itself is not edited by the package.

To move to loopback after an upgrade, delete host.conf and restart the service. A later upgrade prints a NOTICE while host.conf still sets a non-loopback address.

A fresh install has no saved value and listens on 127.0.0.1.