调用java导致网页崩溃,导致崩溃转储的Java错误的解决方法

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?

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值