Skip to content

fix(serve): keep the start directory as the daemon's working directory - #1331

Open
LeeroyHannigan wants to merge 3 commits into
mainfrom
fix/714-daemon-working-directory
Open

LeeroyHannigan wants to merge 3 commits into
mainfrom
fix/714-daemon-working-directory

Conversation

@LeeroyHannigan

@LeeroyHannigan LeeroyHannigan commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Changes since the last review

  • 2c4ef41 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.
  • 1b3ac0e answers the review thread on the v1.1.0 entry. stop, status and catalog-check resolve a relative run_dir against the directory they run in. So they find the PID file of such a daemon only from the directory that serve started in. The v1.1.0 entry now says so, with the command that works, and so does the admin guide's note on stop. The code does not change.

What

extenddb serve without --foreground now keeps the directory it was started in as the daemon's working directory. crates/app/src/cmd_serve.rs gives Daemonize::working_directory the directory .. The daemonize crate moves the daemon to / unless it is given one, so every relative path that the daemon read after the fork resolved against /.

  • Relative paths in the configuration now resolve against the start directory, as they do with --foreground: [import] paths, [export] paths, import_export_root, server.tls.cert_path, server.tls.key_path, docs_dir and server.run_dir.
  • Relative FilePath and FileSource.Path values in requests resolve there too. absolutize and validate_path_parent in crates/engine/src/import_export_io.rs use the process's working directory.
  • stop, status and catalog-check resolve a relative run_dir against the directory they run in, as they do for a server started with --foreground --write-pid-file. From another directory they do not find the daemon's PID file. The v1.1.0 entry and the admin guide's note on stop say to run them in the start directory.
  • Sometimes serve cannot enter the directory it was started in, for example after sudo -u from a home directory that the server's user cannot enter. Then the daemon runs in / as before, and serve prints a warning with what to do. Main serves from such a directory when every path in the configuration is absolute, and so does this change. With an all-absolute configuration, a deleted start directory also keeps main's behaviour.
  • [storage.sqlite] path and the backup store paths were already made absolute at config load. Several places gave the daemon's / as a reason: the comments in crates/storage-sqlite/src/config.rs and crates/config/src/backup.rs, the key_file refusal in crates/config/src/encryption.rs with its comments, two rows of docs/manuals/05-admin-guide.md, and the docstring of test_a_relative_root_resolves_against_the_shell_in_daemon_mode. They no longer do. The key_file refusal itself stays.
  • docs/manuals/07-upgrade-manual.md has a v1.1.0 entry.

The daemon now keeps its start directory in use, so the file system that holds it cannot be unmounted while the server runs. The upgrade entry says this.

Why

#714. Measured on 2026-11-21 with a SQLite build of f0170bd, whose app, server, config and import and export code equals main 8c64333. Each server was started without --foreground from the directory of its configuration:

Relative setting Result on main
run_dir = "rel-run" Failed to daemonize (child): unable to open pid file, errno 2, then Server failed to start: PID file not created within 5 seconds.
[import] paths = ["rel-imports"] and [export] paths = ["rel-exports"] Server failed to start: daemon process <pid> exited during startup.
cert_path = "tls/cert.pem" and key_path = "tls/key.pem" Server failed to start: PID file not created within 5 seconds.

The same three cases, and the three together, start on a SQLite build of this branch. /health answers 200, both roots exist under the start directory, and nothing appears under /.

Closes #714

Testing done

At 1b3ac0e, on main 5252eda.

  • New test_daemon_resolves_relative_paths_from_its_start_directory in tests/test_cli_lifecycle.py, on PostgreSQL like the rest of that file. It writes a configuration with a relative run_dir, relative TLS files and relative import and export roots, and it starts the daemon from the configuration's directory. It checks that the server answers /health over TLS, that both roots exist under that directory, and that the PID file is in the relative run_dir. On Linux it also checks that /proc/<pid>/cwd of the daemon is that directory, which is the directory relative request paths resolve against.
    • On a PostgreSQL build of this branch, the whole file passed: 13 passed, and the Unix socket test skipped for want of a local socket.
    • On a build without the working_directory call, the new test fails with Failed to daemonize (child): unable to open pid file, errno 2.
  • On a PostgreSQL build of 2c4ef41, a daemon with run_dir = "rel-run" started from one directory. From another directory, with --config naming its file, status gave the pid as unknown, stop exited 1 with A server is listening on port 18732 but wrote no PID file (rel-run/extenddb-18732.pid)., and catalog-check ran beside the daemon. From the start directory, all three found it.
  • With an all-absolute SQLite configuration, started from a mode-000 directory and from a deleted directory, main and this branch serve in both cases. In the first case this branch prints the warning.
  • _run_extenddb in tests/lifecycle_helpers.py takes cwd. feat: add StartBulkPutItems and the bulk operation APIs #874 adds the same argument in the same words, so the two hunks merge. The fixture's teardown runs stop in the test's directory, where a relative run_dir resolves.
  • These pass, on the four crates that this branch changes (extenddb-app, extenddb-config, extenddb-storage and extenddb-storage-sqlite):
    • cargo +1.97.0 fmt --all -- --check
    • the lib tests of extenddb-app (105 passed), extenddb-config (25 passed) and extenddb-storage (292 passed)
    • cargo +1.97.0 test -p extenddb-storage-postgres --test doc_version_literals: 3 passed
    • the first command of clippy.yml with -p for the four crates, and the second with -p extenddb-app
    • cargo +1.91.1 check --all-targets with -p for the four crates
    • cargo +1.97.0 clippy --all-targets -- -W clippy::pedantic with -p for the four crates, with no warning on a line that this branch adds
  • The lib tests of extenddb-storage-sqlite gave 327 passed and 2 failed. The 2 open a filesystem backup store, and the store refuses its root on the test host, whose parent directories belong to another user. This branch changes only a comment in that crate.

