|
10 | 10 | import signal |
11 | 11 | import subprocess |
12 | 12 | import sysconfig |
| 13 | +import textwrap |
13 | 14 | import time |
14 | 15 | try: |
15 | 16 | import resource |
@@ -568,6 +569,33 @@ def test_urandom_subprocess(self): |
568 | 569 | data2 = self.get_urandom_subprocess(16) |
569 | 570 | self.assertNotEqual(data1, data2) |
570 | 571 |
|
| 572 | + def test_urandom_fd_non_inheritable(self): |
| 573 | + # Issue #23458: os.urandom() keeps a file descriptor open, but it |
| 574 | + # must be non inheritable |
| 575 | + fd_status = test_support.findfile("fd_status.py", subdir="subprocessdata") |
| 576 | + |
| 577 | + # Need a two subprocesses because the Python test suite opens other |
| 578 | + # inheritable file descriptors, whereas the test is specific to |
| 579 | + # os.urandom() file descriptor |
| 580 | + code = textwrap.dedent(""" |
| 581 | + import os |
| 582 | + import subprocess |
| 583 | + import sys |
| 584 | +
|
| 585 | + # Ensure that the /dev/urandom file descriptor is open |
| 586 | + os.urandom(1) |
| 587 | +
|
| 588 | + exitcode = subprocess.call([sys.executable, %r], |
| 589 | + close_fds=False) |
| 590 | + sys.exit(exitcode) |
| 591 | + """ % fd_status) |
| 592 | + |
| 593 | + proc = subprocess.Popen([sys.executable, "-c", code], |
| 594 | + stdout=subprocess.PIPE, close_fds=True) |
| 595 | + output, error = proc.communicate() |
| 596 | + open_fds = set(map(int, output.rstrip().split(','))) |
| 597 | + self.assertEqual(open_fds - set(range(3)), set()) |
| 598 | + |
571 | 599 |
|
572 | 600 | HAVE_GETENTROPY = (sysconfig.get_config_var('HAVE_GETENTROPY') == 1) |
573 | 601 |
|
|
0 commit comments