Repository navigation
Add file descriptor store support to pystemd.daemon - #130
Merged
Merged
Conversation
- notify(unset_environment, *states, fds=None, **kwstates): when `fds`
is given, call sd_pid_notify_with_fds() so the fds travel with the
message; without it (or with an empty list), sd_notify() exactly as
before. `fds` is keyword-only, and there is no FDS= notify state for
it to shadow. It takes a single fd or a list of them; each fd is an int
or an object with fileno(). fds=0 sends fd 0 (it is not "no fds"), and
str/bytes are rejected with TypeError: bytes would otherwise iterate
as ints and silently send the wrong fds.
e.g. notify(False, fds=[memfd], fdstore=1, fdname="db") hands the
memfd to systemd's file descriptor store.
- listen_fds_with_names(unset_environment=0): sd_listen_fds_with_names(),
returned as {name: [fd, ...]}; this is how a restarted service gets
its stored fds back (and how named socket activation fds are found).
Names are not unique (a socket unit with several ListenStream= passes
all its fds under one name; FDNAME= can repeat), hence the lists.
Tested on systemd 259: a Type=notify service with FileDescriptorStoreMax=1
stores a memfd holding a SQLite database with notify(fds=...) and gets
it back with listen_fds_with_names()["db"][0] across four restarts;
fds=<int> lands in the store too, and two fds stored under the same
FDNAME both come back (LISTEN_FDNAMES=same:same).
Helpers for systemd's file descriptor store, on top of notify(fds=...):
- store_fd(name, fds, unset_environment=False, **kwstates): hand a single
fd or a list of fds to the store under `name`; the same as
notify(unset_environment, fds=fds, fdstore=1, fdname=name, **kwstates).
- store_fds({name: fds, ...}, unset_environment=False, **kwstates): several
names at once. FDNAME= covers a whole notify message, so this sends one
message per name (not atomic); unset_environment and the extra states
(e.g. ready=1) only go with the last message, since unsetting
$NOTIFY_SOCKET earlier would make the remaining sends no-ops. Returns
the lowest notify() result.
unset_environment can be passed positionally (like notify()) or as a
keyword, for readability: store_fds({...}, unset_environment=True).
Both validate names first (1-255 printable ASCII characters, no ':', as
LISTEN_FDNAMES is ':'-separated) and raise ValueError: systemd ignores an
invalid FDNAME= silently, so the fds would just never be stored.
Together with listen_fds_with_names() -> {name: [fd, ...]}, storing and
getting fds back is a dict both ways.
Tested on systemd 259: store_fds({"a": fd, "b": [fd1, fd2], "c": fd},
unset_environment=True, ready=1, status="stored") stores all four fds under three names and
activates the unit with the status set; store_fd() is used by a memfd,
chat-server (listening + client sockets), pty and pidfd example that all
get their fds back across restarts.
aleivag
force-pushed
the
daemon-fd-store
branch
from
November 18, 2026 21:36
dafa50f to
bc7390d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Support for systemd's file descriptor store in
pystemd.daemon:notify(unset_environment, *states, fds=None, **kwstates): whenfdsis given, callsd_pid_notify_with_fds()so the fds travel with the message; without it (or with an empty list),sd_notify()exactly as before.fdsis keyword-only; it takes a single fd or a list, each an int or an object withfileno().fds=0sends fd 0, and str/bytes raise TypeError (bytes would otherwise iterate as ints and send the wrong fds).listen_fds_with_names(unset_environment=0):sd_listen_fds_with_names(), returned as{name: [fd, ...]}. This is how a restarted service gets its stored fds back (and how named socket activation fds are found). Names aren't unique, hence the lists.store_fd(name, fds, unset_environment=False, **kwstates): the same asnotify(unset_environment, fds=fds, fdstore=1, fdname=name, **kwstates).store_fds({name: fds, ...}, unset_environment=False, **kwstates): several names, one message per name (FDNAME= covers a whole message, so it's not atomic);unset_environmentand the extra states only go with the last message.Both helpers validate names first (1-255 printable ASCII characters, no
:) and raise ValueError: systemd silently ignores an invalid FDNAME=, so the fds would just never be stored.Tested on systemd 259: a Type=notify service with FileDescriptorStoreMax=1 stores a memfd holding a SQLite database with
notify(fds=...)and gets it back withlisten_fds_with_names()["db"][0]across four restarts; two fds stored under the same FDNAME both come back;store_fds({"a": fd, "b": [fd1, fd2], "c": fd}, unset_environment=True, ready=1, status="stored")stores all four fds under three names and activates the unit. New unit tests in tests/test_daemon.py.Lint: ruff 0.9.6 (as pinned in .pre-commit-config.yaml), isort and pyrefly pass. The
formattingcheck fails on every PR right now for an unrelated reason: CI installs the latest ruff, which flags existing files in examples/.