Skip to content

Tempfile and deleteOnExit #1905

@cprice404

Description

@cprice404

We've started using JRuby to run some of our existing Ruby code. This is for long-running server-side processes.

It happens that our existing Ruby code uses the Ruby Tempfile class fairly regularly. We had an issue come up where, if we leave the process running for long enough, it will eventually crash with a Java OOM exception.

Inspecting the heap dump, we discovered that there was a huge array of temp file entries registered with the JVM File.deleteOnExit hook. This led us to investigate the JRuby source code, and we noticed that the implementation of Tempfile calls the deleteOnExit hook (unconditionally) whenever a Tempfile object is constructed.

I believe that this effectively renders the Tempfile class unsafe to use for long-running JRuby processes? Obviously this can be worked around by getting rid of all of our uses of Tempfile and using regular File objects, but I thought it was at least worth raising the question of whether this was a known issue and whether it should be considered a JRuby "best practice" to avoid using Tempfile in long-lived processes.

I'm not sure what alternatives there might be; maybe a configuration option that toggles whether deleteOnExit is called, but that's obviously a little kludgy. Or maybe the code could be changed to not call deleteOnExit at all, since you are already calling tmpFile.delete() in the finalizer?:

https://github.com/jruby/jruby/blob/1.7.13/core/src/main/java/org/jruby/ext/tempfile/Tempfile.java#L278

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions