bpd tests: close only existing sockets
Close sockets in `finally`-clauses only after they have actually been created.
@@ -324,19 +324,25 @@ class BPDTestHelper(unittest.TestCase, TestHelper):
)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ try:
sock.connect((host, port))
- sock2 = None
if second_client:
sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ try:
sock2.connect((host, port))
- yield MPCClient(sock, do_hello), MPCClient(sock2, do_hello)
+ yield (
+ MPCClient(sock, do_hello),
+ MPCClient(sock2, do_hello),
+ )
+ finally:
+ sock2.close()
+
else:
yield MPCClient(sock, do_hello)
finally:
sock.close()
- if sock2:
- sock2.close()
+ finally:
server.terminate()
server.join(timeout=0.2)
