Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tempfile and deleteOnExit #1905

Closed
cprice404 opened this Issue Aug 15, 2014 · 3 comments

Comments

Projects
None yet
3 participants
@cprice404
Copy link
Contributor

commented Aug 15, 2014

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

@cprice404

This comment has been minimized.

Copy link
Contributor Author

commented Aug 15, 2014

FWIW Here's a JDK bug report where folks were asking if this is expected behavior for the JVM itself:

http://bugs.java.com/view_bug.do?bug_id=4513817

@headius

This comment has been minimized.

Copy link
Member

commented Aug 15, 2014

Sigh...another bad decision by someone implementing JDK classes. I see no reason we couldn't just make Tempfile implement Finalizable and register it with the Ruby instance to be cleaned up on teardown. Thoughts, @enebo?

@headius

This comment has been minimized.

Copy link
Member

commented Aug 15, 2014

One workaround for you until we fix this:

class java::io::DeleteOnExitHook
  field_accessor :files
end

java::io::DeleteOnExitHook.files.clear

headius added a commit that referenced this issue Aug 18, 2014

@headius headius added the stdlib label Aug 18, 2014

@headius headius modified the milestones: JRuby 9000, JRuby 1.7.14 Aug 18, 2014

@headius headius self-assigned this Aug 18, 2014

@headius headius closed this Aug 18, 2014

@enebo enebo modified the milestones: JRuby 1.7.14, JRuby 1.7.15 Aug 27, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.