Skip to content

systemd: add timeouts to D-Bus calls - #105

Open
kolyshkin wants to merge 3 commits into
opencontainers:mainfrom
kolyshkin:systemd-unit-timeout
Open

kolyshkin wants to merge 3 commits into
opencontainers:mainfrom
kolyshkin:systemd-unit-timeout

Conversation

@kolyshkin

@kolyshkin kolyshkin commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Most systemd D-Bus calls are made with context.TODO(), so if systemd is
hung, they never return. This is especially bad for StartTransientUnit and
StopUnit: go-systemd holds the connection's jobListener lock during the
call, so a single stuck call blocks all other start/stop jobs on the shared
connection, as well as JobRemoved signal handling. Also, the existing 30s
timer in startUnit/stopUnit only starts after the call returns.

This PR:

  • bumps go-systemd to v22.8.0, which adds GetManagerPropertyContext;
  • makes all internal helpers which do D-Bus calls (startUnit, stopUnit,
    setUnitProperties, etc.) accept a context, which is created with a 30s
    timeout by the exported methods (Apply, Destroy, Set, AddPid, and
    NewUnifiedManager in rootless mode), and switches getManagerProperty
    to GetManagerPropertyContext;
  • replaces the timer in startUnit/stopUnit with the context deadline,
    which covers the whole operation (D-Bus call, reconnects, retry, and
    waiting for the job to complete).
  • makes startUnit/stopUnit return errors wrapping the context error
    (so callers can use errors.Is), including on cancellation.

This is a reworked version of
runc#4007 (the code has
since moved here). Unlike that PR, stopUnit still ignores non-timeout D-Bus
errors (e.g. for a non-existent unit).

Testing

Simulated a hung systemd by sending SIGSTOP to systemd --user, then
calling NewUnifiedManager, two concurrent Apply and one Destroy (all
rootless, sharing one D-Bus connection).

Before: everything hangs forever. One goroutine is stuck in startJob's
D-Bus call, holding jobListener; other startJob callers and go-systemd's
signal dispatcher wait for that lock.

After:

[  0.03s] SIGSTOP systemd --user (pid 855)
[ 30.05s] NewUnifiedManager n0: took 30.0s, err: unable to get systemd manager property ControlGroup: context deadline exceeded
[ 30.05s] Apply a0: took 30.0s, err: unable to start unit "hangtest-a0.scope" ...: waiting for systemd to create hangtest-a0.scope: context deadline exceeded
[ 30.05s] Apply a1: took 30.0s, err: unable to start unit "hangtest-a1.scope" ...: waiting for systemd to create hangtest-a1.scope: context deadline exceeded
[ 31.03s] Destroy d0: took 30.0s, err: waiting for systemd to remove hangtest-d0.scope: context deadline exceeded
[ 31.03s] SIGCONT systemd --user
[ 32.04s] after Apply: took 0.0s, err: <nil>
[ 32.05s] after Destroy: took 0.0s, err: <nil>

@kolyshkin kolyshkin left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK I hope we can merge coreos/go-systemd#535 first and make a release, when we'll drop the third commit.

@kolyshkin kolyshkin added this to the 0.1.2 milestone Dec 1, 2026
@kolyshkin
kolyshkin force-pushed the systemd-unit-timeout branch 4 times, most recently from 03e78b5 to 85e1884 Compare December 2, 2026 02:54
Comment thread systemd/common.go Resolved
@kolyshkin
kolyshkin force-pushed the systemd-unit-timeout branch from 85e1884 to 4a43047 Compare December 3, 2026 01:25
@kolyshkin
kolyshkin requested a review from thaJeztah December 3, 2026 07:01
Comment thread systemd/common.go Resolved
Comment thread systemd/common.go Resolved
Comment thread systemd/common.go
Comment on lines 275 to 282
func systemdVersion(cm *dbusConnManager) int {
versionOnce.Do(func() {
version = -1
verStr, err := getManagerProperty(cm, "Version")
// Not using a caller's context here, as the result is cached.
ctx, cancel := context.WithTimeout(context.Background(), dbusTimeout)
defer cancel()
verStr, err := getManagerProperty(ctx, cm, "Version")
if err == nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Brought to you by my LLM;

If the first call times out, -1 is cached for the lifetime of the process, even if systemd subsequently recovers.

This is a pre-existing problem with transient errors, but the introduction of timeouts makes it more likely. Probably a follow-up rather than a blocker.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah; fixing this is not a one-liner so I'd rather not do it here.

Comment thread systemd/common.go Resolved
Assisted-by: Claude Opus 5.5
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
@kolyshkin
kolyshkin force-pushed the systemd-unit-timeout branch from 35225ca to fc27363 Compare December 5, 2026 01:13
All systemd D-Bus calls are made with context.TODO() (or, in case of
GetManagerProperty, with no context at all), so if systemd is hung (or
very slow), they never return, and runc (or kubelet) hangs with them.

Make all the helpers making D-Bus calls (startUnit, stopUnit, addPid,
etc.) accept a context, and use it for the D-Bus call. The context is
created by the caller (Apply, Destroy, Set, AddPid, and NewUnifiedManager
in rootless mode) with a 30 seconds timeout. Since the same context is
used for all attempts made by retryOnDisconnect (once the deadline is
exceeded, the call returns context.DeadlineExceeded, not dbus.ErrClosed,
so there are no more retries), and for all D-Bus calls made by a method,
the whole method can't take longer than that (not counting waiting for
a job to complete, which is addressed by the next commit). This also
prepares for passing a context from a caller of a Manager method, if
such API is added later.

The exceptions are:
 - systemdVersion, which uses its own context, as its result is cached;
 - resetFailedUnit called by startUnit to clean up after a failure or a
   timeout, which needs to be done even if the caller's context is done.

Switch from GetManagerProperty to GetManagerPropertyContext (added in
go-systemd v22.8.0), and add the property name to its error (as plain
"context deadline exceeded" is not very helpful).

Assisted-by: Claude Opus 5.5
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
In startUnit and stopUnit, a separate 30 seconds timer to wait for the
job to complete is only started once the StartTransientUnit/StopUnit
D-Bus call returns, so the whole operation can take up to 30 seconds
(D-Bus call, with all retries) plus another 30 seconds (waiting for the
job), plus, for startUnit, the same again after resetting a failed unit.

Note that the D-Bus call timeout is important here, as go-systemd's
startJob holds the connection's jobListener lock while the call is in
progress. This blocks the JobRemoved signal handler, and all other
start/stop jobs on the same (shared) connection, leading to a pile of
stuck goroutines (as was seen in kubelet, see
opencontainers/runc#4007).

Replace the timer with the caller's context (which has a deadline), so
it is used for both the D-Bus call and waiting for the job to complete.
Since the same context is used for all attempts made by
retryOnDisconnect (and for the startUnit retry after resetting a failed
unit), the whole operation is limited to 30 seconds.

If StartTransientUnit times out, return right away, without trying to
reset the unit (systemd is not responding, so it would also time out).

When the context is done (timed out or canceled), return an error which
wraps the context error, so callers can check for it using errors.Is.

The existing behavior of stopUnit ignoring D-Bus errors (other than
the timeout or cancellation) is kept, as it is needed to handle a
non-existent unit.

Assisted-by: Claude Opus 5.5
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
@kolyshkin
kolyshkin force-pushed the systemd-unit-timeout branch from fc27363 to 87c295e Compare December 5, 2026 01:15
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