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

Sharing a connection between threads is not thread safe on JRuby #801

Open
JonathanPierce opened this issue Dec 2, 2022 · 2 comments
Open
Labels

Comments

@JonathanPierce
Copy link

If we look at connection.rb and how it handles the sockets, socket, and reset methods, there is an issue that only appears is true concurrency environments like JRuby: multiple threads can try to mutate the cache hashes at once.

Since code calls the socket method multiple times, it means that two calls can return two different sockets if threads clobbered the hash creation/access/etc...

To fix this, a Mutex needs to be defined per-connection and these three methods need to be wrapped in mutex.synchornize blocks.

This is an extreme edge case that requires:

  • A shared connection object between threads
  • Extreme lack of luck with timing
  • A ruby interpreter with actual parallelism (this will never happen on CRuby)

Also, you might want to use ObjectSpace::WeakMap for the per-thread cache to avoid memory leaks.

@geemus
Copy link
Contributor

geemus commented Dec 2, 2022

Thanks for the detailed report. It's too bad we have this issue, but at least it doesn't seem likely to come up all the time.

@JonathanPierce would you have capacity and interest to work on the fixes?

I suspect I could eventually look into it, but don't think I'll have much capacity/bandwidth for the foreseeable future.

@github-actions
Copy link

github-actions bot commented Feb 1, 2023

This issue has been marked inactive and will be closed if no further activity occurs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants