service_runner: Fix starting a service twice

This commit is contained in:
Qubasa
2025-10-13 11:47:25 +02:00
parent 3f07f6ac79
commit 2b616575e1
2 changed files with 14 additions and 12 deletions

View File

@@ -301,14 +301,12 @@ def test_start_service_twice_transient(service_manager: ServiceManagerProtocol)
status = service_manager.get_status("simple-service")
assert status == "running"
# Try to start the same service again - this should fail
# systemd won't allow starting a unit with the same name
with pytest.raises(ClanError, match="Failed to start service"):
service_manager.start_service(
name="simple-service",
command=["sleep", "300"],
autostart=False,
)
# Try to start the same service again - this shouldn't fail
service_manager.start_service(
name="simple-service",
command=["sleep", "300"],
autostart=False,
)
# Original service should still be running
status = service_manager.get_status("simple-service")

View File

@@ -184,6 +184,13 @@ class SystemdUserService:
service_name = self._service_name(name)
self._check_executable(command)
# Stop and reset any existing service (allows redefining failed/running services)
self._systemctl("stop", service_name)
run(
["systemctl", "--user", "reset-failed", f"{service_name}.service"],
RunOpts(check=False),
)
if autostart:
if group:
self._create_target_file(group)
@@ -223,10 +230,7 @@ class SystemdUserService:
cmd.extend(command)
result = run(cmd, RunOpts(check=False))
if result.returncode != 0:
msg = f"Failed to start service: {result.stderr}"
raise ClanError(msg)
result = run(cmd, RunOpts(error_msg="Failed to start service"))
return name