Skip to contentSkip to conversation

tinfoil: Ensure sockets don't leak even when exceptions occur

PATCH New Richard Purdie 1 participant 1 message Sep 12, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tinfoil: Ensure sockets don't leak even when exceptions occur

We're seeing leaking open socket connections when errors occur and tinfoil is in use. Improve the exception handling so the sockets are closed even if exceptions occur, allowing more robust behaviour when things go wrong.

---
 lib/bb/tinfoil.py | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)
lib/bb/tinfoil.py +16 -12
index 5755e5a346..2fb1bb7d27 100644
@@ -806,18 +806,22 @@ class Tinfoil:
         prepare() has been called, or use a with... block when you create
         the tinfoil object which will ensure that it gets called.
         """
-        if self.server_connection:
-            self.run_command('clientComplete')
-            _server_connections.remove(self.server_connection)
-            bb.event.ui_queue = []
-            self.server_connection.terminate()
-            self.server_connection = None
-
-        # Restore logging handlers to how it looked when we started
-        if self.oldhandlers:
-            for handler in self.logger.handlers:
-                if handler not in self.oldhandlers:
-                    self.logger.handlers.remove(handler)
+        try:
+            if self.server_connection:
+                try:
+                    self.run_command('clientComplete')
+                finally:
+                    _server_connections.remove(self.server_connection)
+                    bb.event.ui_queue = []
+                    self.server_connection.terminate()
+                    self.server_connection = None
+
+        finally:
+            # Restore logging handlers to how it looked when we started
+            if self.oldhandlers:
+                for handler in self.logger.handlers:
+                    if handler not in self.oldhandlers:
+                        self.logger.handlers.remove(handler)
 
     def _reconvert_type(self, obj, origtypename):
         """
Signature · 1 line
2.25.1