BUG? File handles not being closed/released at all

  • Windows 2025 v24H2 (OS Build 26100.6905) with Python 3.14.
  • Program is running in Windows Terminal, not a cmd.exe box.

I’m posting this as a new thread as this may be a bug and I have narrowed down what is happening.

Since the file handles are not being closed, when I exit the debugger I get 3 I/O errors. Minimal code shows the file handle is still populated whether I use the WITH clause or manual close.

"""file: errfile.py
errfile file handle is not being closed and causes 3 errors when exiting debugger every time.
"""

import os

errfn = "errtest.txt"
with open(errfn, 'w') as errfile: 
    errfile.write("This is a test line\n")
    
print(f"1) errfile={errfile}")

errfile = open(errfn, 'w')
errfile.write("This is test line 2\n")
errfile.close()

print(f"2) errfile={errfile}")

Command line: python -m pdb errfile.py

Program output is:

>python -m pdb errfile.py
> e:\myprog\errfile.py(1)<module>()
-> """errfile file handle is not being closed and causes 3 errors when exiting debugger every time.
(Pdb) c
1) errfile=<_io.TextIOWrapper name='errtest.txt' mode='w' encoding='cp1252'>
2) errfile=<_io.TextIOWrapper name='errtest.txt' mode='w' encoding='cp1252'>
The program finished and will be restarted

The errors are:

> e:\myprog\errfile.py(1)<module>()
-> """errfile file handle is not being closed and causes 3 errors when exiting debugger every time.
(Pdb) q
*** SystemExit: None
(Pdb) Traceback (most recent call last):
  File "C:\Program Files\Python314\Lib\pdb.py", line 3601, in main
    pdb._run(target)
    ~~~~~~~~^^^^^^^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 2522, in _run
    self.run(target.code)
    ~~~~~~~~^^^^^^^^^^^^^
  File "C:\Program Files\Python314\Lib\bdb.py", line 899, in run
    exec(cmd, globals, locals)
    ~~~~^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
  File "E:\myprog\errfile.py", line 1, in <module>
    """errfile file handle is not being closed and causes 3 errors when exiting debugger every time.
    """
  File "C:\Program Files\Python314\Lib\bdb.py", line 94, in wrapper
    ret = func(frame, *args)
  File "C:\Program Files\Python314\Lib\bdb.py", line 130, in line_callback
    frame.f_trace(frame, 'line', None)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python314\Lib\bdb.py", line 284, in trace_dispatch
    return self.dispatch_line(frame)
           ~~~~~~~~~~~~~~~~~~^^^^^^^
  File "C:\Program Files\Python314\Lib\bdb.py", line 310, in dispatch_line
    self.user_line(frame)
    ~~~~~~~~~~~~~~^^^^^^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 547, in user_line
    self.interaction(frame, None)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 736, in interaction
    self._cmdloop()
    ~~~~~~~~~~~~~^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 601, in _cmdloop
    self.cmdloop()
    ~~~~~~~~~~~~^^
  File "C:\Program Files\Python314\Lib\cmd.py", line 137, in cmdloop
    line = input(self.prompt)
ValueError: I/O operation on closed file.

Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> c:\program files\python314\lib\cmd.py(137)cmdloop()
-> line = input(self.prompt)
(Pdb) Traceback (most recent call last):
  File "C:\Program Files\Python314\Lib\pdb.py", line 3601, in main
    pdb._run(target)
    ~~~~~~~~^^^^^^^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 2522, in _run
    self.run(target.code)
    ~~~~~~~~^^^^^^^^^^^^^
  File "C:\Program Files\Python314\Lib\bdb.py", line 899, in run
    exec(cmd, globals, locals)
    ~~~~^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
  File "E:\myprog\errfile.py", line 1, in <module>
    """errfile file handle is not being closed and causes 3 errors when exiting debugger every time.
    """
  File "C:\Program Files\Python314\Lib\bdb.py", line 94, in wrapper
    ret = func(frame, *args)
  File "C:\Program Files\Python314\Lib\bdb.py", line 130, in line_callback
    frame.f_trace(frame, 'line', None)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python314\Lib\bdb.py", line 284, in trace_dispatch
    return self.dispatch_line(frame)
           ~~~~~~~~~~~~~~~~~~^^^^^^^
  File "C:\Program Files\Python314\Lib\bdb.py", line 310, in dispatch_line
    self.user_line(frame)
    ~~~~~~~~~~~~~~^^^^^^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 547, in user_line
    self.interaction(frame, None)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 736, in interaction
    self._cmdloop()
    ~~~~~~~~~~~~~^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 601, in _cmdloop
    self.cmdloop()
    ~~~~~~~~~~~~^^
  File "C:\Program Files\Python314\Lib\cmd.py", line 137, in cmdloop
    line = input(self.prompt)
ValueError: I/O operation on closed file.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Program Files\Python314\Lib\pdb.py", line 3627, in <module>
    pdb.main()
    ~~~~~~~~^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 3614, in main
    pdb.interaction(None, e)
    ~~~~~~~~~~~~~~~^^^^^^^^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 736, in interaction
    self._cmdloop()
    ~~~~~~~~~~~~~^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 601, in _cmdloop
    self.cmdloop()
    ~~~~~~~~~~~~^^
  File "C:\Program Files\Python314\Lib\cmd.py", line 137, in cmdloop
    line = input(self.prompt)
ValueError: I/O operation on closed file.

  1. Should I report this as a bug on Github?

The code does closes the files as I showed in your other thread.

The traceback shows the reverse of what you are claiming that a file is closed and pdb tried to use it.

Why do I get the IO errors only when the program is finished and I exit the debugger?

I have 2 files that I write to: a log and error file. I cannot find anywhere those files are being written after they are closed. After these files are closed I only print to the screen using print().

Seems that pdb wants to do input from a closed file, stdin maybe?
I don’t use pdb so cannot comment on why that should be.

Note that when I run this test in PyCharm, I do not experience any errors and I too am running on Windows with Python v3.14.

The output that I observe when running your script is:

1) errfile=<_io.TextIOWrapper name='errtest.txt' mode='w' encoding='cp1252'>
2) errfile=<_io.TextIOWrapper name='errtest.txt' mode='w' encoding='cp1252'>

When I run it from the command prompt per your exact instructions, I observe this:

errfile_test

Can you try uninstalling and re-installing Python.

Do you get them even if you don’t open and close files? Look into that; it doesn’t look like the files are indeed being leaked here. I suspect it’s unrelated.

I am reading a spreadsheet like this

    try: 
        # df = pandas.read_excel(options.inputfile)
        with pandas.ExcelFile(options.inputfile) as xls:
            df = pandas.read_excel(xls)
        
    except PermissionError: 
        print(f"{procname} ERROR: File '{options.inputfile}' might be opened. Please close it then rerun program")
        sys.exit()
    except Exception as e:
        print(f"{procname} ERROR-openss opening spreadsheet {options.inputfile}. {e}")
        sys.exit()
    if xls is None or not hasattr(xls,'_open'):
        print(f"{procname} xls File is closed.")
    else: 
        print(f"{procname} xls File is still open.")

and confirmed the read spreadsheet is closed.

I also write a spreadsheet but I have verified that that file is being closed.

Go with the first example, you shouldn’t need the two-step thing here. pandas.read_excel is quite happy to take a file name, and then it’s its responsibility to open and close the file.

I’m not sure that that’s a valid way to see whether the file is open.

I fixed it! I had this alias in my .pdbrc file in the same dir as my program: alias q quit() and when I commented it out, I get no more errors. The errors were all coming from pdb.

Now why would that alias cause an error in the first place?

This is a common alias I use in 25+ Python projects with a .pdbrc, I’d like to use it without errors. Could this be the bug?

And from an AI:

In Python 3.14, using aliases in the pdb debugger can cause issues, especially with commands that involve I/O operations, like quitting the debugger. The command alias q quit() can lead to I/O errors because the quit() function can interfere with how pdb handles its input and output.

After removing the alias from my local .pdbrc file “q” still works to quit. It’s a nice shortcut. “q” did not work in Python 3.11.5 pdb. I guess my problem is solved after all.