You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildstream.apache.org by tv...@apache.org on 2022/02/28 08:33:32 UTC

[buildstream] 02/04: sandbox.py: Make the sandbox a context manager and give it a _cleanup() method

This is an automated email from the ASF dual-hosted git repository.

tvb pushed a commit to branch tristan/fix-leaking-grpc-threads
in repository https://gitbox.apache.org/repos/asf/buildstream.git

commit 531254a908d9c20ba68ab04e5567deb1c22f0365
Author: Tristan van Berkom <tr...@codethink.co.uk>
AuthorDate: Mon Feb 28 17:27:25 2022 +0900

    sandbox.py: Make the sandbox a context manager and give it a _cleanup() method
    
    Now sandbox classes can cleanup resources in their _cleanup() method when
    the sandbox is discarded.
---
 src/buildstream/sandbox/sandbox.py | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/src/buildstream/sandbox/sandbox.py b/src/buildstream/sandbox/sandbox.py
index 707951d..f309382 100644
--- a/src/buildstream/sandbox/sandbox.py
+++ b/src/buildstream/sandbox/sandbox.py
@@ -102,6 +102,20 @@ class Sandbox:
         # Pending command batch
         self.__batch = None
 
+    # __enter__()
+    #
+    # Called when entering the with-statement context.
+    #
+    def __enter__(self) -> "Sandbox":
+        return self
+
+    # __exit__()
+    #
+    # Called when exiting the with-statement context.
+    #
+    def __exit__(self, exc_type, exc_value, traceback) -> None:
+        self._cleanup()
+
     def get_virtual_directory(self) -> Directory:
         """Fetches the sandbox root directory as a virtual Directory.
 
@@ -255,6 +269,13 @@ class Sandbox:
     #    Abstract Methods for Sandbox implementations   #
     #####################################################
 
+    # _cleanup():
+    #
+    # Abstract method to release resources when the sandbox is discarded
+    #
+    def _cleanup(self):
+        pass
+
     # _run()
     #
     # Abstract method for running a single command