Skip to content

Commit 4a873f1

Browse files
author
Brandon Black
committed
RUBY-511 adding finalizer to jruby runtime cache to prevent runtime leaks
1 parent 2eff41f commit 4a873f1

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

ext/jbson/src/main/jbson/RubyBSONCallback.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,16 +320,20 @@ protected boolean isStackEmpty() {
320320
return _stack.size() < 1;
321321
}
322322

323-
private static HashMap<String, Object> _getRuntimeCache(Ruby runtime) {
324-
// each JRuby runtime may have different objects for these constants,
325-
// so cache them separately for each runtime
323+
private static HashMap<String, Object> _getRuntimeCache(final Ruby runtime) {
326324
@SuppressWarnings("unchecked")
327325
HashMap<String, Object> cache = _runtimeCache.get( runtime );
328326

329327
if(cache == null) {
330328
cache = new HashMap<String, Object>();
331329
_runtimeCache.put( runtime, cache );
330+
runtime.addFinalizer( new Finalizable() {
331+
public void finalize() {
332+
_runtimeCache.remove(runtime);
333+
}
334+
});
332335
}
336+
333337
return cache;
334338
}
335339

ext/jbson/src/main/jbson/RubyBSONEncoder.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -763,15 +763,19 @@ public void writeCString( String s ){
763763
private ByteBuffer _stringB = ByteBuffer.wrap( new byte[1024 + 1] );
764764
private CharsetEncoder _encoder = Charset.forName( "UTF-8" ).newEncoder();
765765

766-
private static Map _getRuntimeCache(Ruby runtime) {
767-
// each JRuby runtime may have different objects for these constants,
768-
// so cache them separately for each runtime
769-
Map cache = (Map) _runtimeCache.get( runtime );
766+
private static Map _getRuntimeCache(final Ruby runtime) {
767+
Map cache = (Map) _runtimeCache.get(runtime);
770768

771769
if(cache == null) {
772770
cache = new HashMap();
773-
_runtimeCache.put( runtime, cache );
771+
_runtimeCache.put(runtime, cache);
772+
runtime.addFinalizer( new Finalizable() {
773+
public void finalize() {
774+
_runtimeCache.remove( runtime );
775+
}
776+
});
774777
}
778+
775779
return cache;
776780
}
777781

ext/jbson/target/jbson.jar

1.35 KB
Binary file not shown.

0 commit comments

Comments
 (0)