Can I close file descriptors open with code that I don’t own?

I am using a third-party commercial library that seems to be skipping files (I tested this on Linux using lsof). In the end, the server (Tomcat) begins to receive the infamous "Too many open file errors" and I need to restart the JVM.

I have already contacted the seller. In the meantime, however, I would like to find a workaround for this. I do not have access to their source code. Is there a way in Java to clear file descriptors without having access to the source object File(or FileWriter, FileOutputStreametc.)?

+5
source share
2 answers

an interesting way would be to write a dynamic library and use LD_PRELOAD to load this java instance that you run ... this DLL can override the corresponding underlying open (2) system call (or use some other logic) to close existing process file descriptors before passing the call libc (or kernel) implementations. You need to do some serious accounting and maybe deal with threads; But it can be done. Especially if you take the hints from / proc / pid / fd / to determine if the closure is suitable for the target fd.

+2
source
0

All Articles