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

8316580: HttpClient with StructuredTaskScope does not close when a task fails #15883

Closed

Conversation

dfuch
Copy link
Member

@dfuch dfuch commented Sep 22, 2023

Hi,

Please find below a fix for 8316580: HttpClient with StructuredTaskScope does not close when a task fails.

The problem here is a subtle reference release issue: Interrupting the VirtualThread means that the CompletableFuture returned by sendAsync is eligible for GC after having been cancelled, which in turn means that some of the intermediate operations that would have been completed before that CF was completed get eligible for being GC’ed too. One of these intermediate operations is the action that decrements the ref counting. Since the refcount isn’t decremented properly, the client won’t exit.

Holding onto the CompletableFuture returned to the caller by HttpClient::sendAsync until that CompletableFuture gets completed from upstream fixes the issue.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8316580: HttpClient with StructuredTaskScope does not close when a task fails (Bug - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/15883/head:pull/15883
$ git checkout pull/15883

Update a local copy of the PR:
$ git checkout pull/15883
$ git pull https://git.openjdk.org/jdk.git pull/15883/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 15883

View PR using the GUI difftool:
$ git pr show -t 15883

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/15883.diff

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 22, 2023

👋 Welcome back dfuchs! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot changed the title 8316580 8316580: HttpClient with StructuredTaskScope does not close when a task fails Sep 22, 2023
@openjdk openjdk bot added the rfr Pull request is ready for review label Sep 22, 2023
@openjdk
Copy link

openjdk bot commented Sep 22, 2023

@dfuch The following label will be automatically applied to this pull request:

  • net

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the net net-dev@openjdk.org label Sep 22, 2023
@mlbridge
Copy link

mlbridge bot commented Sep 22, 2023

Webrevs

@dfuch
Copy link
Member Author

dfuch commented Sep 25, 2023

@djelinski has found out that the issue had nothing to do with GC, but that cancelling the last CompletableFuture in the chain actually caused the action leading to it (which unfortunately contained the refcount) to be popped off the stack.
So the fix is to always insert a dummy action before returning the completable future to the caller.
I have revised my fix to do just that - and refactored the code a bit to avoid inserting this action if not needed (e.g. when the future is already completed).

Since the issue has nothing to do with StructuredTaskScope or VirtualThread I have reworked the test to use a plain executor and to emulate a scope where an exception occurring in any of the concurrent tasks causes all others to be cancelled. The test still fails without the fix and passes with it.

// make sure to fail with CancellationException if cancel(true)
// was called.
t = x.initCause(Utils.getCancelCause(t));
t = cancellationException(t);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for this change?

Copy link
Member Author

@dfuch dfuch Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive by fix - you can set initCause only once - so it's better to actually reconstruct the CancellationException when setting its cause. Especially since the original exception is shared.

.build();
}

static final String HOST = "localhost:62057";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have something listening on that port? Some of our tests used to fail after receiving an unexpected connenction, and 62057 is in the ephemeral range.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I guess it doesn't matter as long as it doesn't reply with a valid response, but if the port is a server port used by some other test running on the machine that only expect a given type/number/sequence of requests it might make the other test fail. Let me experiment with a ServerSocket that doesn't answer, then we might get either TimeoutException or Connection Refused when the backlog is full. That should work as well to test the fix.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@djelinski Now done.

Copy link

@mikko-apo mikko-apo Sep 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm the submitter of the bug report. I found the issue when the server was down and nothing was listening in the port.

The bug also happens if there is a server listening in the port, the server sends a response and the code running in the subtask throws an Exception. For this case it seems I need to use more concurrent threads to achieve the same effect. For no servers 70 was enough but for a running server I need 200.

The test program I wrote is available at: https://github.com/mikko-apo/jvm21-demo/blob/main/jvm21-demo/src/main/java/fi/iki/apo/HttpGetBurst.java and I use it against helidon/quickstart-standalone-mp-4m2 which is in the same repository.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mikko-apo - the JBS issue also has a reproducer and I have verified that it no longer fails with the proposed fix.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dfuch Great that you managed to get that fixed so quickly :)

Copy link
Member

@djelinski djelinski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@openjdk
Copy link

openjdk bot commented Sep 28, 2023

@dfuch This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8316580: HttpClient with StructuredTaskScope does not close when a task fails

Reviewed-by: djelinski

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 49 new commits pushed to the master branch:

  • 7981251: 8316970: Add internal annotation to mark restricted methods
  • 77fac0f: 8316906: Clarify TLABWasteTargetPercent flag
  • 2d154fc: 8316566: RISC-V: Zero extended narrow oop passed to Atomic::cmpxchg
  • 42924ed: 8316946: jtreg failure handler pages are mislabelling the jcmd/thread/dump_to_file results.
  • 384d2ea: 8316125: Windows call_stub unnecessarily saves xmm16-31 when UseAVX>=3
  • 84390dd: 8316658: serviceability/jvmti/RedefineClasses/RedefineLeakThrowable.java fails intermittently
  • 52073b2: 8316994: Avoid modifying ClassLoader and Module objects during -Xshare:dump
  • 83c0e45: 8306630: Bump minimum boot jdk to JDK 21
  • d3a79b5: 8316371: Open some swing tests 6
  • 750da00: 8316743: RISC-V: Change UseVectorizedMismatchIntrinsic option result to warning
  • ... and 39 more: https://git.openjdk.org/jdk/compare/0f0c5b2d71e6dec442a5105ba305043cb59b99fc...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Sep 28, 2023
@dfuch
Copy link
Member Author

dfuch commented Sep 28, 2023

/integrate

@openjdk
Copy link

openjdk bot commented Sep 28, 2023

Going to push as commit fc98998.
Since your change was applied there have been 55 commits pushed to the master branch:

  • 065203d: 8313631: SA: stack trace printed using "where" command does not show class name
  • 0c55887: 8309599: WeakHandle and OopHandle release should clear obj pointer
  • 1230aed: 8316885: jcmd: Compiler.CodeHeap_Analytics cmd does not inform about missing aggregate
  • d0c1444: 8316957: Serial: Change GenCollectedHeap to SerialHeap inside gc/serial folder
  • bc5a39b: 8317144: Exclude sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java on Linux ppc64le
  • 3481ecb: 8316159: Update BoxLayout sample image for crisper edges
  • 7981251: 8316970: Add internal annotation to mark restricted methods
  • 77fac0f: 8316906: Clarify TLABWasteTargetPercent flag
  • 2d154fc: 8316566: RISC-V: Zero extended narrow oop passed to Atomic::cmpxchg
  • 42924ed: 8316946: jtreg failure handler pages are mislabelling the jcmd/thread/dump_to_file results.
  • ... and 45 more: https://git.openjdk.org/jdk/compare/0f0c5b2d71e6dec442a5105ba305043cb59b99fc...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Sep 28, 2023
@openjdk openjdk bot closed this Sep 28, 2023
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Sep 28, 2023
@openjdk
Copy link

openjdk bot commented Sep 28, 2023

@dfuch Pushed as commit fc98998.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integrated Pull request has been integrated net net-dev@openjdk.org
4 participants