The test edits the configuration with three regular expressions inline. _patch_config_port sets only the port, and a general patch helper would take more lines than the edits.

#874 adds a lifecycle test that asserts the daemon's working directory is / (os.readlink(f"/proc/{daemon_pid}/cwd") == "/"). With this change it is the start directory, so the second of the two to merge changes that assert. #537 deletes both admin guide rows that this change edits, the backup store paths and key_file. It moves their text into docs/configuration.md and extenddb.sample.toml, where it still gives the daemon's / as a reason. The second of the two to merge drops that reason there. 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, #1332, #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

A relative path that named an existing directory under /, such as tmp, now names a directory under the start directory. This holds for the configuration and for requests. The daemon keeps its start directory in use. 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 daemonizes with the daemonize crate,
which moves the daemon to / unless it is given a working directory. So
every relative path that the daemon read after the fork resolved
against / and not against the directory serve was started in. On
1.0.0, a daemon with a relative run_dir failed with "Failed to
daemonize (child): unable to open pid file". With relative import and
export roots it failed with "daemon process <pid> exited during
startup", and with relative TLS files the PID file never appeared.
With --foreground the same configurations start.

serve now gives the daemon "." as its working directory. Relative
paths in the configuration and relative FilePath and FileSource.Path
values in requests resolve as they do with --foreground. When serve
cannot enter the directory it was started in, the daemon runs in / as
before, and serve prints a warning.

[storage.sqlite] path and the backup store paths were already made
absolute at config load. Their comments, the admin guide, the key_file
refusal and a backup store test gave the daemon's / as a reason. They
no longer do.

A new test in tests/test_cli_lifecycle.py writes a configuration with a
relative run_dir, relative TLS files and relative import and export
roots. It starts the daemon from that configuration's directory. It
then checks that the server serves TLS, that both roots exist there,
and that the PID file is in the relative run_dir. On Linux it also
checks that the daemon's working directory is that directory.
_run_extenddb takes a cwd, and the fixture's teardown runs stop in the
test's directory.

The upgrade manual has a v1.1.0 entry.

Closes #714

@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 59199d2.

1 finding is left inline.

Comment thread docs/manuals/07-upgrade-manual.md Outdated

**What changed.** `extenddb serve` without `--foreground` keeps the directory it was started in as the daemon's working directory. 1.0.0 moved the daemon to `/`. Relative paths in the configuration file, such as `[import] paths`, `[export] paths`, `server.tls.cert_path` and `server.run_dir`, now resolve against that directory, as they do with `--foreground`. Relative `FilePath` and `FileSource.Path` values in requests resolve there too (#714).

**Who is affected.** A deployment whose configuration has a relative path, and a client that sends a relative `FilePath` or `FileSource.Path`. 1.0.0 failed to start the daemon with a relative `run_dir` or relative TLS files. It also failed with relative import and export roots that its user could not create under `/`. A relative `run_dir` gave `Failed to daemonize (child): unable to open pid file`, and relative import and export roots gave `daemon process <pid> exited during startup`. A relative path that did exist under `/` named that directory: with `[import] paths = ["tmp"]`, 1.0.0 imported from `/tmp`, and this release imports from `tmp` under the start directory. With `run_dir = "tmp"`, 1.0.0 reported a failed start but left the daemon serving, with its PID file in `/tmp`. Sometimes `serve` cannot enter the directory it was started in, for example after `sudo -u` from a home directory that the server's user cannot enter. Then the daemon runs in `/` as before, and `serve` prints a warning. The daemon keeps its start directory in use, so the file system that holds it cannot be unmounted while the server runs. No catalog migration is required.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A daemon with a relative run_dir now starts, but stop and status find its PID file only from the same start directory, and the entry does not say so.

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. stop, status and catalog-check build the PID path from run_dir as written (pid_file_path in cmd_stop.rs, cmd_status.rs and cmd_catalog_check.rs), so they resolve a relative run_dir against the directory they run in. On a PostgreSQL build of 2c4ef41, this branch with main merged, a daemon with run_dir = "rel-run" started from one directory. From another directory, with --config naming that file, status gave the pid as unknown. stop exited 1 with "A server is listening on port 18732 but wrote no PID file (rel-run/extenddb-18732.pid)", and catalog-check ran beside the daemon. From the start directory, all three found the daemon.

