gemini-code-assist[bot] commented on code in PR #38744:
URL: https://github.com/apache/beam/pull/38744#discussion_r3324700758


##########
sdks/python/apache_beam/runners/portability/portable_runner_test.py:
##########
@@ -295,6 +295,22 @@ def create_options(self):
 class PortableRunnerTestWithSubprocesses(PortableRunnerTest):
   _use_subprocesses = True
 
+  # TODO(https://github.com/grpc/grpc/issues/37710): Remove once fixed.
+  @classmethod
+  def setUpClass(cls):
+    cls._old_fork_support = os.environ.get('GRPC_ENABLE_FORK_SUPPORT')
+    os.environ['GRPC_ENABLE_FORK_SUPPORT'] = 'false'
+    super().setUpClass()
+
+  # TODO(https://github.com/grpc/grpc/issues/37710): Remove once fixed.
+  @classmethod
+  def tearDownClass(cls):
+    if cls._old_fork_support is None:
+      os.environ.pop('GRPC_ENABLE_FORK_SUPPORT', None)
+    else:
+      os.environ['GRPC_ENABLE_FORK_SUPPORT'] = cls._old_fork_support
+    super().tearDownClass()

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   If `super().setUpClass()` raises an exception, `tearDownClass` will not be 
executed by the test runner, which can leak the modified 
`GRPC_ENABLE_FORK_SUPPORT` environment variable to other test classes running 
in the same process.
   
   Using `self.addClassCleanup` (available in Python 3.8+) is a cleaner and 
more robust approach. It ensures that the cleanup function is executed even if 
`setUpClass` fails, and it allows us to completely eliminate the 
`tearDownClass` method.
   
   ```python
     @classmethod
     def setUpClass(cls):
       old_fork_support = os.environ.get('GRPC_ENABLE_FORK_SUPPORT')
       os.environ['GRPC_ENABLE_FORK_SUPPORT'] = 'false'
       def restore_fork_support():
         if old_fork_support is None:
           os.environ.pop('GRPC_ENABLE_FORK_SUPPORT', None)
         else:
           os.environ['GRPC_ENABLE_FORK_SUPPORT'] = old_fork_support
       cls.addClassCleanup(restore_fork_support)
       super().setUpClass()
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to