This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: subprocess.Popen os.close(p2cread) in _execute_child can cause garbage collection of self.stdout to close a reused file descriptor
Type: Stage:
Components: Versions: Python 3.7, Python 3.6, Python 3.4, Python 3.5
Status: closed Resolution: third party
Dependencies: Superseder:
Assigned To: Nosy List: zmedico
Priority: normal Keywords:

Created on 2017-01-25 17:21 by zmedico, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg286268 - (view) Author: Zac Medico (zmedico) Date: 2017-01-25 17:21
In Popen _execute_child method, os.close(p2cread) closes a file descriptor which is referenced by self.stdout with closefd=True, created by this code:

   self.stdout =, 'rb', bufsize)

When self.stdout is finally garbage collected, it can close a file descriptor which has been reused since the os.close(p2cread) call. In the rare cases when this happens, it results in a traceback like the following:

Traceback (most recent call last):
  File "/usr/lib64/python3.4/", line 1418, in _execute_child
    part = _eintr_retry_call(, errpipe_read, 50000)
  File "/usr/lib64/python3.4/", line 491, in _eintr_retry_call
    return func(*args)
OSError: [Errno 9] Bad file descriptor

I have observed this issue with python 3.4.5, and the code flaw also appears to be present in 3.5, 3.6, and 3.7. This sort of garbage collection issue has been highlighted in the first comment of, but there is still a remaining problem with this os.close(p2cread) call.
msg286270 - (view) Author: Zac Medico (zmedico) Date: 2017-01-25 17:36
Actually, os.close(p2cread) closes a different file descriptor than c2pread, so my analysis is not correct. I don't know the precise cause of the EBADF error that I have observed. I suppose it could be some other code closing a file descriptor that belongs to a file object. Closing for now. Sorry for the noise.

I will follow up on the issue here:
Date User Action Args
2022-04-11 14:58:42adminsetgithub: 73559
2017-01-25 17:36:29zmedicosetstatus: open -> closed
resolution: third party
messages: + msg286270
2017-01-25 17:21:16zmedicocreate