forked from clan/clan-core
cmd.py: Fix deadlock because of incorrect select usage
This commit is contained in:
parent
3351f87a7f
commit
0941367bb3
@ -25,7 +25,10 @@ def handle_output(process: subprocess.Popen, log: Log) -> tuple[str, str]:
|
|||||||
stdout_buf = b""
|
stdout_buf = b""
|
||||||
stderr_buf = b""
|
stderr_buf = b""
|
||||||
|
|
||||||
while len(rlist) != 0:
|
# Note: We need to read till the process is done, otherwise we might block
|
||||||
|
# forever because the process might be waiting for us to read from the pipe
|
||||||
|
# before it can continue and will block in the write call.
|
||||||
|
while True:
|
||||||
r, _, _ = select.select(rlist, [], [], 0)
|
r, _, _ = select.select(rlist, [], [], 0)
|
||||||
|
|
||||||
def handle_fd(fd: IO[Any] | None) -> bytes:
|
def handle_fd(fd: IO[Any] | None) -> bytes:
|
||||||
@ -33,7 +36,6 @@ def handle_output(process: subprocess.Popen, log: Log) -> tuple[str, str]:
|
|||||||
read = os.read(fd.fileno(), 4096)
|
read = os.read(fd.fileno(), 4096)
|
||||||
if len(read) != 0:
|
if len(read) != 0:
|
||||||
return read
|
return read
|
||||||
rlist.remove(fd)
|
|
||||||
return b""
|
return b""
|
||||||
|
|
||||||
ret = handle_fd(process.stdout)
|
ret = handle_fd(process.stdout)
|
||||||
@ -48,6 +50,11 @@ def handle_output(process: subprocess.Popen, log: Log) -> tuple[str, str]:
|
|||||||
sys.stderr.buffer.write(ret)
|
sys.stderr.buffer.write(ret)
|
||||||
sys.stderr.flush()
|
sys.stderr.flush()
|
||||||
stderr_buf += ret
|
stderr_buf += ret
|
||||||
|
|
||||||
|
# Check if the process is still running
|
||||||
|
if process.poll() is not None:
|
||||||
|
break
|
||||||
|
|
||||||
return stdout_buf.decode("utf-8"), stderr_buf.decode("utf-8")
|
return stdout_buf.decode("utf-8"), stderr_buf.decode("utf-8")
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import subprocess
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from clan_cli.errors import ClanError
|
from clan_cli.cmd import Log, run
|
||||||
|
|
||||||
from ..machines.machines import Machine
|
from ..machines.machines import Machine
|
||||||
|
|
||||||
@ -17,15 +16,14 @@ def generate_secrets(machine: Machine) -> None:
|
|||||||
env["PYTHONPATH"] = ":".join(sys.path) # TODO do this in the clanCore module
|
env["PYTHONPATH"] = ":".join(sys.path) # TODO do this in the clanCore module
|
||||||
|
|
||||||
print(f"generating secrets... {machine.generate_secrets}")
|
print(f"generating secrets... {machine.generate_secrets}")
|
||||||
proc = subprocess.run(
|
run(
|
||||||
[machine.generate_secrets],
|
[machine.generate_secrets],
|
||||||
env=env,
|
env=env,
|
||||||
|
error_msg="failed to generate secrets",
|
||||||
|
log=Log.BOTH,
|
||||||
)
|
)
|
||||||
|
|
||||||
if proc.returncode != 0:
|
print("successfully generated secrets")
|
||||||
raise ClanError("failed to generate secrets")
|
|
||||||
else:
|
|
||||||
print("successfully generated secrets")
|
|
||||||
|
|
||||||
|
|
||||||
def generate_command(args: argparse.Namespace) -> None:
|
def generate_command(args: argparse.Namespace) -> None:
|
||||||
|
Loading…
Reference in New Issue
Block a user