Skip to content

Instantly share code, notes, and snippets.

@oconnor663
Last active July 2, 2024 01:09
Show Gist options
  • Save oconnor663/5e16388ede4101fae951d74ef0ce42dc to your computer and use it in GitHub Desktop.
Save oconnor663/5e16388ede4101fae951d74ef0ce42dc to your computer and use it in GitHub Desktop.
Python resource leak demo
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]
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'
@oconnor663
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment