Skip to content

Commit 406afac

Browse files
phlogistonjohnmergify[bot]
authored andcommitted
commands: time out waiting for conditions after 5min
Put a time bounds on the code that waits for the conditions using a context manager that wraps alarm. Signed-off-by: John Mulligan <[email protected]>
1 parent 07f3f87 commit 406afac

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

sambacc/commands/run.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>
1717
#
1818

19+
import contextlib
1920
import logging
21+
import signal
2022
import time
23+
import typing
2124

2225
from sambacc import samba_cmds
2326
import sambacc.paths as paths
@@ -103,16 +106,32 @@ def _run_container_args(parser):
103106
)
104107

105108

109+
_COND_TIMEOUT = 5 * 60
110+
111+
112+
@contextlib.contextmanager
113+
def _timeout(timeout: int) -> typing.Iterator[None]:
114+
def _handler(sig: int, frame: typing.Any) -> None:
115+
raise RuntimeError("timed out waiting for conditions")
116+
117+
signal.signal(signal.SIGALRM, _handler)
118+
signal.alarm(timeout)
119+
yield
120+
signal.alarm(0)
121+
signal.signal(signal.SIGALRM, signal.SIG_DFL)
122+
123+
106124
@commands.command(name="run", arg_func=_run_container_args)
107125
def run_container(ctx: Context) -> None:
108126
"""Run a specified server process."""
109127
if ctx.cli.no_init and ctx.cli.setup:
110128
raise Fail("can not specify both --no-init and --setup")
111129

112130
if ctx.cli.wait_for:
113-
conditions = [_wait_for_conditions[n]() for n in ctx.cli.wait_for]
114-
while not all(c.met(ctx) for c in conditions):
115-
time.sleep(1)
131+
with _timeout(_COND_TIMEOUT):
132+
conditions = [_wait_for_conditions[n]() for n in ctx.cli.wait_for]
133+
while not all(c.met(ctx) for c in conditions):
134+
time.sleep(1)
116135

117136
# running servers expect to make use of ctdb whenever it is configured
118137
ctx.expects_ctdb = True

0 commit comments

Comments
 (0)