Skip to content

fix(serve): report a daemon started once it answers /health - #1332

Open
LeeroyHannigan wants to merge 3 commits into
mainfrom
fix/736-daemon-start-readiness
Open

LeeroyHannigan wants to merge 3 commits into
mainfrom
fix/736-daemon-start-readiness

Conversation

@LeeroyHannigan

@LeeroyHannigan LeeroyHannigan commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Changes since the last review

  • 3529946 merges main at 5252eda. Main and this branch each add a v1.1.0 entry at the same place in docs/manuals/07-upgrade-manual.md. The merge keeps both entries, main's first.
  • d34a74c names the PID file of a server started with --port 0 for the port that the listener bound, as the review thread on serve_helpers.rs asked. At 7e994b7, serve printed the bound port but wrote 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. test_serve_checks_the_port_a_daemon_bound_with_port_0 now runs status and stop with the printed port. The v1.1.0 entry and the troubleshooting entry for stop say which port to pass.

What

extenddb serve without --foreground now reports extenddb server started once the daemon answers GET /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_started in crates/app/src/serve_helpers.rs polls the daemon's /health with the probe of extenddb healthcheck (check_started in crates/app/src/cmd_healthcheck.rs), and checks the daemon with is_process_alive, as status and stop do. The server answers /health only after it connects to its storage backend.
  • serve reports 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 has Type=forking and Restart=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.
  • The 60 seconds are longer than the waits after which a backend's startup gives up. These are 10 seconds for the catalog registration on PostgreSQL, MongoDB and Cassandra, and 30 seconds 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, so the documented unit needs no TimeoutStartSec.
  • serve names the bound address in its banner and in the check, so it checks a daemon started with --port 0 on the port that the daemon got. serve also names the PID file for that port, so extenddb stop --port <port> and extenddb status --port <port> find the daemon. For a nonzero port the name does not change. Two daemons started with --port 0 in one run_dir no longer share extenddb-0.pid.
  • docs/manuals/05-admin-guide.md says what serve waits for, and docs/manuals/07-upgrade-manual.md has a v1.1.0 entry. docs/troubleshooting.md says which port to pass to stop for 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:

Build serve
main's check (the serve code of 8c64333) exit 0 after 3.6 s, extenddb server started (pid <n>, 127.0.0.1:<port>). status reported it running, and 12 s later not running.
this branch exit 1 after 13.3 s, Server failed to start: daemon process <n> exited during startup.

#736 also asks serve to print the daemon's error. This PR does not do that: the error is still only in syslog, as before, and serve prints the command that shows it (journalctl -t extenddb on Linux). #736 stays open for it.

Refs #736

Testing done

At d34a74c, on main 5252eda.

  • Three new tests in tests/test_cli_lifecycle.py, on PostgreSQL like the rest of that file:
    • test_serve_fails_when_the_daemon_cannot_reach_its_catalog gives serve a catalog address that never accepts, through EXTENDDB__STORAGE__POSTGRES__CONNECTION_STRING. It expects exit 1 with exited during startup, after more than 3 and less than 60 seconds.
    • test_serve_reports_a_daemon_still_starting_after_its_wait holds 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_0 starts a daemon with --port 0. It expects exit 0 with no warning, and the port the daemon got in the extenddb server started line. Then status --port <that port> must give the daemon's pid, and stop --port <that port> must stop it.
    • test_init_serve_status_stop now checks that a daemon that answers at once gets no warning.
  • On a PostgreSQL build of this branch, the whole file passed: 15 passed, and the Unix socket test skipped for want of a local socket. With main's check, the three new tests fail. In the first, serve exits 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 its status check, which gives the pid as unknown.
  • These pass:
    • cargo +1.97.0 fmt --all -- --check
    • the lib tests of extenddb-app: 105 passed
    • cargo +1.97.0 test -p extenddb-storage-postgres --test doc_version_literals: 3 passed
    • both commands of clippy.yml, with -p extenddb-app
    • cargo +1.91.1 check -p extenddb-app --all-targets

These 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

  • I have read CONTRIBUTING.md
  • All tests pass (cargo test --workspace)
  • Code is formatted (cargo fmt --check)
  • Clippy is clean (cargo clippy -- -W clippy::pedantic)
  • I have added or updated tests for new functionality
  • I have updated documentation if behavior changed
  • Breaking changes are noted below (if any)
  • If this changes the wire protocol, Storage trait, auth model, on-disk
    format, 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

serve without --foreground exits 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 0 writes the PID file of the port it bound, extenddb-<port>.pid, where it wrote extenddb-0.pid. stop --port 0 no longer finds it: pass the port that serve printed to stop and status. With port = 0 in the configuration, stop without --port no longer finds the daemon, and catalog-check no 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.

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 LJMart11 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.

Reviewed at 7e994b7.

1 finding is left inline.

}

println!("extenddb server started (pid {pid}, {bind_addr})");
println!("extenddb server started (pid {pid}, {bound})");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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 demohan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants