[ https://issues.apache.org/jira/browse/WW-5414?focusedWorklogId=918910&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-918910 ]
ASF GitHub Bot logged work on WW-5414: -------------------------------------- Author: ASF GitHub Bot Created on: 11/May/24 08:05 Start Date: 11/May/24 08:05 Worklog Time Spent: 10m Work Description: lukaszlenart commented on code in PR #932: URL: https://github.com/apache/struts/pull/932#discussion_r1597394691 ########## core/src/main/java/org/apache/struts2/interceptor/exec/StrutsBackgroundProcess.java: ########## @@ -61,11 +66,17 @@ public BackgroundProcess prepare() { try { beforeInvocation(); result = invocation.invokeActionOnly(); - afterInvocation(); } catch (Exception e) { + LOG.warn("Exception during invokeActionOnly() execution", e); exception = e; } finally { - done = true; + try { + afterInvocation(); + } catch (Exception ex) { + exception = ex; Review Comment: Yeah, I know, I thought about adding additional `afterException` and maybe `beforeException` fields to keep this information Issue Time Tracking ------------------- Worklog Id: (was: 918910) Time Spent: 1h (was: 50m) > AfterInvocation of BackgroundProcess is not called when an exception occurs > when using ExecuteAndWaitInterceptor > ---------------------------------------------------------------------------------------------------------------- > > Key: WW-5414 > URL: https://issues.apache.org/jira/browse/WW-5414 > Project: Struts 2 > Issue Type: Bug > Components: Core Interceptors > Affects Versions: 2.5.30, 6.3.0 > Reporter: Yukio Suzuki > Assignee: Lukasz Lenart > Priority: Major > Fix For: 6.5.0 > > Time Spent: 1h > Remaining Estimate: 0h > > In my project, we are using Struts2.5.x and recently started using the > ExecuteAndWaitInterceptor. We have extended BackgroundProcess and overridden > the beforeInvocation and afterInvocation methods to perform certain actions > before and after the invocation of an action. However, we are facing a > problem where afterInvocation is not called when an exception occurs. Here is > the relevant code: > > {code:java} > final Thread t = new Thread(new Runnable() { > public void run() { > try { > beforeInvocation(); > result = invocation.invokeActionOnly(); > afterInvocation(); > } catch (Exception e) { > exception = e; > } > > done = true; > } > }); > {code} > In the existing code, the beforeInvocation and afterInvocation methods set > and clear the context, but it seems unintentional that the context is not > cleared when an exception occurs. > {code:java} > protected void beforeInvocation() throws Exception { > ActionContext.setContext(invocation.getInvocationContext()); > } > protected void afterInvocation() throws Exception { > ActionContext.setContext(null); > }{code} > One possible improvement is to modify the code as follows, ensuring that > afterInvocation is called even when an exception occurs: > {code:java} > beforeInvocation(); > try { > result = invocation.invokeActionOnly(); > } finally { > afterInvocation(); > }{code} > Alternatively, if compatibility is a concern, you can add an > afterInvocation(Throwable t) method and modify the code as follows: > {code:java} > beforeInvocation(); > try { > result = invocation.invokeActionOnly(); > } catch (Throwable t) { > afterInvocation(t); > throw t; > } > afterInvocation();{code} > Please consider these modifications to ensure that afterInvocation is called > even when an exception occurs. -- This message was sent by Atlassian Jira (v8.20.10#820010)