diff --git a/pyiceberg/catalog/hive.py b/pyiceberg/catalog/hive.py index e558c8c30f..93ece35cbb 100644 --- a/pyiceberg/catalog/hive.py +++ b/pyiceberg/catalog/hive.py @@ -185,6 +185,11 @@ def __enter__(self) -> Client: try: self._transport.open() except (TypeError, TTransport.TTransportException): + # Close the old transport before reinitializing to prevent resource leaks + try: + self._transport.close() + except Exception: + pass # reinitialize _transport self._transport = self._init_thrift_transport() self._transport.open() diff --git a/pyproject.toml b/pyproject.toml index 1d94838081..ba3b5e2421 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -346,8 +346,6 @@ markers = [ # Turns a warning into an error filterwarnings = [ "error", - "ignore:A plugin raised an exception during an old-style hookwrapper teardown.", - "ignore:unclosed None: self._socket.listen() @@ -222,6 +223,7 @@ def run(self) -> None: try: client = self._socket.accept() if client: + self._clients.append(client) # Track the client client.write(self._response) client.flush() except Exception: @@ -233,6 +235,12 @@ def port(self) -> Optional[int]: return self._port def close(self) -> None: + # Close all client connections first + for client in self._clients: + try: + client.close() + except Exception: + pass self._socket.close() @@ -1392,6 +1400,7 @@ def test_create_hive_client_with_kerberos_using_context_manager( with client as open_client: assert open_client._iprot.trans.isOpen() + assert not open_client._iprot.trans.isOpen() # Use the context manager a second time to see if # closing and re-opening work as expected. with client as open_client: