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

8312436: CompletableFuture never completes when 'Throwable.toString()' method throws Exception #18988

Closed
wants to merge 1 commit into from

Conversation

viktorklang-ora
Copy link
Contributor

@viktorklang-ora viktorklang-ora commented Apr 28, 2024

Primarily offering this PR for discussion, as Throwables throwing exceptions on toString(), getLocalizedMessage(), or getMessage() seems like a rather unreasonable thing to do.

Nevertheless, there is some things we can do, as witnessed in this PR.


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-8312436: CompletableFuture never completes when 'Throwable.toString()' method throws Exception (Bug - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 18988

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

Using diff file

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

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Apr 28, 2024

👋 Welcome back vklang! 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
Copy link

openjdk bot commented Apr 28, 2024

@viktorklang-ora 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:

8312436: CompletableFuture never completes when 'Throwable.toString()' method throws Exception

Reviewed-by: alanb

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 527 new commits pushed to the master branch:

  • 9a8096f: 8330047: ASAN build error with gcc 13
  • 6882b38: 8333590: UnmodifiableHeaders.toString() returns a value that represents empty headers
  • cbb6747: 8329581: Java launcher no longer prints a stack trace
  • 789f704: 8322732: ForkJoinPool may underutilize cores in async mode
  • 2c1b311: 8331854: ubsan: copy.hpp:218:10: runtime error: addition of unsigned offset to 0x7fc2b4024518 overflowed to 0x7fc2b4024510
  • 765ad0e: 8331947: Preview creates checkbox for JEP-less preview feature
  • 8e903ee: 8331896: JFR: Improve check for JDK classes
  • 3cbdf8d: 8333554: Parallel: Remove unused PSParallelCompact::is_in
  • 3944e67: 8312132: Add tracking of multiple address spaces in NMT
  • d0052c0: 8333326: Linux Alpine build fails after 8302744
  • ... and 517 more: https://git.openjdk.org/jdk/compare/ea06129851be7bd9876685f74e35392874154179...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
Copy link

openjdk bot commented Apr 28, 2024

@viktorklang-ora The following label will be automatically applied to this pull request:

  • core-libs

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.

Comment on lines +2652 to +2653
* String {@code "Completed normally"} or the String {@code
* "Completed exceptionally"}, or the String {@code "Not
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These two looks like a discrepancy between spec and implementation, and it seemed safer to amend the spec rather than the implementation, as code might be relying on actual behavior rather than the spec:ed one.

@DougLea You might have some thoughts to offer on this :)

@openjdk openjdk bot added the core-libs core-libs-dev@openjdk.org label Apr 28, 2024
@@ -2623,7 +2667,7 @@ public String toString() {
? "[Not completed]"
: "[Not completed, " + count + " dependents]")
: (((r instanceof AltResult) && ((AltResult)r).ex != null)
? "[Completed exceptionally: " + ((AltResult)r).ex + "]"
? "[Completed exceptionally: " + ((AltResult)r).ex.getClass().getName() + "]"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@DougLea @AlanBateman It seemed safer to switch to outputting the cause FQCN rather than wrapping in a try-catch and trying to come up with a fallback text.

The reason for amending this is for things like JShell, results are toString():ed which would mean that the current session would break if toString():ing the result throws (also further signalling that throwing on toString() seems unreasonable in general).

// This makes sure that CompletableFuture still behaves appropriately
// even if thrown exceptions end up throwing exceptions from their String
// representations.
@Override public String getMessage() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I decided to do this to make sure that all pre-existing test cases would still work even if the exception used has a misbehaving toString() (toString() transitively calls getLocalizedMessage() which in turn calls getMessage())

Comment on lines +309 to +352
static CompletionException wrapInCompletionException(Throwable t) {
if (t == null)
return new CompletionException();

String message;
Throwable suppressed;
try {
message = t.toString();
suppressed = null;
} catch (Throwable unknown) {
message = "";
suppressed = unknown;
}

final CompletionException wrapping = new CompletionException(message, t);

if (suppressed != null)
wrapping.addSuppressed(suppressed);

return wrapping;
}

static ExecutionException wrapInExecutionException(Throwable t) {
if (t == null)
return new ExecutionException();

String message;
Throwable suppressed;
try {
message = t.toString();
suppressed = null;
} catch (Throwable unknown) {
message = "";
suppressed = unknown;
}

final ExecutionException wrapping = new ExecutionException(message, t);

if (suppressed != null)
wrapping.addSuppressed(suppressed);

return wrapping;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the main thing—and the big question is if this approach is the best path or not.

@@ -306,13 +306,57 @@ final boolean completeValue(T t) {
return RESULT.compareAndSet(this, null, (t == null) ? NIL : t);
}

static CompletionException wrapInCompletionException(Throwable t) {
if (t == null)
Copy link
Member

Choose a reason for hiding this comment

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

Is there any preexisting code path that ever passes a null? If not I don't think this check is necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I opted to be more safe than sorry. Since this is on the failure-path it isn't performance critical so I think affording a null-check is fine.

@viktorklang-ora viktorklang-ora marked this pull request as ready for review May 10, 2024 14:38
@openjdk openjdk bot added the rfr Pull request is ready for review label May 10, 2024
@mlbridge
Copy link

mlbridge bot commented May 10, 2024

Webrevs

@viktorklang-ora
Copy link
Contributor Author

@AlanBateman Do you have a minute to review this one?

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jun 5, 2024
@viktorklang-ora
Copy link
Contributor Author

/integrate

@openjdk
Copy link

openjdk bot commented Jun 5, 2024

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

  • 9a8096f: 8330047: ASAN build error with gcc 13
  • 6882b38: 8333590: UnmodifiableHeaders.toString() returns a value that represents empty headers
  • cbb6747: 8329581: Java launcher no longer prints a stack trace
  • 789f704: 8322732: ForkJoinPool may underutilize cores in async mode
  • 2c1b311: 8331854: ubsan: copy.hpp:218:10: runtime error: addition of unsigned offset to 0x7fc2b4024518 overflowed to 0x7fc2b4024510
  • 765ad0e: 8331947: Preview creates checkbox for JEP-less preview feature
  • 8e903ee: 8331896: JFR: Improve check for JDK classes
  • 3cbdf8d: 8333554: Parallel: Remove unused PSParallelCompact::is_in
  • 3944e67: 8312132: Add tracking of multiple address spaces in NMT
  • d0052c0: 8333326: Linux Alpine build fails after 8302744
  • ... and 517 more: https://git.openjdk.org/jdk/compare/ea06129851be7bd9876685f74e35392874154179...master

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Jun 5, 2024

@viktorklang-ora Pushed as commit 326dbb1.

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

@He-Pin
Copy link

He-Pin commented Jun 28, 2024

Will this be backported to JDK21U?

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

Successfully merging this pull request may close these issues.

4 participants