-
Notifications
You must be signed in to change notification settings - Fork 3k
Discard remaining REST handlers when the client connection is closed prematurely #50990
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -212,6 +212,9 @@ public void restart(RuntimeResource target, boolean setLocatorTarget) { | |
| public void setupInitialMatchAndRestart(RequestMapper.RequestMatch<RestInitialHandler.InitialMatch> initialMatch) { | ||
| this.initialMatch = initialMatch; | ||
|
|
||
| // add a default close handler that simply discards whatever REST handlers still remain to be run | ||
| serverResponse().addCloseHandler(new DiscardRemainingRunner(this)); | ||
|
|
||
| restart(initialMatch.value.handlers); | ||
| setMaxPathParams(initialMatch.value.maxPathParams); | ||
| setRemaining(initialMatch.remaining); | ||
|
|
@@ -435,6 +438,14 @@ public void close() { | |
| super.close(); | ||
| } | ||
|
|
||
| /** | ||
| * This method ensures that no more handlers will run and that all the resources tied to the request are closed | ||
| */ | ||
| private void discardRemaining() { | ||
| setPosition(getHandlers().length); | ||
| close(); | ||
| } | ||
|
|
||
| public LazyResponse getResponse() { | ||
| return response; | ||
| } | ||
|
|
@@ -1330,4 +1341,24 @@ public PreviousResource(RuntimeResource locatorTarget, Object locatorPathParamVa | |
| private final PreviousResource prev; | ||
|
|
||
| } | ||
|
|
||
| private static class DiscardRemainingRunner implements Runnable { | ||
|
|
||
| private ResteasyReactiveRequestContext context; | ||
|
|
||
| private DiscardRemainingRunner(ResteasyReactiveRequestContext context) { | ||
| this.context = context; | ||
| } | ||
|
|
||
| @Override | ||
| public void run() { | ||
| try { | ||
| context.discardRemaining(); | ||
| } catch (Exception ignored) { | ||
|
|
||
|
Comment on lines
+1355
to
+1358
|
||
| } finally { | ||
| context = null; | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a potential race condition: the close handler could be invoked from another thread while the request is still being processed. If
discardRemaining()is called while handlers are executing,setPosition(getHandlers().length)could interfere with the handler chain execution. Consider adding synchronization or checking if the request context is already closed before modifying the position.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh, I was going to say exactly the same thing. I'm impressed.
I'm not sure what thread invokes the close handler (I guess/hope the same request's context/thread) and what other thread might be running the request (especially in the case of blocking requests).
So this might indeed have more than one thread interact with the handler position in parallel. Needs to be checked.