Skip to content

Fixed assignment to catch block parameter #15384

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 1 commit into from
Oct 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions server/src/main/java/org/opensearch/search/SearchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -680,14 +680,15 @@
}
} catch (Exception e) {
// execution exception can happen while loading the cache, strip it
if (e instanceof ExecutionException) {
e = (e.getCause() == null || e.getCause() instanceof Exception)
? (Exception) e.getCause()
: new OpenSearchException(e.getCause());
Exception exception = e;
if (exception instanceof ExecutionException) {
exception = (exception.getCause() == null || exception.getCause() instanceof Exception)
Copy link
Member

Choose a reason for hiding this comment

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

@dbwiddis Does this logic make sense? If exception.getCause() is null, then we assign null to exception, which is going to NPE when we do throw exception; three lines later. Am I reading this right?

Copy link
Member

Choose a reason for hiding this comment

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

@andrross I didn't fully study the logic, as it already existed.

The only change here was really making a copy of the e argument passed to the catch block and renaming all the other e to exception. It actually would have made a smaller diff had it been catch (Exception ex) followed by Exception e = ex and then no other changes.

That said, I see the former logic does include casting null as an Exception and then rethrowing it later. But all that said, will the cause of an ExecutionException ever be null in this situation? I don't think so.

Copy link
Member

Choose a reason for hiding this comment

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

Agree this change is good by itself.

The existing logic seems to be quite wonky, but I think you're right that the contract of ExecutionException is that the cause will never be null.

Copy link
Member

Choose a reason for hiding this comment

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

the contract of ExecutionException is that the cause will never be null.

I think you can explicitly create one with the no-param constructor. But I can't find any instances of that in the OpenSearch codebase, and most other handling of this instanceof don't even bother to check for null. So we could probably remove the null check. It's an NPE in either case, just a different spot in the code.

? (Exception) exception.getCause()
: new OpenSearchException(exception.getCause());

Check warning on line 687 in server/src/main/java/org/opensearch/search/SearchService.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/search/SearchService.java#L686-L687

Added lines #L686 - L687 were not covered by tests
}
logger.trace("Query phase failed", e);
processFailure(readerContext, e);
throw e;
logger.trace("Query phase failed", exception);
processFailure(readerContext, exception);
throw exception;
} finally {
taskResourceTrackingService.writeTaskResourceUsage(task, clusterService.localNode().getId());
}
Expand Down
Loading