From 967388395f9f861c745c91c5d5ef4c4bf726d122 Mon Sep 17 00:00:00 2001 From: Tobias Waldekranz Date: Thu, 5 Dec 2024 10:49:39 +0100 Subject: [PATCH] test: netns: Avoid infinite hangs on exceptions in `.call()`s If the callable threw an exception, then the process running inside the netns would exit before sending back a value on the tx pipe, which meant that `rx.recv()` would hang in the original process. Make sure that we catch any exceptions, and send that instead of the result over the pipe in those scenarios. Then in the original process, re-raise the exception. --- test/infamy/netns.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/test/infamy/netns.py b/test/infamy/netns.py index 8f3cbe11..daa970b7 100644 --- a/test/infamy/netns.py +++ b/test/infamy/netns.py @@ -139,6 +139,10 @@ class IsolatedMacVlans: f"{a[0]:02x}:{a[1]:02x}:{a[2]:02x}:" + \ f"{a[3]:02x}:{a[4]:02x}:{a[5]:02x}" + class CallException: + def __init__(self, inner): + self.inner = inner + def _ns_call(self, fn, tx): pid = self.sleeper.pid @@ -150,7 +154,12 @@ class IsolatedMacVlans: setns(nns, CLONE_NEWNET) os.close(nns) - tx.send(fn()) + try: + ret = fn() + except Exception as e: + ret = IsolatedMacVlans.CallException(e) + + tx.send(ret) tx.close() def call(self, fn): @@ -161,6 +170,9 @@ class IsolatedMacVlans: ret = rx.recv() rx.close() proc.join() + + if type(ret) == IsolatedMacVlans.CallException: + raise ret.inner return ret def _mangle_subprocess_args(self, args, kwargs):