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

[Dubbo-619] Fix consumer will generate wrong stackTrace #2956

Merged
merged 2 commits into from
Dec 18, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.dubbo.rpc;

import java.lang.reflect.Field;

/**
* RPC Result.
*
Expand All @@ -39,6 +41,23 @@ public RpcResult(Throwable exception) {
@Override
public Object recreate() throws Throwable {
if (exception != null) {
// fix issue#619
try {
// get Throwable class
Class clazz = exception.getClass();
while (!clazz.getName().equals(Throwable.class.getName())) {
clazz = clazz.getSuperclass();
}
// get stackTrace value
Field stackTraceField = clazz.getDeclaredField("stackTrace");
stackTraceField.setAccessible(true);
Object stackTrace = stackTraceField.get(exception);
if (stackTrace == null) {
exception.setStackTrace(new StackTraceElement[0]);
}
} catch (Exception e) {
// ignore
}
throw exception;
}
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.rpc;


import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.fail;

public class RpcResultTest {
@Test
public void testRecreateWithNormalException() {
NullPointerException npe = new NullPointerException();
RpcResult rpcResult = new RpcResult(npe);
try {
rpcResult.recreate();
fail();
} catch (Throwable throwable) {
StackTraceElement[] stackTrace = throwable.getStackTrace();
Assert.assertNotNull(stackTrace);
Assert.assertTrue(stackTrace.length > 1);
}
}

/**
* please run this test in Run mode
*/
@Test
public void testRecreateWithEmptyStackTraceException() {
// begin to construct a NullPointerException with empty stackTrace
Throwable throwable = null;
Long begin = System.currentTimeMillis();
while (System.currentTimeMillis() - begin < 60000) {
try {
((Object) null).getClass();
} catch (Exception e) {
if (e.getStackTrace().length == 0) {
throwable = e;
break;
}
}
}
/**
* may be there is -XX:-OmitStackTraceInFastThrow or run in Debug mode
*/
if (throwable == null) {
System.out.println("###testRecreateWithEmptyStackTraceException fail to construct NPE");
return;
}
// end construct a NullPointerException with empty stackTrace

RpcResult rpcResult = new RpcResult(throwable);
try {
rpcResult.recreate();
fail();
} catch (Throwable t) {
StackTraceElement[] stackTrace = t.getStackTrace();
Assert.assertNotNull(stackTrace);
Assert.assertTrue(stackTrace.length == 0);
}
}
}