From df7fcd29674259c22c3934296824d9c70c17e8a5 Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Thu, 1 Feb 2024 22:21:04 -0500 Subject: [PATCH] Attempt at process pipe full close --- .gitignore | 1 + client/broadcast.py | 26 +++++++++++++++++++++++--- client/fstream.py | 12 ++++++------ client/watch.py | 28 ++++++++++++++++++++++++---- 4 files changed, 54 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index ce7e61a..f31153e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /users/ /client/config/ +*.swp \ No newline at end of file diff --git a/client/broadcast.py b/client/broadcast.py index 6aeaa18..05c14ac 100755 --- a/client/broadcast.py +++ b/client/broadcast.py @@ -14,6 +14,8 @@ sp = pUp(s) import toml import subprocess +def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) + def addressStr(addr): return addr[0] + ":" + str(addr[1]) @@ -100,7 +102,25 @@ def main(): procs.append(subprocess.Popen(cmds[i],stdin=procs[i - 1].stdout,stdout=subprocess.PIPE)) procs.append(subprocess.Popen(cmds[-1],stdin=procs[-1].stdout)) - for proc in reversed(procs): - proc.wait() - + # Pwetty pwease add thew funcshun owo uwu xoxoxox + import time + running = True + while running: + for proc in procs: + if proc.poll() is not None: + eprint("Process died: " + str(proc.args)) + running = False + break + time.sleep(1) + + eprint("Terminating process chain (" + str(len(procs)) + ")...") + + for proc in procs: + eprint("Closing: " + str(proc.args)) + try: + proc.terminate() + proc.wait() + except Exception as ex: + eprint("Error waiting for process!!! " + str(ex)) + main() diff --git a/client/fstream.py b/client/fstream.py index 694684e..1700187 100755 --- a/client/fstream.py +++ b/client/fstream.py @@ -44,10 +44,10 @@ def listToCommand(lst): def recv(conn,l): start = time.process_time() timeo = conn.gettimeout() - bytes = b"" + bytes = bytearray() while l > 0: b = conn.recv(l) - if b == b"": raise ConnectionResetError + if not b: raise ConnectionResetError if time.process_time() - start > timeo: raise TimeoutError bytes += b l -= len(b) @@ -114,11 +114,11 @@ def main(): eprint("Receiving token...") while True: data = connection.recv(bufferSize) - if data != b"": + if data: token += data continue - if token == b"": + if not token: eprint("Connection closed: failure") os.exit(1) else: @@ -134,7 +134,7 @@ def main(): eprint("Receiving data...") while True: data = connection.recv(bufferSize) - if data == b"": + if not data: eprint("Connection closed.") return unbufferedStdout.write(data) @@ -147,7 +147,7 @@ def main(): eprint("Sending data...") while True: data = sys.stdin.buffer.read(bufferSizeStdin) - if data == b"": + if not data: return connection.sendall(data) except: diff --git a/client/watch.py b/client/watch.py index e270409..9698e52 100755 --- a/client/watch.py +++ b/client/watch.py @@ -14,6 +14,8 @@ sp = pUp(s) import toml import subprocess +def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) + def addressStr(addr): return addr[0] + ":" + str(addr[1]) @@ -81,8 +83,26 @@ def main(): for i in range(1,len(cmds) - 1): procs.append(subprocess.Popen(cmds[i],stdin=procs[i - 1].stdout,stdout=subprocess.PIPE)) procs.append(subprocess.Popen(cmds[-1],stdin=procs[-1].stdout)) - - for proc in reversed(procs): - proc.wait() - + + # Pwetty pwease add thew funcshun owo uwu xoxoxox () + import time + running = True + while running: + for proc in procs: + if proc.poll() is not None: + eprint("Process died: " + str(proc.args)) + running = False + break + time.sleep(1) + + eprint("Terminating process chain (" + str(len(procs)) + ")...") + + for proc in procs: + eprint("Closing: " + str(proc.args)) + try: + proc.terminate() + proc.wait() + except Exception as ex: + eprint("Error waiting for process!!! " + str(ex)) + main()