csurfer / pyheatmagic

IPython magic command to profile and view your python code as a heat map.
MIT License
1.03k stars 23 forks source link

BUGFIX: Fixed A File Descriptor Leak Related Bug #9

Open fazledyn opened 2 months ago

fazledyn commented 2 months ago

Overview

This PR fixes a file descriptor leak related bug in heat.py file. The calling of mkstemp() returns a random temp file name and a file descriptor. It becomes the responsibility of the user to close that file descriptor. Having too much of open file descriptors leads to an OSError [Errno 24].

PoC

from tempfile import mkstemp
import os

def heat():
        _, tmp_file = mkstemp()
        with open(tmp_file, "w") as f:
                f.write("foo")
        # do something
        os.remove(tmp_file)

if __name__ == "__main__":
        for i in range(100_000):
                print(i+1)
                heat()

Running the above program gives the output:

...
1019
1020
1021
Traceback (most recent call last):
  File "/mnt/f/GitHub/test_pyheat.py", line 17, in <module>
  File "/mnt/f/GitHub/test_pyheat.py", line 7, in heat
OSError: [Errno 24] Too many open files: '/tmp/tmp3h57izmy'