Skip to content

Commit f7b24ff

Browse files
gh-124986: Fix test_no_leaking in test_subprocess on NetBSD and FreeBSD (GH-132476)
On platforms where the file descriptor limit is larger than FD_SETSIZE that test was always skipped (FreeBSD) or always failing (NetBSD).
1 parent 1fc1df8 commit f7b24ff

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

Lib/test/test_subprocess.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
import grp
4242
except ImportError:
4343
grp = None
44+
try:
45+
import resource
46+
except ImportError:
47+
resource = None
4448

4549
try:
4650
import fcntl
@@ -1211,6 +1215,16 @@ def test_no_leaking(self):
12111215
max_handles = 1026 # too much for most UNIX systems
12121216
else:
12131217
max_handles = 2050 # too much for (at least some) Windows setups
1218+
if resource:
1219+
# And if it is not too much, try to make it too much.
1220+
try:
1221+
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
1222+
if soft > 1024:
1223+
resource.setrlimit(resource.RLIMIT_NOFILE, (1024, hard))
1224+
self.addCleanup(resource.setrlimit, resource.RLIMIT_NOFILE,
1225+
(soft, hard))
1226+
except (OSError, ValueError):
1227+
pass
12141228
handles = []
12151229
tmpdir = tempfile.mkdtemp()
12161230
try:
@@ -1225,7 +1239,9 @@ def test_no_leaking(self):
12251239
else:
12261240
self.skipTest("failed to reach the file descriptor limit "
12271241
"(tried %d)" % max_handles)
1228-
# Close a couple of them (should be enough for a subprocess)
1242+
# Close a couple of them (should be enough for a subprocess).
1243+
# Close lower file descriptors, so select() will work.
1244+
handles.reverse()
12291245
for i in range(10):
12301246
os.close(handles.pop())
12311247
# Loop creating some subprocesses. If one of them leaks some fds,

0 commit comments

Comments
 (0)