Skip to content

Commit

Permalink
[java] fixed a potential deadlock in processing events #12576
Browse files Browse the repository at this point in the history
  • Loading branch information
joerg1985 committed Aug 26, 2023
1 parent 7773f7b commit 6d6b110
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
9 changes: 8 additions & 1 deletion java/src/org/openqa/selenium/bidi/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,14 @@ private void handleEventResponse(Map<String, Object> rawDataMap) {
+ eventCallbacks.keySet().size()
+ "callbacks available");
Lock lock = callbacksLock.readLock();
lock.lock();
// A waiting writer will block a reader to enter the lock, even if there are currently other
// readers holding the lock. TryLock will bypass the waiting writers and acquire the read lock.
// A thread processing an event (and holding the read-lock) might wait for another event before
// continue processing the event (and releasing the read-lock). Without tryLock this would end
// in a deadlock, as soon as a writer will try to acquire a write-lock.
if (!lock.tryLock()) {
lock.lock();
}
try {
eventCallbacks.keySet().stream()
.filter(
Expand Down
11 changes: 10 additions & 1 deletion java/src/org/openqa/selenium/devtools/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,16 @@ private void handle(CharSequence data) {
"Method {0} called with {1} callbacks available",
new Object[] {raw.get("method"), eventCallbacks.keySet().size()});
Lock lock = callbacksLock.readLock();
lock.lock();
// A waiting writer will block a reader to enter the lock, even if there are currently other
// readers holding the lock. TryLock will bypass the waiting writers and acquire the read
// lock.
// A thread processing an event (and holding the read-lock) might wait for another event
// before continue processing the event (and releasing the read-lock). Without tryLock this
// would end in a deadlock, as soon as a writer will try to acquire a write-lock.
// (e.g. the devtools.idealized.Network works this way)
if (!lock.tryLock()) {
lock.lock();
}
try {
// TODO: Also only decode once.
eventCallbacks.keySet().stream()
Expand Down

0 comments on commit 6d6b110

Please sign in to comment.