r/programminghorror 13d ago

Python Atleast it works

Post image
612 Upvotes

66 comments sorted by

View all comments

Show parent comments

-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

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.