Skip to content

feat(node): heartbeat the systemd watchdog from the runtime and test serve-loop starvation - #2145

Merged
alpergundogdu merged 2 commits into
mainfrom
fix/runtime-starvation-watchdog
Sep 24, 2026
Merged

alpergundogdu merged 2 commits into
mainfrom
fix/runtime-starvation-watchdog

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Problem

A serve-loop iteration that neither awaits nor makes progress holds a tokio worker. Before #2133, a sliver voucher put deliver in exactly that state (#2132). Once every worker is held:

  • the node serves nothing, logs nothing, and answers no /metrics scrape;
  • the process keeps running, so its systemd unit stays active and nothing restarts it.

#2133 fixed the accounting, and the no-progress guard now fails the stream instead of letting it spin. Two gaps remain:

Fix

crates/node/src/systemd_watchdog.rs: a runtime-liveness heartbeat for the systemd service watchdog.

  • When systemd passes WATCHDOG_USEC and NOTIFY_SOCKET, a task on the tokio runtime sends WATCHDOG=1 every half period.
  • The heartbeat runs on the runtime, so it stops when no worker is free. systemd then kills the node at the deadline and Restart= brings it back.
  • It adds no node setting. The unit's WatchdogSec= turns it on and sets the period, and without WATCHDOG_USEC it never starts.
  • It honors WATCHDOG_PID and supports both path and abstract (@) sockets. The socket is non-blocking, so a full queue cannot block a worker.
  • main spawns it first, so it also covers bring-up and shutdown drain.
  • The unit must set NotifyAccess=main and allow AF_UNIX in RestrictAddressFamilies.

a_sliver_voucher_does_not_starve_the_runtime (crates/node/tests/client_loopback.rs):

Considered and left out:

  • An in-process watchdog thread that aborts: systemd already does this. Under a container supervisor, an HTTP health check on /metrics sees the same hang, because that listener runs on the starved runtime.
  • A node config knob: WatchdogSec= is the knob.

Verification

  • The new test fails in about 2 s against 5ca3e7f1^, the parent of fix(node): keep a partly paid chunk owed so the serve loop cannot spin (#2132) #2133 (the runtime worker ticked 0 times in 1 s), and passes on main (10/10 stress runs).
  • systemd_watchdog unit tests: env parsing (period, missing or zero values, PID match) and a round trip over a real UnixDatagram.
  • cargo nextest run -p decdn-node: 1270 passed.
  • Workspace cargo clippy --all-targets --all-features -D warnings, cargo fmt --check and cargo doc -D warnings are clean.

🤖 Generated with Claude Code

…serve-loop starvation

A serve loop that goes around without awaiting holds a tokio worker. Once
every worker is held, the node serves nothing, logs nothing and answers no
/metrics scrape, but its systemd unit stays `active`, so nothing restarts it.
#2133 fixed the sliver-credit loop that did this (#2132). This change makes
the failure class recoverable and pins the fixed case.

- `systemd_watchdog`: when systemd passes `WATCHDOG_USEC` and
  `NOTIFY_SOCKET`, a runtime task sends `WATCHDOG=1` every half period. The
  heartbeat runs on the runtime, so it stops when no worker is free, and
  systemd restarts the node. No node setting: the unit's `WatchdogSec=` turns
  it on. Honors `WATCHDOG_PID`; supports path and abstract sockets; the socket
  is non-blocking so a full queue cannot block a worker.
- `a_sliver_voucher_does_not_starve_the_runtime`: runs the #2132 sliver case
  on a one-worker runtime and fails if a heartbeat task on it stops ticking.
  It fails in ~2 s against the parent of #2133 (0 ticks) and passes on main.
  A starved runtime is leaked, not dropped, because the drop would wait
  forever for the worker to yield.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The change is self-contained, matches the stated operational goal, and includes targeted unit/integration coverage; remaining feedback is a minor performance refinement.

Review effort: Lite
Findings: 1 Medium severity

Open (1)
What changed in this PR

This PR adds a systemd watchdog heartbeat that runs on the Tokio runtime so systemd can restart decdn-node when the runtime is wedged (e.g., all workers busy-spinning), and it adds a regression test that detects serve-loop starvation on a single-worker runtime.

Changes:

  • Add a systemd_watchdog module that reads WATCHDOG_USEC/NOTIFY_SOCKET and sends WATCHDOG=1 at half-period from a runtime task.
  • Spawn the watchdog heartbeat at the start of decdn-node bring-up to cover initialization and shutdown drain.
  • Add a loopback regression test that fails if a sliver-voucher scenario causes the runtime heartbeat to stop ticking.
File Description
crates/​node/​src/​systemd_watchdog.rs Implements env-parsed watchdog heartbeat + unit tests for parsing and datagram send.
crates/​node/​src/​main.rs Spawns the watchdog heartbeat early in main.
crates/​node/​src/​lib.rs Exposes the new systemd_watchdog module.
crates/​node/​tests/​client_loopback.rs Adds a single-worker starvation regression test keyed to the sliver-voucher case.
CHANGELOG.md Documents the runtime watchdog heartbeat feature and the new regression test.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread crates/node/src/systemd_watchdog.rs Resolved
@github-code-quality

github-code-quality Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Code Coverage Overview

Languages: Rust

Rust / code-coverage/cargo-llvm-cov

The overall line coverage in commit 20e9b45 in the fix/runtime-starvati... branch remains at 85%, unchanged from commit 869141e in the main branch.

Show a line coverage summary of the most impacted files.
File main 869141e fix/runtime-starvati... 20e9b45 +/-
crates/node/src...s/client/mod.rs 96% 95% -1%
crates/protocol/src/framing.rs 97% 97% 0%
crates/node/src...gin/pull_leg.rs 88% 89% +1%
crates/node/src...emd_watchdog.rs 0% 66% +66%

Updated September 24, 2026 10:57 UTC

@alpergundogdu
alpergundogdu merged commit 9a4de88 into main Sep 24, 2026
51 of 53 checks passed
@alpergundogdu
alpergundogdu deleted the fix/runtime-starvation-watchdog branch September 24, 2026 11:00
alpergundogdu added a commit to decdn/devops that referenced this pull request Sep 24, 2026
* feat(decdn_node): enable the systemd watchdog on the node unit

decdn-node heartbeats WATCHDOG=1 from its tokio runtime when the unit sets
WatchdogSec (decdn/decdn#2145). A node whose runtime workers are all held
by work that never yields stops the heartbeat, and systemd restarts it
instead of leaving it `active` while it serves nothing.

- WatchdogSec={{ decdn_node_watchdog_sec }} (default 60) plus
  NotifyAccess=main, so systemd accepts the main PID's heartbeat.
- AF_UNIX joins RestrictAddressFamilies, since the heartbeat goes over
  NOTIFY_SOCKET.
- decdn_node_watchdog_sec: 0 omits all three, for a binary that predates
  the heartbeat and would otherwise be killed every period.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(decdn_node): make the systemd watchdog opt-in

The role cannot tell whether the binary it installs heartbeats, and a
binary that does not is killed every period, so decdn_node_watchdog_sec
defaults to 0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants