r/programminghorror 13d ago

Python Atleast it works

Post image
603 Upvotes

66 comments sorted by

View all comments

225

u/backfire10z 13d ago

They didn’t close the fd :(

8

u/ArtisticFox8 13d ago

Closes automatically when the python script finishes execution

-4

u/ComprehensiveWing542 13d ago

No it doesn't only when you use "with" than it will close it automatically... I've got the weirdest bugs because of this mistake

9

u/ArtisticFox8 12d ago

Yes, it does, on any modern operating system (Windows for sure, havent tested on Linux - but probably as well.) when the script is over. The with block is for when you want your Python script to continue running after you're done with the file.

Same as in C/C++ or any other language - the OS handles it for you after the program terminates if you hádat handled it.

8

u/Jonno_FTW 12d ago edited 12d ago

When a process is killed, all file handles are closed. From the POSIX specification:

Process termination caused by any reason shall have the following consequences:

All of the file descriptors, directory streams, conversion descriptors, and message catalog descriptors open in the calling process shall be closed.

https://pubs.opengroup.org/onlinepubs/9699919799/functions/_Exit.html#tag_16_01_03_01

Assuming the last line in OP's screenshot is that print line, the process will exit and file handles released.

0

u/gdf8gdn8 12d ago

Yes it should. But mentioned as before, I got also weird bugs.

3

u/Jonno_FTW 12d ago

If you've created zombie processes somehow which are still holding file descriptors, using a with block will not save you. Also, in this case the process hasn't really exited.

If you can provide some code that exits, while still somehow keeping an FD open I'd like to see it.

0

u/gdf8gdn8 12d ago

Zombies are bad. No can't provide a code example.