Skip to content

Conversation

muzahidul-opti
Copy link
Contributor

@muzahidul-opti muzahidul-opti commented Oct 21, 2025

Summary

  • Introduce a lock array to prevent race conditions
  • Implement getLockIndex method for calculating lock index based on user and rule IDs

Test plan

  • All test cases should be passed

Issues

  • FSSDK-11900

- Introduce a lock array to prevent race conditions
- Implement getLockIndex method for calculating lock index based on user and rule IDs
@muzahidul-opti muzahidul-opti changed the title refactor: improve thread safety in getDecision function [FSSDK-11900] fix: potential concurrent issue for cmab decision Oct 21, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR addresses a potential race condition in the CMAB decision-making process by implementing a lock array mechanism to ensure thread-safe access to decision operations.

  • Introduces an array of 1000 NSLock objects to manage concurrent access
  • Implements a hash-based lock selection strategy using user and rule IDs to distribute lock contention

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@muzahidul-opti muzahidul-opti marked this pull request as ready for review October 21, 2025 15:58
raju-opti
raju-opti previously approved these changes Oct 22, 2025
- Replace hash function with MurmurHash3 for improved performance
- Calculate lockIndex directly instead of using intermediate variables
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

private func getLockIndex(userId: String, ruleId: String) -> Int {
let combinedKey = userId + ruleId
let hashValue = MurmurHash3.hash32(key: combinedKey)
let lockIndex = Int(hashValue) % Self.NUM_LOCKS
Copy link

Copilot AI Oct 22, 2025

Choose a reason for hiding this comment

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

The conversion from UInt32 to Int could result in a negative value on platforms where Int is 32-bit, leading to a potential crash when accessing the locks array. Use Int(hashValue % UInt32(Self.NUM_LOCKS)) to ensure the modulo operation happens before conversion.

Suggested change
let lockIndex = Int(hashValue) % Self.NUM_LOCKS
let lockIndex = Int(hashValue % UInt32(Self.NUM_LOCKS))

Copilot uses AI. Check for mistakes.

Comment on lines +73 to +75
return lock.withLock {
var result: Result<CmabDecision, Error>!
let semaphore = DispatchSemaphore(value: 0)
Copy link

Copilot AI Oct 22, 2025

Choose a reason for hiding this comment

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

Using a semaphore to block while holding a lock can lead to deadlock if the asynchronous callback in getDecision attempts to acquire the same lock. The lock should be released before waiting on the semaphore, or the async operation should be moved outside the lock scope.

Copilot uses AI. Check for mistakes.

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