1b3ac0e adds this to the v1.1.0 entry with the command that works, cd <that directory> && extenddb stop --config extenddb.toml, and to the admin guide's note on run_dir under Stopping. There is no code change: serve --foreground --write-pid-file resolves a relative run_dir the same way on main.

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.
A daemon with a relative run_dir now starts, and it writes its PID file
under the directory that serve started in. stop, status and
catalog-check resolve the same relative run_dir against the directory
they run in. From another directory, stop exits 1 and says that the
server wrote no PID file. status gives the pid as unknown, and
catalog-check does not refuse to run beside the daemon.

The v1.1.0 entry of the upgrade manual now says so, with the command
that works from the start directory. The note on stop in the admin
guide, which names run_dir, says it too.

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

Checked at 1b3ac0e against the list from 59199d2. The upgrade entry now documents the relative run_dir limitation for stop and status; app and config tests pass. No new findings.

Previous items: 1 fixed.

@demohan demohan self-assigned this Nov 26, 2026

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

A daemon serve resolves relative paths as --foreground does at 1b3ac0e. Started from the config's directory or another one, the daemon resolves a relative run_dir, TLS files, import and export roots, docs_dir, the backup store and the SQLite path as the foreground server does; main's daemon fails to start on run_dir, TLS and the import and export roots, and starts without documentation on docs_dir. Relative FilePath and FileSource.Path requests complete in the daemon, where main answers 400 Path must resolve under one of the configured allowed paths. The new lifecycle test fails against main and with .working_directory removed, the lifecycle file passes 13 with 1 skipped, and fmt, clippy, the lib tests and the 1.91.1 check are clean. CI's wasm failure comes from main, and integration and mongodb-integration were cancelled; a merge of main and a rerun would settle them.

I will approve once the fallback to / has a test.

// config and in requests resolve as they do with --foreground.
// When this process cannot enter that directory, the daemon runs
// in / as before, and serve says so.
let workdir = match std::env::set_current_dir(".") {

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.

The fallback to / carries startup from a directory the daemon cannot enter, and no test reaches it: with . in place of / the lifecycle file still passes 13 with 1 skipped, and a start from a mode-000 directory then fails with Failed to daemonize (child): unable to change directory, where main serves. A lifecycle test that starts from such a directory and checks the warning, /health 200 and / as the working directory would hold it. Separately, a deleted start directory is not caught here: chdir(".") succeeds in it on Linux, the daemon runs in .../sub (deleted), and relative request paths then answer Cannot determine current directory where main answered Path must resolve....


**What changed.** `extenddb serve` without `--foreground` keeps the directory it was started in as the daemon's working directory. 1.0.0 moved the daemon to `/`. Relative paths in the configuration file, such as `[import] paths`, `[export] paths`, `server.tls.cert_path` and `server.run_dir`, now resolve against that directory, as they do with `--foreground`. Relative `FilePath` and `FileSource.Path` values in requests resolve there too (#714).

**Who is affected.** A deployment whose configuration has a relative path, and a client that sends a relative `FilePath` or `FileSource.Path`. 1.0.0 failed to start the daemon with a relative `run_dir` or relative TLS files. It also failed with relative import and export roots that its user could not create under `/`. A relative `run_dir` gave `Failed to daemonize (child): unable to open pid file`, and relative import and export roots gave `daemon process <pid> exited during startup`. A relative path that did exist under `/` named that directory: with `[import] paths = ["tmp"]`, 1.0.0 imported from `/tmp`, and this release imports from `tmp` under the start directory. With `run_dir = "tmp"`, 1.0.0 reported a failed start but left the daemon serving, with its PID file in `/tmp`. `stop`, `status` and `catalog-check` resolve a relative `run_dir` against the directory they run in, so run them in the directory `serve` was started in. For example: `cd <that directory> && extenddb stop --config extenddb.toml`. From another directory, `stop` exits 1 because it finds no PID file, `status` gives the pid as unknown, and `catalog-check` runs beside the daemon. Sometimes `serve` cannot enter the directory it was started in, for example after `sudo -u` from a home directory that the server's user cannot enter. Then the daemon runs in `/` as before, and `serve` prints a warning. The daemon keeps its start directory in use, so the file system that holds it cannot be unmounted while the server runs. No catalog migration is required.

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. On main the message is a race: relative import and export roots gave PID file not created within 5 seconds. in 3 of 4 runs and exited during startup in 1, and relative TLS gave exited during startup 4 of 4, so the body's table pairs them the other way round. Naming both, or Server failed to start:, would cover it.

# Stop
_run_extenddb("stop", config=cli_env["config_path"])

def test_daemon_resolves_relative_paths_from_its_start_directory(self, cli_env, tmp_path):

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 test checks the daemon's working directory; a relative export or import request and a relative docs_dir, which #714 asks about, would check what the reader sees.

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.

Daemon mode resolves relative config and request paths against / instead of the directory serve was started from

3 participants