Skip to content

Conversation

FarhanAnjum-opti
Copy link
Contributor

@FarhanAnjum-opti FarhanAnjum-opti commented Oct 13, 2025

Summary

Fixes concurrency bug in cmab service by introducing locking in order to ensure consistent result.

Test plan

Introduced unit tests

Issues

FSSDK-11902

# Create a hash of user_id + rule_id for consistent lock selection
hash_input = "#{user_id}#{rule_id}"
# Use Ruby's built-in hash method and ensure positive result
hash_value = hash_input.hash.abs
Copy link
Contributor

Choose a reason for hiding this comment

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

String#hash is not deterministic across Ruby processes. It uses a random seed that changes per process/restart to prevent hash collision attacks. This means:

  • Same input → Different hash values in different Ruby processes
  • Cache keys will map to different locks in different processes
  • If using multi-process deployment (Unicorn, Puma workers), this defeats the purpose

Hmm, we shld use a use a deterministic hash function.

is there ruby gem for murmurhash?
If so the you could use:
require 'murmurhash3'

hash_value = MurmurHash3::V32.str_hash(hash_input)
hash_value % NUM_LOCK_STRIPES

if not, then could use:
require 'digest'

hash_value = Digest::MD5.hexdigest(hash_input).hex % (2**32)
hash_value % NUM_LOCK_STRIPES

Copy link
Contributor

@Mat001 Mat001 left a comment

Choose a reason for hiding this comment

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

looks good

@FarhanAnjum-opti FarhanAnjum-opti merged commit 7f7f988 into master Oct 16, 2025
10 checks passed
@FarhanAnjum-opti FarhanAnjum-opti deleted the farhan/FSSDK-11902-fix-cmab-concurrency-bug branch October 16, 2025 14:41
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