I am running code which after sometimes hours, sometimes minutes fails with the error
OSError: [Errno 24] Too many open files
And I have real trouble debugging this. The error itself is always triggered by the marked line in the code snippet below
try:
with open(filename, 'rb') as f:
contents = f.read() <----- error triggered here
except OSError as e:
print("e = ", e)
raise
else:
# other stuff happens
However, I can't see any problem in this part of the code (right?) so I guess that other parts of the code don't close files properly. However, while I do open files quite a bit, I always open them with the 'with' statement, and my understanding is that even if an error occurs the files will be closed (right?). So another part of my code looks like this
try:
with tarfile.open(filename + '.tar') as tar:
tar.extractall(path=target_folder)
except tarfile.ReadError as e:
print("e = ", e)
except OSError as e:
print("e = ", e)
else:
# If everything worked, we are done
return
The code above does run into a ReadError quite frequently, but even if that happens, the file should be closed, right? So I just don't understand how I can run into too many open files? Sorry this is not reproducible for you, since I can't debug it enough, I just fishing for some tips here, since I am lost. Any help is appreciated...
Edit: I am on a macbook. Here is the output of ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) unlimited
open files (-n) 256
pipe size (512 bytes, -p) 1
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 1418
virtual memory (kbytes, -v) unlimited
Following the suggestion by @sj95126 I changed the code concerning the tar file to something which ensures that the file is closed
try:
tar = tarfile.open(filename + '.tar')
tar.extractall(path=target_folder)
except tarfile.ReadError as e:
print("tarfile.ReadError e = ", e)
except OSError as e:
print("e = ", e)
else:
# If everything worked, we are done
return
finally:
print("close tar file")
try:
tar.close()
except:
print("file already closed")
but it did not solve the problem.
file locks
usingulimit -a
in cmd. – Annemariewith
should result in the file being closed; however, there's always the possibility of a bug in the managing object. Atarfile
object has to close the file, thewith
isn't going to reach through to thefile
object and do it. Just for testing, try adding afinally
clause to thetry
and explicitly calltar.close()
, and see if that helps. – Martinetls -l /proc/PID/fd
, with the PID of the Python process, will let you see what the open files are; it'l be much easier to identify the code that's supposed to be responsible for closing them after you've determined that. – Theosophy