Skip to content

Commit c99ac16

Browse files
authored
Merge pull request #50990 from geoand/quarkus-rest-close-handlers
Discard remaining REST handlers when the client connection is closed prematurely
2 parents 9853819 + 61d1b10 commit c99ac16

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/ResteasyReactiveRequestContext.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ public void restart(RuntimeResource target, boolean setLocatorTarget) {
212212
public void setupInitialMatchAndRestart(RequestMapper.RequestMatch<RestInitialHandler.InitialMatch> initialMatch) {
213213
this.initialMatch = initialMatch;
214214

215+
// add a default close handler that simply discards whatever REST handlers still remain to be run
216+
serverResponse().addCloseHandler(new DiscardRemainingRunner(this));
217+
215218
restart(initialMatch.value.handlers);
216219
setMaxPathParams(initialMatch.value.maxPathParams);
217220
setRemaining(initialMatch.remaining);
@@ -435,6 +438,14 @@ public void close() {
435438
super.close();
436439
}
437440

441+
/**
442+
* This method ensures that no more handlers will run and that all the resources tied to the request are closed
443+
*/
444+
private void discardRemaining() {
445+
setPosition(getHandlers().length);
446+
close();
447+
}
448+
438449
public LazyResponse getResponse() {
439450
return response;
440451
}
@@ -1330,4 +1341,24 @@ public PreviousResource(RuntimeResource locatorTarget, Object locatorPathParamVa
13301341
private final PreviousResource prev;
13311342

13321343
}
1344+
1345+
private static class DiscardRemainingRunner implements Runnable {
1346+
1347+
private ResteasyReactiveRequestContext context;
1348+
1349+
private DiscardRemainingRunner(ResteasyReactiveRequestContext context) {
1350+
this.context = context;
1351+
}
1352+
1353+
@Override
1354+
public void run() {
1355+
try {
1356+
context.discardRemaining();
1357+
} catch (Exception ignored) {
1358+
1359+
} finally {
1360+
context = null;
1361+
}
1362+
}
1363+
}
13331364
}

0 commit comments

Comments
 (0)