]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-124986: Fix test_no_leaking in test_subprocess on NetBSD and FreeBSD (GH...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 14 Apr 2025 06:38:13 +0000 (08:38 +0200)
committerGitHub <noreply@github.com>
Mon, 14 Apr 2025 06:38:13 +0000 (06:38 +0000)
On platforms where the file descriptor limit is larger than FD_SETSIZE
that test was always skipped (FreeBSD) or always failing (NetBSD).
(cherry picked from commit f7b24ffefda839f367b048c06879df6bded128a1)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/test/test_subprocess.py

index bf77779b32c8ef3f622199b1fb741c20f43b6225..7d72737e9cb066ab9f46867a50391ab25316aeab 100644 (file)
@@ -40,6 +40,10 @@ try:
     import grp
 except ImportError:
     grp = None
+try:
+    import resource
+except ImportError:
+    resource = None
 
 try:
     import fcntl
@@ -1210,6 +1214,16 @@ class ProcessTestCase(BaseTestCase):
             max_handles = 1026 # too much for most UNIX systems
         else:
             max_handles = 2050 # too much for (at least some) Windows setups
+        if resource:
+            # And if it is not too much, try to make it too much.
+            try:
+                soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
+                if soft > 1024:
+                    resource.setrlimit(resource.RLIMIT_NOFILE, (1024, hard))
+                    self.addCleanup(resource.setrlimit, resource.RLIMIT_NOFILE,
+                                    (soft, hard))
+            except (OSError, ValueError):
+                pass
         handles = []
         tmpdir = tempfile.mkdtemp()
         try:
@@ -1224,7 +1238,9 @@ class ProcessTestCase(BaseTestCase):
             else:
                 self.skipTest("failed to reach the file descriptor limit "
                     "(tried %d)" % max_handles)
-            # Close a couple of them (should be enough for a subprocess)
+            # Close a couple of them (should be enough for a subprocess).
+            # Close lower file descriptors, so select() will work.
+            handles.reverse()
             for i in range(10):
                 os.close(handles.pop())
             # Loop creating some subprocesses. If one of them leaks some fds,