Commit 06463b40 authored by segfault's avatar segfault

Fix bug causing onionkitd to fail to start sometimes

machinectl sometimes incorrectly states that a container is
running even though it is not. This is now handled by calling
`machinectl terminate` in that case.
parent a5c615f0
......@@ -217,11 +217,7 @@ class ContainerManager(object):
sh.machinectl("bind", "--mkdir", self.name, source, target)
def is_running(self) -> bool:
try:
sh.machinectl("show", self.name)
return True
except sh.ErrorReturnCode_1:
return False
return container_is_running(self.name)
def is_ready(self) -> bool:
try:
......@@ -305,3 +301,20 @@ class ContainerManager(object):
raise
finally:
self.stop()
def container_is_running(container: str) -> bool:
try:
sh.machinectl("show", container)
except sh.ErrorReturnCode_1:
# Container is not running
return False
out = sh.systemctl("--no-pager", "show", "[email protected]%s.service" % container, "-p", "ActiveState")
state = out.replace("ActiveState=", "").strip()
if state in ("inactive", "failed"):
logger.warning("machinectl incorrectly states that the container is running. Terminating machine.")
sh.machinectl("terminate", container)
return False
return True
......@@ -6,6 +6,7 @@ import time
from onionkit.exceptions import MonitorError
from onionkit.service_status import Status
from onionkit.container import container_is_running
logger = getLogger()
......@@ -53,12 +54,8 @@ class SystemdManager(object):
(self.id, START_TIMEOUT, self._get_service_status_output()))
def is_running(self) -> bool:
try:
sh.machinectl("show", self.container)
except sh.ErrorReturnCode_1:
# Container is not running
if not container_is_running(self.container):
return False
active_state, sub_state = self._get_state()
return active_state == "active" and sub_state == "running"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment