From 0d576a6f68a4afae50c4dc25ea9ae7adc7265447 Mon Sep 17 00:00:00 2001 From: jiangyuan <469391363@qq.com> Date: Tue, 16 Apr 2024 20:13:02 +0800 Subject: [PATCH] Fix: sendResponse if RpcRequestProcessor.doProcess throw Throwable (#334) * fix: sendResponse if RpcRequestProcessor.doProcess throw Throwable in ProcessTask * update unit test * update unit test --- .../rpc/protocol/RpcRequestProcessor.java | 10 ++++----- .../remoting/rpc/exception/ExceptionTest.java | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/alipay/remoting/rpc/protocol/RpcRequestProcessor.java b/src/main/java/com/alipay/remoting/rpc/protocol/RpcRequestProcessor.java index 9b92adc1..151210cf 100644 --- a/src/main/java/com/alipay/remoting/rpc/protocol/RpcRequestProcessor.java +++ b/src/main/java/com/alipay/remoting/rpc/protocol/RpcRequestProcessor.java @@ -395,11 +395,11 @@ public void run() { //protect the thread running this task String remotingAddress = RemotingUtil.parseRemoteAddress(ctx.getChannelContext() .channel()); - logger - .error( - "Exception caught when process rpc request command in RpcRequestProcessor, Id=" - + msg.getId() + "! Invoke source address is [" + remotingAddress - + "].", e); + String errMsg = "Exception caught when process rpc request command in RpcRequestProcessor, Id=" + + msg.getId(); + logger.error(errMsg + "! Invoke source address is [" + remotingAddress + "].", e); + sendResponseIfNecessary(ctx, msg.getType(), getCommandFactory() + .createExceptionResponse(msg.getId(), e, errMsg)); } } diff --git a/src/test/java/com/alipay/remoting/rpc/exception/ExceptionTest.java b/src/test/java/com/alipay/remoting/rpc/exception/ExceptionTest.java index c95cf7ca..0d79ca85 100644 --- a/src/test/java/com/alipay/remoting/rpc/exception/ExceptionTest.java +++ b/src/test/java/com/alipay/remoting/rpc/exception/ExceptionTest.java @@ -341,4 +341,26 @@ public Executor getExecutor() { latch.await(); Assert.assertEquals(InvokeServerException.class, ret.get(0).getClass()); } + + @Test + public void testGetBizClassLoaderException1() { + server.registerUserProcessor(new SimpleServerUserProcessor() { + @Override + public ClassLoader getBizClassLoader() { + throw new RuntimeException("getBizClassLoader fail."); + } + }); + + RequestBody req = new RequestBody(4, "hello world"); + Object result = null; + try { + result = client.invokeSync(addr, req, 3000); + String errMsg = "Should throw InvokeServerException!"; + logger.error(errMsg); + Assert.fail(errMsg); + } catch (Exception e) { + Assert.assertNull(result); + Assert.assertEquals(InvokeServerException.class, e.getClass()); + } + } }