Repository navigation
fix(serve): report a daemon started once it answers /health - #1332
LeeroyHannigan wants to merge 3 commits into
Conversation
extenddb serve without --foreground watched the daemon for 3 seconds and reported it started if it was still running then. A daemon that failed later, for example on a storage backend that did not answer, was reported started, and serve exited 0. In one run on PostgreSQL, with a catalog address that accepts connections and never answers, serve reported the daemon started after 3.6 seconds. The daemon exited some seconds later. verify_daemon_started now waits until the daemon answers GET /health, which the server serves only after its storage backend has connected. It reuses the probe of extenddb healthcheck and the liveness check of extenddb status. It exits 1 when the daemon exits first. In the run above that was after 13.3 seconds. A daemon that has not answered within 60 seconds is reported started with a warning that names extenddb healthcheck, and goes on starting. Its startup can run longer, for example to rebuild an index after a crash. The 60 seconds are longer than the waits after which a backend's startup gives up. These are 10 seconds for the catalog registration and 30 for the SQLite and PostgreSQL pools. The 60 seconds are less than the 90 seconds that systemd gives a Type=forking unit to start by default. serve checks the address the listener bound and names it in its banner. A daemon started with --port 0 is checked on the port it got. Three tests in tests/test_cli_lifecycle.py cover the wait on PostgreSQL. In one, the catalog address is a listener that never accepts, and serve exits 1 between 3 and 60 seconds. In another, the test holds a lock in the catalog that the daemon's startup waits on. serve then reports the daemon started with the warning after 60 seconds. The third starts a daemon with --port 0 and expects it reported started with no warning and with the port it got. test_init_serve_status_stop checks that a daemon that starts at once gets no warning. The admin guide says what serve waits for, and the upgrade manual has a v1.1.0 entry. The daemon's own error is still only in syslog, as before. Refs #736
LJMart11
left a comment
There was a problem hiding this comment.
Reviewed at 7e994b7.
1 finding is left inline.
| } | ||
|
|
||
| println!("extenddb server started (pid {pid}, {bind_addr})"); | ||
| println!("extenddb server started (pid {pid}, {bound})"); |
There was a problem hiding this comment.
With --port 0 this line now names the bound port, but the PID file is still named for port 0 (cmd_serve.rs:217), so stop --port <the printed port> refuses the daemon and says it was probably started with --foreground.
There was a problem hiding this comment.
Thanks, reproduced. On a PostgreSQL build of 7e994b7, serve --port 0 printed 127.0.0.1:43977 and wrote extenddb-0.pid. Then status --port 43977 gave the pid as unknown, and stop --port 43977 refused the daemon as a --foreground one. Only stop --port 0 stopped it.
Fixed in d34a74c: serve names the PID file for the port the listener bound, so the PID file of a nonzero port keeps its name. test_serve_checks_the_port_a_daemon_bound_with_port_0 now runs status and stop with the printed port, and it fails on 7e994b7 at the status check. The v1.1.0 upgrade entry says that stop --port 0 no longer finds such a daemon.
Main is at 5252eda. The base of this branch is main 8c64333, and main has 25 commits since then. Of the files that this branch changes, main changes only docs/manuals/07-upgrade-manual.md. There, main and this branch each add a v1.1.0 entry at the same place, and git reported a conflict. The merge keeps both entries, main's first.
With --port 0, serve named the bound port in its banner and in the extenddb server started line. The PID file still had the name of port 0, extenddb-0.pid. So stop --port <the printed port> refused the daemon as one started with --foreground, and status --port <the printed port> gave its pid as unknown. Only stop --port 0 found it. serve now names the PID file for the port that the listener bound. For a nonzero port, the bound port is the port of --port or of the configuration, so the name does not change. The server already reads its port from the listener. test_serve_checks_the_port_a_daemon_bound_with_port_0 now runs status and stop with the port of the extenddb server started line. The v1.1.0 entry of the upgrade manual says which port stop, status and catalog-check use for such a daemon. The troubleshooting entry for stop says which port to pass.
demohan
left a comment
There was a problem hiding this comment.
serve reports a daemon started only once it answers /health at d34a74c. On PostgreSQL, a catalog that never accepts gives exit 1 after 13.2 s where main reports a start after 3.0 s; a start blocked for 75 s gives exit 0 after 62.1 s with the warning and the daemon then serves; and a lost registration that is then refused gives exit 1 after 19.3 s where main reports a start after 3.1 s, while one that registers again is reported started after 19.1 s. status and stop work afterwards and while the daemon is still starting, --port 0 prints a port that status and stop use, and dev-mode, ::1 and localhost binds start. All three new tests fail on main's code, removing the liveness check or reverting the PID file name fails its test, and fmt, clippy, 105 app tests and the 1.91.1 check are clean. Merged with #1331 the lifecycle file passes 16 with 1 skipped; the only conflict is text in the upgrade manual.
I will approve once catalog-check refuses beside a daemon started with port = 0.
crates/app/src/cmd_catalog_check.rs:50
With port = 0 in the config, catalog-check and catalog-check --fix run beside a live daemon, where main's code refuses with Server is running (PID ...) and exit 1; stop and status without --port no longer find it either. The PID file now carries the bound port (cmd_serve.rs:217), so the guard looking for the configured port misses it. Refusing while any live extenddb-*.pid is in run_dir, with a test that starts a daemon on port 0 and runs catalog-check --fix, would hold it.
| // the grandchild after the double-fork), then for the daemon | ||
| // to answer /health. This catches a daemon that exits during | ||
| // startup (bad config, missing tables, TLS cert errors). | ||
| return verify_daemon_started(pid_file, bound); |
There was a problem hiding this comment.
Optional. Every failed start now takes about 3 s longer: a bad TLS certificate exits after 3.24 and 3.22 s against 0.21 s on main, a pending migration after 3.29 s against 0.20 s. The parent still holds its copy of the listening socket while it probes; with drop(std_listener) before verify_daemon_started, a bad certificate exits after 0.23 s, twice.
| env_override={"EXTENDDB_ADMIN_PASSWORD": "TestPass1!"}, | ||
| ) | ||
| result = _run_extenddb("serve", "--port", "0", config=cli_env["config_path"], check=False) | ||
| started = re.search(r"server started \(pid (\d+), 127\.0\.0\.1:(\d+)\)", result.stdout) |
There was a problem hiding this comment.
Optional. The --port 0 test calls serve and asserts before its try, so a failure there leaves the daemon running and the teardown stops the wrong port.
Changes since the last review
docs/manuals/07-upgrade-manual.md. The merge keeps both entries, main's first.--port 0for the port that the listener bound, as the review thread onserve_helpers.rsasked. At 7e994b7,serveprinted the bound port but wroteextenddb-0.pid. Sostop --port <the printed port>refused the daemon as one started with--foreground, andstatus --port <the printed port>gave its pid as unknown.test_serve_checks_the_port_a_daemon_bound_with_port_0now runsstatusandstopwith the printed port. The v1.1.0 entry and the troubleshooting entry forstopsay which port to pass.What
extenddb servewithout--foregroundnow reportsextenddb server startedonce the daemon answersGET /health, and exits 1 when the daemon exits first. Before, it watched the daemon for 3 seconds and reported it started if it was still running then.verify_daemon_startedincrates/app/src/serve_helpers.rspolls the daemon's/healthwith the probe ofextenddb healthcheck(check_startedincrates/app/src/cmd_healthcheck.rs), and checks the daemon withis_process_alive, asstatusandstopdo. The server answers/healthonly after it connects to its storage backend.servereports a daemon that has not answered within 60 seconds as started, as main does, with a warning on stderr. The daemon goes on starting. Its startup can run longer, for example to rebuild an index after a crash. The documented systemd unit hasType=forkingandRestart=on-failure. With it, an exit 1 at the bound would make systemd stop such a daemon and start it again. A long start would then never finish.Type=forkingunit to start by default, so the documented unit needs noTimeoutStartSec.servenames the bound address in its banner and in the check, so it checks a daemon started with--port 0on the port that the daemon got.servealso names the PID file for that port, soextenddb stop --port <port>andextenddb status --port <port>find the daemon. For a nonzero port the name does not change. Two daemons started with--port 0in onerun_dirno longer shareextenddb-0.pid.docs/manuals/05-admin-guide.mdsays whatservewaits for, anddocs/manuals/07-upgrade-manual.mdhas a v1.1.0 entry.docs/troubleshooting.mdsays which port to pass tostopfor a server started with--port 0.Why
#736. Measured on 2026-11-21 on PostgreSQL. The catalog address of an initialized deployment was a listener that accepts connections and never answers:
serveextenddb server started (pid <n>, 127.0.0.1:<port>).statusreported it running, and 12 s later not running.Server failed to start: daemon process <n> exited during startup.#736 also asks
serveto print the daemon's error. This PR does not do that: the error is still only in syslog, as before, andserveprints the command that shows it (journalctl -t extenddbon Linux). #736 stays open for it.Refs #736
Testing done
At d34a74c, on main 5252eda.
tests/test_cli_lifecycle.py, on PostgreSQL like the rest of that file:test_serve_fails_when_the_daemon_cannot_reach_its_cataloggivesservea catalog address that never accepts, throughEXTENDDB__STORAGE__POSTGRES__CONNECTION_STRING. It expects exit 1 withexited during startup, after more than 3 and less than 60 seconds.test_serve_reports_a_daemon_still_starting_after_its_waitholds a lock in the catalog that the daemon's startup waits on. It expects exit 0 and the warning after 60 to 90 seconds, and then the daemon serving once the test releases the lock.test_serve_checks_the_port_a_daemon_bound_with_port_0starts a daemon with--port 0. It expects exit 0 with no warning, and the port the daemon got in theextenddb server startedline. Thenstatus --port <that port>must give the daemon's pid, andstop --port <that port>must stop it.test_init_serve_status_stopnow checks that a daemon that answers at once gets no warning.serveexits 0. In the second, it reports the daemon started with no warning. In the third, it names port 0. With the code of 7e994b7, the third test fails at itsstatuscheck, which gives the pid as unknown.cargo +1.97.0 fmt --all -- --checkextenddb-app: 105 passedcargo +1.97.0 test -p extenddb-storage-postgres --test doc_version_literals: 3 passedclippy.yml, with-p extenddb-appcargo +1.91.1 check -p extenddb-app --all-targetsThese open PRs also add an entry to
docs/manuals/07-upgrade-manual.md. Each conflicts with this branch there: #1166, #1314, #1322, #1324, #1328, #1329, #1331, #1340, #1349, #1427, #368, #479, #484, #485, #542, #546, #558, #571, #591 and #738.Checklist
cargo test --workspace)cargo fmt --check)cargo clippy -- -W clippy::pedantic)Storagetrait, auth model, on-diskformat, or public CLI surface, an RFC has been accepted or is linked
below. Otherwise, an ADR captures the decision (link below).
ADR / RFC: n/a
Breaking changes
servewithout--foregroundexits 1 when the daemon exits before it answers/health, where it exited 0 when the daemon was still running 3 seconds after the start. It now returns when the daemon answers or exits, or 60 seconds after the daemon's PID file appears. A last probe can add up to 3 seconds.A server started with
--port 0writes the PID file of the port it bound,extenddb-<port>.pid, where it wroteextenddb-0.pid.stop --port 0no longer finds it: pass the port thatserveprinted tostopandstatus. Withport = 0in the configuration,stopwithout--portno longer finds the daemon, andcatalog-checkno longer refuses to run beside it. The upgrade manual has a v1.1.0 entry.By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache License 2.0 and I agree to the Developer Certificate of
Origin (DCO). See CONTRIBUTING.md for details.