-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[grid] Improve race conditions in Grid session distribution #16939
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
Conversation
Signed-off-by: Viet Nguyen Duc <nguyenducviet4496@gmail.com>
PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
|||||||||||||||||||||||||||||||||||
PR Code Suggestions ✨Explore these optional code suggestions:
|
||||||||||||
Signed-off-by: Viet Nguyen Duc <nguyenducviet4496@gmail.com>
67d8350 to
5f84284
Compare
User description
🔗 Related Issues
💥 What does this PR do?
reserveSlot()with write lock is exclusive - only one thread can hold it. All other threads must wait, even though selectSlot() is just reading data. Now, use read lock for slot selection (allows concurrent reads), write lock only for actual reservation (brief, atomic). The procedure would beImprove
When multiple concurrent session requests arrive, the Grid can send multiple requests to the same Node slot, causing the Node to reject with "Max session count reached" even when slots should be available. (point 2 - they may pick the same slot).
Few reasons: The Distributor maintains a snapshot of slot states, but multiple threads could:
Only the first thread to acquire write lock can reserve a slot. Subsequent threads see session != null and try the next slot.
volatileensures visibility across threads. Logging moved before stop for better debugging.computeIfPresentprovides atomic check-and-remove, preventing race where inUse could be incremented between check and removal.🔧 Implementation Notes
💡 Additional Considerations
🔄 Types of changes
PR Type
Bug fix
Description
Optimize locking strategy in slot selection using read locks for concurrent reads
Prevent double reservation by filtering already-reserved slots during selection
Add volatile modifier to ensure thread-safe visibility of session state
Fix TOCTOU race condition in HTTP client cleanup using atomic operations
Diagram Walkthrough
File Walkthrough
LocalDistributor.java
Optimize locking strategy for concurrent slot selectionjava/src/org/openqa/selenium/grid/distributor/local/LocalDistributor.java
only for reservation
successfully
LocalGridModel.java
Prevent double reservation with session state checkjava/src/org/openqa/selenium/grid/distributor/local/LocalGridModel.java
SessionSlot.java
Add volatile modifier and improve logging in SessionSlotjava/src/org/openqa/selenium/grid/node/local/SessionSlot.java
visibility
better debugging
HandleSession.java
Fix TOCTOU race in HTTP client cleanup with atomic operationsjava/src/org/openqa/selenium/grid/router/HandleSession.java
operations
check and removal
computeIfPresent