Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanup the temp files /tmp/jruby-*/jruby*jar
Browse files Browse the repository at this point in the history
when the jruby process gets killed hard it can leave a few jars in the
system temp directory. this helper method is used to identify stale jars and
delete them.

fixes #3381

Sponsored by Lookout Inc.
mkristian committed Oct 12, 2015
1 parent b7e6461 commit 3c9e581
Showing 2 changed files with 43 additions and 0 deletions.
24 changes: 24 additions & 0 deletions core/src/main/ruby/jruby/jruby.rb
Original file line number Diff line number Diff line change
@@ -44,6 +44,30 @@ def with_current_runtime_as_global
end
end

def cleanup_stale_tempfiles
Dir[File.join(ENV['TMPDIR'], 'jruby-*')].each do |dir|
pid = File.basename(dir)[6..-1]
unless process_exists?(pid)
# do not use fileutils here as we are in jruby-core
Dir[File.join(dir, '*')].each do |file|
File.delete(file) rescue warn "could not delete #{file}"
end
Dir.delete(dir) rescue warn "could not delete #{dir}"
end
end
end

# stolen from process_exists gem https://github.com/wilsonsilva/process_exists/blob/master/lib/process_exists/core_ext/process.rb
def process_exists?(pid)
Process.kill(0, pid.to_i)
true
rescue Errno::ESRCH # No such process
false
rescue Errno::EPERM # The process exists, but you dont have permission to send the signal to it.
true
end
private :process_exists?

# Parse the given block or the provided content, returning a JRuby AST node.
def parse(content = nil, filename = (default_filename = true; '-'), extra_position_info = false, &block)
if block
19 changes: 19 additions & 0 deletions test/test_tempfile_cleanup.rb
Original file line number Diff line number Diff line change
@@ -15,6 +15,25 @@ def teardown
FileUtils.rm_f @tmpdir
end

def test_cleanup_jars_from_jruby_class_loader
# run only in embedded case
skip unless ENV['RUBY']
cmd = ENV['RUBY'].sub(/^java/, 'java -Djruby.home=uri:classloader://META-INF/jruby.home') + ' -ropenssl -e "sleep 123"'
pid = Process.spawn cmd
# give jruby sometime to start
sleep 10

tmpfiles = File.join( ENV['TMPDIR'], "jruby-#{pid}", 'jruby*.jar' )

assert Dir[tmpfiles].size > 0

Process.kill(9, pid)

JRuby.cleanup_stale_tempfiles

assert Dir[tmpfiles].size == 0
end

def test_cleanup
10.times { Tempfile.open('blah', @tmpdir) }

0 comments on commit 3c9e581

Please sign in to comment.