Skip to content

Commit

Permalink
[grid] Update for node shutdown gracefully
Browse files Browse the repository at this point in the history
Signed-off-by: Viet Nguyen Duc <[email protected]>
  • Loading branch information
VietND96 committed Oct 21, 2024
1 parent d922168 commit b684f60
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci-java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
# https://github.com/bazelbuild/rules_jvm_external/issues/1046
java-version: 17
run: |
fsutil 8dot3name set 0
bazel test --flaky_test_attempts 3 //java/test/org/openqa/selenium/chrome:ChromeDriverFunctionalTest `
//java/test/org/openqa/selenium/federatedcredentialmanagement:FederatedCredentialManagementTest `
//java/test/org/openqa/selenium/firefox:FirefoxDriverBuilderTest `
Expand Down
17 changes: 13 additions & 4 deletions java/src/org/openqa/selenium/grid/node/local/LocalNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,13 @@ protected LocalNode(
heartbeatPeriod.getSeconds(),
TimeUnit.SECONDS);

Runtime.getRuntime().addShutdownHook(new Thread(this::stopAllSessions));
Runtime.getRuntime()
.addShutdownHook(
new Thread(
() -> {
stopAllSessions();
drain();
}));
new JMXHelper().register(this);
}

Expand All @@ -316,7 +322,6 @@ private void stopTimedOutSession(RemovalNotification<SessionId, SessionSlot> not
}
// Attempt to stop the session
slot.stop();
this.sessionToDownloadsDir.invalidate(id);
// Decrement pending sessions if Node is draining
if (this.isDraining()) {
int done = pendingSessions.decrementAndGet();
Expand Down Expand Up @@ -473,8 +478,6 @@ public Either<WebDriverException, CreateSessionResponse> newSession(
sessionToDownloadsDir.put(session.getId(), uuidForSessionDownloads);
currentSessions.put(session.getId(), slotToUse);

checkSessionCount();

SessionId sessionId = session.getId();
Capabilities caps = session.getCapabilities();
SESSION_ID.accept(span, sessionId);
Expand Down Expand Up @@ -513,6 +516,8 @@ public Either<WebDriverException, CreateSessionResponse> newSession(
span.addEvent("Unable to create session with the driver", attributeMap);
return Either.left(possibleSession.left());
}
} finally {
checkSessionCount();
}
}

Expand Down Expand Up @@ -765,6 +770,10 @@ public HttpResponse uploadFile(HttpRequest req, SessionId id) {
public void stop(SessionId id) throws NoSuchSessionException {
Require.nonNull("Session ID", id);

if (sessionToDownloadsDir.getIfPresent(id) != null) {
sessionToDownloadsDir.invalidate(id);
}

SessionSlot slot = currentSessions.getIfPresent(id);
if (slot == null) {
throw new NoSuchSessionException("Cannot find session with id: " + id);
Expand Down

0 comments on commit b684f60

Please sign in to comment.