Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I close file handles opened by code I don't own?

Tags:

java

io

I'm using a third-party commercial library which seems to be leaking file handles (I verified this on Linux using lsof). Eventually the server (Tomcat) starts getting the infamous "Too many open files error", and I have to re-start the JVM.

I've already contacted the vendor. In the meantime, however, I would like to find a workaround for this. I do not have access to their source code. Is there any way, in Java, to clean up file handles without having access to the original File object (or FileWriter, FileOutputStream, etc.)?

like image 568
Matt Solnit Avatar asked Nov 05 '22 11:11

Matt Solnit


1 Answers

a fun way would be to write a dynamic library and use LD_PRELOAD to load it for the java instance you are launching ... this DLL could override the appropriate underlying open(2) system call (or use some other logic) to close existing file descriptors of the process before passing the call to the libc implementation (or the kernel). You need to do some serious accounting and possibly deal with threads; but it can be done. Especially if you take hints from /proc/pid/fd/ for figuring whether or not a close is appropriate for the target fd.

like image 131
Ahmed Masud Avatar answered Nov 09 '22 06:11

Ahmed Masud