Last active
July 2, 2024 01:09
-
-
Save oconnor663/5e16388ede4101fae951d74ef0ce42dc to your computer and use it in GitHub Desktop.
Python resource leak demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def open_ten_files(): | |
files = [open("/dev/null") for _ in range(10)] | |
silly_map1 = {"files": files} | |
silly_map2 = {"files": files} | |
silly_map1["other_map"] = silly_map2 | |
silly_map2["other_map"] = silly_map1 | |
return silly_map1 | |
mylist = [] | |
max_len = 5 | |
for _ in range(1000): | |
mylist.append(open_ten_files()) | |
if len(mylist) > max_len: | |
del mylist[0] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Traceback (most recent call last): | |
File "/tmp/test.py", line 13, in <module> | |
mylist.append(open_ten_files()) | |
^^^^^^^^^^^^^^^^ | |
File "/tmp/test.py", line 2, in open_ten_files | |
files = [open("/dev/null") for _ in range(10)] | |
^^^^^^^^^^^^^^^^^ | |
OSError: [Errno 24] Too many open files: '/dev/null' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://godbolt.org/z/fPa341fMr