Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix the java-callback throw exception caused coredump #42

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions cpp/wedpr-transport/ppc-front/ppc-front/CallbackManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
});
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -37,6 +39,7 @@ public MessageDispatcherCallbackImpl(String nodeID) {
this.nodeID = nodeID;
}

@SneakyThrows(Exception.class)
@Override
public void onMessage(IMessage message) {
System.out.println(
Expand All @@ -47,6 +50,7 @@ public void onMessage(IMessage message) {
+ ", payload: "
+ new String(message.getPayload())
+ "#######");
throw new WeDPRSDKException("Exception test");
}
}

Expand All @@ -58,7 +62,7 @@ public MessageErrorCallbackImpl(String nodeID) {
}

@Override
public void onError(Error error) {
public void onErrorResult(Error error) {
System.out.println(
"##### Node: "
+ nodeID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading