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.

classification
Title: Multiple fdopen() on mkstemp() descriptor crashes py27 interpreter
Type: crash Stage:
Components: Windows Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Thomas Krijnen, eryksun, paul.moore, steve.dower, tim.golden, vstinner, zach.ware
Priority: normal Keywords:

Created on 2015-07-25 09:57 by Thomas Krijnen, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg247329 - (view) Author: Thomas Krijnen (Thomas Krijnen) Date: 2015-07-25 09:57
Following code crashes my Python 2.7.9 interpreter on Windows:

import os, tempfile
a, b = tempfile.mkstemp()
f = os.fdopen(a, "wb")
f = os.fdopen(a, "wb")
f.write("beer")
f.close()
msg247397 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2015-07-25 21:45
Reassigning "f" closes the first file object, but not before the second file object gets created. You can write to the already-closed file, assuming the write is small enough to not flush the FILE stream buffer (e.g. "beer", given an empty 4 KiB buffer). However, closing an already-closed file is an EBADF (bad file descriptor) error. In this case, Microsoft's C runtime defaults to calling an invalid parameter handler that terminates the process.

File operations have to be guarded to work around this, which isn't implemented for 2.x file objects. However, this check is implemented for functions in the os module. To avoid killing the process you could check os.fstat(f.fileno()), which raises OSError for a bad file descriptor.
msg247398 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-07-25 22:01
As eryksun explained, you have a bug in your example. You should fix your code. I would not call it a bug in Python, but more yet another bug (or "unexpected behaviour") of the C stdio of Windows. My list of bugs in the C stdio is already long:
https://haypo-notes.readthedocs.org/python.html#bugs-in-the-c-stdio-used-by-the-python-i-o

You should try to use the io module which is available since Python 2.6.

On Linux with Python 2.7.10, I get an OSError:

$ python x.py 
Traceback (most recent call last):
  File "x.py", line 7, in <module>
    f.close()
IOError: [Errno 9] Bad file descriptor

I suggest to close the issue as "not a bug".
msg247431 - (view) Author: Thomas Krijnen (Thomas Krijnen) Date: 2015-07-26 15:07
Thanks for the feedback. I realize the example code is rather bad, I was just surprised to see the interpreter actually crash. Good to know in py3 things are more robust. Closed.
History
Date User Action Args
2022-04-11 14:58:19adminsetgithub: 68904
2015-07-26 15:07:32Thomas Krijnensetstatus: open -> closed
resolution: not a bug
messages: + msg247431
2015-07-25 22:01:22vstinnersetmessages: + msg247398
2015-07-25 21:45:46eryksunsetnosy: + eryksun
messages: + msg247397
2015-07-25 10:03:20serhiy.storchakasetnosy: + vstinner
2015-07-25 09:57:50Thomas Krijnencreate