Search code examples
javawindowsmultithreadingniocrash-dumps

Workaround for Java bug which causes crash dump


A program that I've developed is crashing the JVM occasionally due to this bug: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8029516. Unfortunately the bug has not been resolved by Oracle and the bug report says that there are no known workarounds.

I've tried to modify the example code from the bug report by calling .register(sWatchService, eventKinds) in the KeyWatcher thread instead, by adding all pending register request to a list that I loop through in the KeyWatcher thread but it's still crashing. I'm guessing this just had the same effect as synchronizing on sWatchService (like the submitter of the bug report tried).

Can you think of any way to get around this?


Solution

  • I've managed to create a workaround though it's somewhat ugly.

    The bug is in JDK method WindowsWatchKey.invalidate() that releases native buffer while the subsequent calls may still access it. This one-liner fixes the problem by delaying buffer clean-up until GC.

    Here is a compiled patch to JDK. In order to apply it add the following Java command-line flag:
    -Xbootclasspath/p:jdk-8029516-patch.jar

    If patching JDK is not an option in your case, there is still a workaround on the application level. It relies on the knowledge of Windows WatchService internal implementation.

    public class JDK_8029516 {
        private static final Field bufferField = getField("sun.nio.fs.WindowsWatchService$WindowsWatchKey", "buffer");
        private static final Field cleanerField = getField("sun.nio.fs.NativeBuffer", "cleaner");
        private static final Cleaner dummyCleaner = Cleaner.create(Thread.class, new Thread());
    
        private static Field getField(String className, String fieldName) {
            try {
                Field f = Class.forName(className).getDeclaredField(fieldName);
                f.setAccessible(true);
                return f;
            } catch (Exception e) {
                throw new IllegalStateException(e);
            }
        }
    
        public static void patch(WatchKey key) {
            try {
                cleanerField.set(bufferField.get(key), dummyCleaner);
            } catch (IllegalAccessException e) {
                throw new IllegalStateException(e);
            }
        }
    }
    

    Call JDK_8029516.patch(watchKey) right after the key is registred, and it will prevent watchKey.cancel() from releasing the native buffer prematurely.