Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Process org.freedesktop.systemd1 exited with status 1" when using GetUnitPropertiesContext #424

Closed
patrickjane opened this issue Jul 3, 2023 · 1 comment

Comments

@patrickjane
Copy link

I am using this library in my program, and when I manually start the program, everything is fine. The programm will call dbus.NewUserConnectionContext() to establish a new dbus connection, and afterwards it will call GetUnitPropertiesContext() to retrieve the status of a given systemd unit. This works well.

However, I also put this program in a dedicate systemd unit to start it upon boot. In this case, after reboot of the machine, the above does no longer work. Instead, GetUnitPropertiesContext() will give me the error: Process org.freedesktop.systemd1 exited with status 1

Since the status query is triggered via HTTP at regular intervals, my program will retry this every couple of seconds, but it will fail all the time.
However, when I do a systemctl restart myprogram.service, it will instantly be able to query the status using the above mentioned method.

I don't quite understand what is going on here. On the one hand, I assume it has to do with systemd dependencies, and I should put dependencies to dbus/systemd to myprogram.service - which I have tried, but did not help. On the other hand, even if this was a race condition during boot, I do not understand why a simple restart of my program solves the issue. Does dbus.NewUserConnectionContext() create some shared/global context which does not reset after conn.Close() and thus persists the observer error until my program restarts? It does not make sense to me.

This is the code which is called every 5 seconds:

func (s *SystemdPlatform) HandleStatus(ctx context.Context, unitName string) ([]byte, error) {
	conn, err := dbus.NewUserConnectionContext(ctx)

	if err != nil {
		log.Error().Msgf("Failed to connect to DBUS: %s", err)
		return nil, err
	}

	defer conn.Close()

	props, err := conn.GetUnitPropertiesContext(ctx, unitName)

	if err != nil {
		log.Error().Msgf("Failed to retrieve systemd status for service '%s': %s", unitName, err)
		return nil, err
	}

[...]
}
@patrickjane
Copy link
Author

Seems like a user instance of systemd is not yet spawned and this leads to the program not being able to query status right after boot.

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

No branches or pull requests

1 participant