Repository navigation
Conversation
kolyshkin
force-pushed
the
systemd-unit-timeout
branch
from
November 28, 2026 19:52
867ea02 to
b3a130c
Compare
kolyshkin
commented
Nov 28, 2026
kolyshkin
left a comment
Contributor
Author
There was a problem hiding this comment.
OK I hope we can merge coreos/go-systemd#535 first and make a release, when we'll drop the third commit.
kolyshkin
force-pushed
the
systemd-unit-timeout
branch
4 times, most recently
from
December 2, 2026 02:54
03e78b5 to
85e1884
Compare
thaJeztah
reviewed
Dec 2, 2026
kolyshkin
force-pushed
the
systemd-unit-timeout
branch
from
December 3, 2026 01:25
85e1884 to
4a43047
Compare
thaJeztah
reviewed
Dec 4, 2026
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 { |
Member
There was a problem hiding this comment.
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.
Contributor
Author
There was a problem hiding this comment.
Yeah; fixing this is not a one-liner so I'd rather not do it here.
Assisted-by: Claude Opus 5.5 Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
kolyshkin
force-pushed
the
systemd-unit-timeout
branch
from
December 5, 2026 01:13
35225ca to
fc27363
Compare
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
force-pushed
the
systemd-unit-timeout
branch
from
December 5, 2026 01:15
fc27363 to
87c295e
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.
Most systemd D-Bus calls are made with
context.TODO(), so if systemd ishung, they never return. This is especially bad for
StartTransientUnitandStopUnit: go-systemd holds the connection'sjobListenerlock during thecall, 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/stopUnitonly starts after the call returns.This PR:
GetManagerPropertyContext;startUnit,stopUnit,setUnitProperties, etc.) accept a context, which is created with a 30stimeout by the exported methods (
Apply,Destroy,Set,AddPid, andNewUnifiedManagerin rootless mode), and switchesgetManagerPropertyto
GetManagerPropertyContext;startUnit/stopUnitwith the context deadline,which covers the whole operation (D-Bus call, reconnects, retry, and
waiting for the job to complete).
startUnit/stopUnitreturn 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,
stopUnitstill ignores non-timeout D-Buserrors (e.g. for a non-existent unit).
Testing
Simulated a hung systemd by sending SIGSTOP to
systemd --user, thencalling
NewUnifiedManager, two concurrentApplyand oneDestroy(allrootless, sharing one D-Bus connection).
Before: everything hangs forever. One goroutine is stuck in
startJob'sD-Bus call, holding
jobListener; otherstartJobcallers and go-systemd'ssignal dispatcher wait for that lock.
After: