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

fix: safety close in AsyncConnectionProvider #2935 #2960

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Roiocam
Copy link
Contributor

@Roiocam Roiocam commented Aug 15, 2024

Motivation

ConcurrentHashMap didn't guarantee the iterator consistency with entries...

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.

@tishun
Copy link
Collaborator

tishun commented Aug 30, 2024

Hey @Roiocam

is this related to #2935?

@tishun tishun added the status: waiting-for-feedback We need additional information before we can continue label Aug 30, 2024
@Roiocam
Copy link
Contributor Author

Roiocam commented Aug 30, 2024

Hey @Roiocam

is this related to #2935?

Yes, I knew it wasn't thread-safe, but my original post didn't figure out the actually reason.

I think my latest commit with a test case can strongly explain why, it also updates the solution of this issue.

@Roiocam Roiocam changed the title fix: safety iterating #2935 fix: safety close in AsyncConnectionProvider #2935 Aug 30, 2024
Copy link
Contributor Author

@Roiocam Roiocam left a comment

Choose a reason for hiding this comment

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

@tishun could you take a look? thanks

CountDownLatch slowCreate = new CountDownLatch(1);
CountDownLatch slowShutdown = new CountDownLatch(1);

// create a provider with a slow connection creation
Copy link
Contributor Author

Choose a reason for hiding this comment

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

that's means it will create an Sync but hasn't completed yet, even when we need to close provider

});

// add slow shutdown connection first
SlowCloseFuture slowCloseFuture = new SlowCloseFuture(slowShutdown);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

use for blocking the iterating.

CompletableFuture<AsyncCloseable> createFuture = provider.getConnection("slowCreate");

// close the connection.
CompletableFuture<Void> closeFuture = provider.close();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

close the provider, the hashmap contains two elements, but only the slowShutdown key Sync will be add it to close futures, because it has been created, but slowCreate didn't(it will add it to futures after creation completed, this path will cause unsafety, which mean IndexOutOfBound or ConcurrentModificationException)

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

Successfully merging this pull request may close these issues.

2 participants