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

Add defensive copy for Futures allOf() method #2943

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jinkshower
Copy link

@jinkshower jinkshower commented Aug 6, 2024

Closes #2935

I've added defensive copy for the method so that external modification could not affect stages during iteration.

As for test, I can't come up with any better idea so just used two Threads each calling allOf() and modifying stages simultaneously.

Thank you.

Make sure that:

  • You have read the contribution guidelines.
  • You have created a feature request first to discuss your contribution intent. Please reference the feature request ticket number in the pull request.
  • You applied code formatting rules using the mvn formatter:format target. Don’t submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.

@jinkshower
Copy link
Author

jinkshower commented Aug 7, 2024

I've applied provided formatter again! Could you re-run the test please? Thank you in advance.

@tishun
Copy link
Collaborator

tishun commented Aug 8, 2024

I've applied provided formatter again! Could you re-run the test please? Thank you in advance.

You need to fetch the fix from #2949

@jinkshower
Copy link
Author

You need to fetch the fix from #2949

I've fetched #2949 and pushed it again!

@@ -32,10 +32,11 @@ public static CompletableFuture<Void> allOf(Collection<? extends CompletionStage

LettuceAssert.notNull(stages, "Futures must not be null");

CompletableFuture[] futures = new CompletableFuture[stages.size()];
CompletionStage[] copies = stages.toArray(new CompletionStage[0]);
CompletableFuture[] futures = new CompletableFuture[copies.length];

Copy link
Contributor

Choose a reason for hiding this comment

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

Why need to create a copy array if you only need to get the size?

Copy link
Author

@jinkshower jinkshower Aug 11, 2024

Choose a reason for hiding this comment

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

the issue stated that this method throws ArrayIndexOutOfBoundsException. @tishun mentioned in #2935 that is probably because of the concurrent modification of the 'stages' and I agreed. So I created copy array and used in the for loop.

for (CompletionStage<?> stage : copies) { //use copies instead of stages 
    futures[index++] = stage.toCompletableFuture();
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I see, thanks.

@tishun tishun added the status: waiting-for-feedback We need additional information before we can continue label Aug 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-feedback We need additional information before we can continue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Seeing ArrayIndexOutOfBoundsException
3 participants