From 07456fa71fe5938edb69cf05548fe815d994085a Mon Sep 17 00:00:00 2001 From: cyjseagull Date: Tue, 24 Sep 2024 01:21:58 +0800 Subject: [PATCH] fix the java-callback throw exception caused coredump --- .../ppc-front/ppc-front/CallbackManager.cpp | 13 +++++++++++-- .../wedpr/sdk/jni/demo/TransportDemo.java | 6 +++++- .../transport/handlers/GetPeersCallback.java | 17 ++++++++++++++++- .../jni/transport/handlers/MessageCallback.java | 10 +++++++++- .../handlers/MessageDispatcherCallback.java | 10 +++++++++- .../handlers/MessageErrorCallback.java | 13 +++++++++++++ 6 files changed, 63 insertions(+), 6 deletions(-) diff --git a/cpp/wedpr-transport/ppc-front/ppc-front/CallbackManager.cpp b/cpp/wedpr-transport/ppc-front/ppc-front/CallbackManager.cpp index efddec1c..1da25b26 100644 --- a/cpp/wedpr-transport/ppc-front/ppc-front/CallbackManager.cpp +++ b/cpp/wedpr-transport/ppc-front/ppc-front/CallbackManager.cpp @@ -116,8 +116,17 @@ void CallbackManager::handleCallback(bcos::Error::Ptr const& error, std::string { return; } - m_threadPool->enqueue( - [error, callback, message, resFunc] { callback->msgCallback(error, message, resFunc); }); + m_threadPool->enqueue([error, callback, message, resFunc] { + try + { + callback->msgCallback(error, message, resFunc); + } + catch (std::exception const& e) + { + FRONT_LOG(WARNING) << LOG_DESC("handleCallback exception") + << LOG_KV("error", boost::diagnostic_information(e)); + } + }); } diff --git a/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/demo/TransportDemo.java b/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/demo/TransportDemo.java index 7c26c4e0..f9ef4550 100644 --- a/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/demo/TransportDemo.java +++ b/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/demo/TransportDemo.java @@ -15,6 +15,7 @@ package com.webank.wedpr.sdk.jni.demo; +import com.webank.wedpr.sdk.jni.common.WeDPRSDKException; import com.webank.wedpr.sdk.jni.generated.*; import com.webank.wedpr.sdk.jni.generated.Error; import com.webank.wedpr.sdk.jni.transport.IMessage; @@ -24,6 +25,7 @@ import com.webank.wedpr.sdk.jni.transport.handlers.MessageErrorCallback; import com.webank.wedpr.sdk.jni.transport.impl.TransportImpl; import com.webank.wedpr.sdk.jni.transport.model.TransportEndPoint; +import lombok.SneakyThrows; public class TransportDemo { public static class MessageDispatcherCallbackImpl extends MessageDispatcherCallback { @@ -37,6 +39,7 @@ public MessageDispatcherCallbackImpl(String nodeID) { this.nodeID = nodeID; } + @SneakyThrows(Exception.class) @Override public void onMessage(IMessage message) { System.out.println( @@ -47,6 +50,7 @@ public void onMessage(IMessage message) { + ", payload: " + new String(message.getPayload()) + "#######"); + throw new WeDPRSDKException("Exception test"); } } @@ -58,7 +62,7 @@ public MessageErrorCallbackImpl(String nodeID) { } @Override - public void onError(Error error) { + public void onErrorResult(Error error) { System.out.println( "##### Node: " + nodeID diff --git a/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/transport/handlers/GetPeersCallback.java b/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/transport/handlers/GetPeersCallback.java index 17e783d5..3f02e7f1 100644 --- a/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/transport/handlers/GetPeersCallback.java +++ b/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/transport/handlers/GetPeersCallback.java @@ -17,10 +17,25 @@ import com.webank.wedpr.sdk.jni.generated.Error; import com.webank.wedpr.sdk.jni.generated.GetPeersInfoHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public abstract class GetPeersCallback extends GetPeersInfoHandler { + private static final Logger logger = LoggerFactory.getLogger(GetPeersCallback.class); - public abstract void onPeersInfo(Error e, String peersInfo); + public abstract void onPeers(Error e, String peersInfo); + + public void onPeersInfo(Error e, String peersInfo) { + try { + onPeers(e, peersInfo); + } catch (Exception exception) { + logger.warn( + "onPeersInfo exception, result: {}, peersInfo: {}, e: ", + e.toString(), + peersInfo, + exception); + } + } // release the ownership to c++, in case of it's released by the jvm @Override diff --git a/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/transport/handlers/MessageCallback.java b/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/transport/handlers/MessageCallback.java index 9b2b67d0..8e12f8e5 100644 --- a/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/transport/handlers/MessageCallback.java +++ b/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/transport/handlers/MessageCallback.java @@ -21,8 +21,12 @@ import com.webank.wedpr.sdk.jni.generated.SendResponseHandler; import com.webank.wedpr.sdk.jni.transport.IMessage; import com.webank.wedpr.sdk.jni.transport.IMessageBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public abstract class MessageCallback extends IMessageHandler { + private static final Logger logger = LoggerFactory.getLogger(MessageCallback.class); + public abstract void onMessage( Error error, IMessage message, SendResponseHandler sendResponseHandler); @@ -34,6 +38,10 @@ protected void finalize() { @Override public void onMessage(Error e, Message msg, SendResponseHandler sendResponseHandler) { - onMessage(e, IMessageBuilder.build(msg), sendResponseHandler); + try { + onMessage(e, IMessageBuilder.build(msg), sendResponseHandler); + } catch (Exception exception) { + logger.warn("onMessage exception, msg: {}, e:", msg.toString(), exception); + } } } diff --git a/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/transport/handlers/MessageDispatcherCallback.java b/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/transport/handlers/MessageDispatcherCallback.java index eda2ef65..7eb0eec5 100644 --- a/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/transport/handlers/MessageDispatcherCallback.java +++ b/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/transport/handlers/MessageDispatcherCallback.java @@ -19,8 +19,12 @@ import com.webank.wedpr.sdk.jni.generated.MessageDispatcherHandler; import com.webank.wedpr.sdk.jni.transport.IMessage; import com.webank.wedpr.sdk.jni.transport.IMessageBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public abstract class MessageDispatcherCallback extends MessageDispatcherHandler { + private static final Logger logger = LoggerFactory.getLogger(MessageDispatcherCallback.class); + public abstract void onMessage(IMessage message); // TODO: check this will cause memory leak or not @@ -32,6 +36,10 @@ protected void finalize() { @Override public void onMessage(Message msg) { - onMessage(IMessageBuilder.build(msg)); + try { + onMessage(IMessageBuilder.build(msg)); + } catch (Exception e) { + logger.warn("onMessage exception, msg: {}, e:", msg.toString(), e); + } } } diff --git a/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/transport/handlers/MessageErrorCallback.java b/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/transport/handlers/MessageErrorCallback.java index 0d85b06a..0eb22a13 100644 --- a/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/transport/handlers/MessageErrorCallback.java +++ b/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/transport/handlers/MessageErrorCallback.java @@ -15,10 +15,23 @@ package com.webank.wedpr.sdk.jni.transport.handlers; +import com.webank.wedpr.sdk.jni.generated.Error; import com.webank.wedpr.sdk.jni.generated.ErrorCallback; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public abstract class MessageErrorCallback extends ErrorCallback { + private static final Logger logger = LoggerFactory.getLogger(MessageErrorCallback.class); + public abstract void onErrorResult(Error error); + + public void onError(Error error) { + try { + onErrorResult(error); + } catch (Exception e) { + logger.warn("onError exception, error: {}, e:", error.toString(), e); + } + } // release the ownership to c++, in case of it's released by the jvm @Override protected void finalize() {