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

Make the state hash fully thread safe #77

Merged
merged 1 commit into from
Oct 8, 2023

Conversation

mensfeld
Copy link
Contributor

Fixes issue described here: ruby-concurrency/concurrent-ruby#970

Describe the change

Makes the state hash thread-safe

Why are we doing this?

Because the author (you) may not be aware about a potential race condition here.

Benefits

One less thing to worry about in case ppl use it heavily from the require moment in multiple threads.

Drawbacks

Hard to debug issues. Took me weeks to stop it in Karafka.

Requirements

  • Tests written & passing locally? (no)
  • Code style checked?
  • Rebased with master branch?
  • Documentation updated? (no need)
  • Changelog updated? (no need - no API changes)

@granthusbands
Copy link

This isn't enough, as the innermost data structure there is an array and should be something like a Concurrent::Array

There are significant other thread safety concerns through the file that may need attention. Like

if ev_for_fingerprint.empty?
  ev_by_fingerprint.delete(event.fingerprint)
end

In general, using thread-safe data structures is not enough to make a program thread-safe. However, the pull request is still an improvement.

@mensfeld
Copy link
Contributor Author

In general, using thread-safe data structures is not enough to make a program thread-safe. However, the pull request is still an improvement.

That what I was aiming for. Not to fix everything but to start somewhere.

@piotrmurach piotrmurach self-assigned this Aug 20, 2023
Copy link
Owner

@piotrmurach piotrmurach left a comment

Choose a reason for hiding this comment

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

I appreciate you making this PR and bringing this issue to my attention. I want to release a new version shortly and would like to have these changes be part of it. Would you have time to address my comments? It'd be good to include an entry in the changelog as well. I record any internal changes that are considered fixes and this seems to fall into this category. The note can say, for example, Fix a concurrency issue in callbacks initialization or similar.

@@ -18,7 +18,7 @@ class Hooks
def initialize
@hooks_map = Concurrent::Map.new do |events_hash, hook_event|
events_hash[hook_event] = Concurrent::Map.new do |state_hash, name|
Copy link
Owner

Choose a reason for hiding this comment

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

By the same token, shouldn't the events_hash be changed as well?

@@ -18,7 +18,7 @@ class Hooks
def initialize
@hooks_map = Concurrent::Map.new do |events_hash, hook_event|
events_hash[hook_event] = Concurrent::Map.new do |state_hash, name|
state_hash[name] = []
state_hash.compute_if_absent(name) { [] }
Copy link
Owner

Choose a reason for hiding this comment

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

If we're making things safer, it would probably make sense to convert this array to Concurrent::Array.new?

@mensfeld
Copy link
Contributor Author

I'm sorry @piotrmurach but I ATM do not have capacity to apply any fixes to this code anymore. I can close this PR if you are ok with this as I won't be able to do it :(

@piotrmurach
Copy link
Owner

@mensfeld Thank you for the swift reply. I wanted to check with you first and give you a chance to claim this contribution. I'm keen to fix this one way or another. It's up to you what you feel is most appropriate. You can leave it open and I'll force push commits and then create a multi-author commit and merge it in. Or you can close this PR and I'll make a separate commit that fixes all the noted issues and take all the glory 👿 . I'd like you to be mentioned since you kindly brought this issue up. This would also leave a nice trace as to why things were done. No work on your part would be needed.

@mensfeld
Copy link
Contributor Author

@piotrmurach

. You can leave it open and I'll force push commits and then create a multi-author commit and merge it in.

Go for it! Once again sorry I cannot help but at the moment I am overloaded with my own OSS and it's hard for me to context switch

@piotrmurach piotrmurach merged commit 62ccd68 into piotrmurach:master Oct 8, 2023
17 checks passed
@mensfeld
Copy link
Contributor Author

mensfeld commented Oct 8, 2023

Thank you :)

@mensfeld mensfeld deleted the patch-1 branch October 8, 2023 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants