diff --git a/java/src/org/openqa/selenium/grid/distributor/local/LocalDistributor.java b/java/src/org/openqa/selenium/grid/distributor/local/LocalDistributor.java index b6208734fd066..daeb21b0f815f 100644 --- a/java/src/org/openqa/selenium/grid/distributor/local/LocalDistributor.java +++ b/java/src/org/openqa/selenium/grid/distributor/local/LocalDistributor.java @@ -753,7 +753,17 @@ private class NewSessionRunnable implements Runnable { @Override public void run() { - boolean loop = true; + Set inQueue; + boolean loop; + if (rejectUnsupportedCaps) { + inQueue = sessionQueue.getQueueContents().stream() + .map(SessionRequestCapability::getRequestId) + .collect(Collectors.toSet()); + loop = !inQueue.isEmpty(); + } else { + inQueue = null; + loop = true; + } while (loop) { // We deliberately run this outside of a lock: if we're unsuccessful // starting the session, we just put the request back on the queue. @@ -762,13 +772,11 @@ public void run() { Map stereotypes = getAvailableNodes().stream() .filter(NodeStatus::hasCapacity) - .map( + .flatMap( node -> node.getSlots().stream() - .map(Slot::getStereotype) - .collect(Collectors.toList())) - .flatMap(Collection::stream) - .collect(Collectors.groupingBy(ImmutableCapabilities::new, Collectors.counting())); + .map(Slot::getStereotype)) + .collect(Collectors.groupingBy(ImmutableCapabilities::copyOf, Collectors.counting())); if (!stereotypes.isEmpty()) { List matchingRequests = sessionQueue.getNextAvailable(stereotypes); @@ -780,7 +788,9 @@ public void run() { } } if (rejectUnsupportedCaps) { - checkMatchingSlot(sessionQueue.getQueueContents()); + checkMatchingSlot(sessionQueue.getQueueContents().stream() + .filter((src) -> inQueue.contains(src.getRequestId())) + .collect(Collectors.toList())); } }