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 concurrency bug #26

Merged
merged 4 commits into from
May 13, 2024
Merged

Fix concurrency bug #26

merged 4 commits into from
May 13, 2024

Conversation

emeasure-github-private
Copy link
Contributor

Thank you for this excellent library!

I ran into unexpected runtime exceptions while decrypting concurrently on the JVM. The issue appears to be due to a typo in pooling.kt (see PR). The proposed change fixes the crashes for my use case.

Copy link
Owner

@whyoleg whyoleg left a comment

Choose a reason for hiding this comment

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

Hey! Thank you for fixing it! Somehow I overlooked this...
Could you also please add a small unit test?

@emeasure-github-private
Copy link
Contributor Author

Sure thing! Attached a small test that fails with the old code.

Copy link
Owner

@whyoleg whyoleg left a comment

Choose a reason for hiding this comment

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

Thank you! Still, I would propose to add some more testing of contract here.

Note: I do think, that this implementation will evolve (by using approach without locks) and also could be used in other providers (on different platforms) so I would be very happy if there will be tests which properly tests the contract!

}

@Test
fun testConcurrentAccessOnCachedPoolShouldNotReuseInstances() = runTest {
Copy link
Owner

Choose a reason for hiding this comment

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

while testing concurrent access is fine, I would say that we don't really need it here.
We can easily write a sequential test here, something like:

pool.use { i1 -> pool.use { i2 -> pool.use { i3 -> }}}

So let's keep both sequential and concurrent variant.

Additionally it would be good to not only count instantiate call, but to check, that instances are different.
So f.e make instance an Int instead of object and save instantiateCount there. And then assert, that the instances used in use are different.
And then, we are good to go!

@emeasure-github-private
Copy link
Contributor Author

Note: I do think, that this implementation will evolve (by using approach without locks) and also could be used in other providers (on different platforms) so I would be very happy if there will be tests which properly tests the contract!

Good idea! Have you considered borrowing a battle-tested implementation? For example, ktor's Pool looks reasonable to me and supports major platforms (although I didn't notice much in the way of unit testing in that repository).

@emeasure-github-private
Copy link
Contributor Author

I added an overlapping sequential unit test, simplified a bit, and added some assertSame/NotSame.

@whyoleg
Copy link
Owner

whyoleg commented Mar 26, 2024

Have you considered borrowing a battle-tested implementation?

Yeah, so, most likely I wanted to borrow an idea of pool implementation from okio/ktor/others - I don't want to add additional dependencies for such small thing.
Also, I haven't implemented something more smart, as I do want to implement proper pooling/caching until there will be support for streaming operations or other issues. May be in the end pooling will be not really needed, some benchmarks are needed here.

Copy link
Owner

@whyoleg whyoleg left a comment

Choose a reason for hiding this comment

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

Nice!
Thank you for PR and addressing all of the comments!

@emeasure-github-private
Copy link
Contributor Author

Makes sense to me! Thanks for the quick review. Looking forward to your next release!

@whyoleg whyoleg merged commit 2837434 into whyoleg:main May 13, 2024
whyoleg pushed a commit that referenced this pull request May 21, 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