Skip to content

Fix pystemd.run waits and pystemd.futures on systemd 259 / Python 3.14 - #126

Merged
aleivag merged 6 commits into
systemd:mainfrom
aleivag:fix-futures-systemd259-py314
Nov 18, 2026
Merged

aleivag merged 6 commits into
systemd:mainfrom
aleivag:fix-futures-systemd259-py314

Conversation

@aleivag

@aleivag aleivag commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

pystemd.run:

  • wait=True / wait_for_activation=True could block forever: systemd only emits unit PropertiesChanged signals while some client has called Manager.Subscribe(), and run() only eavesdrops via BecomeMonitor. On a desktop something else is usually subscribed; on a minimal host (e.g. a Fedora 44 cloud VM) nobody is. Subscribe before starting the unit.
  • Drain every message sd-bus has buffered on each wakeup. One socket read can bring several signals, and select() does not fire again for the ones already read, so processing one message per wakeup could miss the state change we are waiting for.
  • wait_for_activation=True only stopped waiting at SubState "running". A Type=notify or Type=exec unit that fails during startup goes straight to "failed", so run() blocked forever. It now also stops once the start job is over and the unit is in an exit substate (exited/failed/dead), as wait=True does; with raise_on_fail the caller gets PystemdRunError as usual. Tested: Type=notify sh -c "exit 3" and Type=exec /nonexistent return in ~20ms as failed (both hung before); a Type=notify unit that sends READY=1 still waits for it.

pystemd.futures:

  • Python 3.14 made forkserver the default start method on Linux, which broke TransientUnitPoolExecutor ("SemLock created in a fork context is being shared with a process in a spawn context"). Workers now subclass ForkProcess, which enter_unit relies on anyway.
  • Start the anchor unit with Type=exec. With Type=simple the unit is reported running right after fork, while systemd >= 255's executor is still root, so enter_unit copied uid 0 and User= was ignored.
  • The pool's anchor process is now tail --pid=<parent> instead of sleep infinity (as TransientUnitProcess already does), so the unit does not leak when the parent dies without reaching exit.

Tested on Fedora 44 (systemd 259, kernel 6.19, aarch64, Python 3.14): futures.run with User=nobody + PrivateNetwork, CPUQuota and MemoryMax; examples/future_cpucap_{method,process,pool}.py; unit tests pass.

@aleivag
aleivag force-pushed the fix-futures-systemd259-py314 branch 7 times, most recently from 2db3f13 to c80ed8e Compare October 30, 2026 00:01

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

Good job.
All of my suggestions, except the one where ValueError is thrown for None while the type-hint specifies (thereby an interface contract) that None is valid, are mere personal preferences -- feel free to ignore them.

pystemd.run:
- wait=True / wait_for_activation=True could block forever: systemd only
  emits unit PropertiesChanged signals while some client has called
  Manager.Subscribe(), and run() only eavesdrops via BecomeMonitor. On a
  desktop something else is usually subscribed; on a minimal host
  (e.g. a Fedora 44 cloud VM) nobody is. Subscribe before starting the unit.
- Drain every message sd-bus has buffered on each wakeup. One socket read
  can bring several signals, and select() does not fire again for the ones
  already read, so processing one message per wakeup could miss the state
  change we are waiting for.

pystemd.futures:
- Python 3.14 made forkserver the default start method on Linux, which
  broke TransientUnitPoolExecutor ("SemLock created in a fork context is
  being shared with a process in a spawn context"). Workers now subclass
  ForkProcess, which enter_unit relies on anyway.
- Start the anchor unit with Type=exec. With Type=simple the unit is
  reported running right after fork, while systemd >= 255's executor is
  still root, so enter_unit copied uid 0 and User= was ignored.
- The pool's anchor process is now `tail --pid=<parent>` instead of
  `sleep infinity` (as TransientUnitProcess already does), so the unit
  does not leak when the parent dies without reaching __exit__.

Tested on Fedora 44 (systemd 259, kernel 6.19, aarch64, Python 3.14):
futures.run with User=nobody + PrivateNetwork, CPUQuota and MemoryMax;
examples/future_cpucap_{method,process,pool}.py; unit tests pass.
wait_for_activation=True only stopped waiting when it saw SubState
"running" for the start job. A Type=notify or Type=exec unit that fails
during startup never gets there (it goes straight to "failed"), so
pystemd.run() blocked forever.

Also stop once the start job is over and the unit is in an exit
substate (exited/failed/dead), the same condition wait=True uses; with
raise_on_fail the caller then gets PystemdRunError as usual.

Tested on systemd 259: Type=notify `sh -c "exit 3"` and Type=exec
`/nonexistent` now return in ~20ms as failed/failed (both hung before);
a Type=notify unit that sends READY=1 still waits for it.
@aleivag
aleivag force-pushed the fix-futures-systemd259-py314 branch from a6df5af to 6d2ccbe Compare November 18, 2026 18:56
@aleivag
aleivag merged commit 872cb6d into systemd:main Nov 18, 2026
49 checks passed
@aleivag
aleivag deleted the fix-futures-systemd259-py314 branch November 18, 2026 23:34
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