Skip to content
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

fix: Fix hung launcher on shutdown of Safari #38

Merged
merged 2 commits into from
Apr 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ const {installWebDrivers} = require('webdriver-installer');
const DRIVER_CACHE = path.join(os.homedir(), '.webdriver-installer-cache');
fs.mkdirSync(DRIVER_CACHE, {recursive: true});

// Delay on startup to allow the WebDriver server to start.
const WEBDRIVER_STARTUP_DELAY_SECONDS = 2;

// If it takes longer than this to close our WebDriver session, give up.
const CLOSE_WEBDRIVER_SESSION_TIMEOUT_SECONDS = 5;
theodab marked this conversation as resolved.
Show resolved Hide resolved

let driversInstalledPromise = null;

// Map nodejs OS names to Selenium platform names.
Expand Down Expand Up @@ -91,7 +97,7 @@ const LocalWebDriverBase = function(baseBrowserDecorator, args, logger) {
});

this.on('start', async (url) => {
await new Promise((resolve) => setTimeout(resolve, 2000));
await delay(WEBDRIVER_STARTUP_DELAY_SECONDS);

this.browser.init(this.spec, (error) => {
if (error) {
Expand All @@ -116,9 +122,18 @@ const LocalWebDriverBase = function(baseBrowserDecorator, args, logger) {
await this.stopWebdriver_();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Say, what's the difference between the kill and forceKill operations? They both just call stopWebdriver_.
Is kill called in such a way that we don't need to worry about them being nested, or about a forceKIll being nested inside a kill?

Copy link
Member Author

Choose a reason for hiding this comment

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

kill() and forceKill() are part of the launcher API, but in my logs, I don't see kill() being called. In a purely external-process-based launcher, the base classes would send different POSIX signals to the subprocess for kill() and forceKill() if I recall correctly.

I initially thought forceKill() should never fail or be infinitely delayed, so I tried adding a timeout at the level of forceKill(). But that didn't solve the issue. In the end, I added the timeout to the WebDriver quit command, since that's the only thing that can fail or be delayed.

So after all the debugging and logging, I concluded that kill() and forceKill() can still effectively be the same, except for the state tracking part.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see. I was going to suggest moving the new logic for saving the operation as a promise into stopWebdriver_, instead of forceKill. But if kill isn't being called I guess it doesn't matter.

};

this.forceKillOperation_ = null;

this.forceKill = async () => {
// Don't nest force-kill operations. If forceKill() was already called,
// just return the same Promise again.
if (this.state == 'BEING_FORCE_KILLED') {
return this.forceKillOperation_;
}

this.state = 'BEING_FORCE_KILLED';
await this.stopWebdriver_();
this.forceKillOperation_ = this.stopWebdriver_();
await this.forceKillOperation_;
};

const originalStart = this.start;
Expand Down Expand Up @@ -173,17 +188,25 @@ const LocalWebDriverBase = function(baseBrowserDecorator, args, logger) {

this.stopWebdriver_ = async () => {
if (this.browser) {
await new Promise(resolve => this.browser.quit(resolve));
// If it takes too long to close the session, give up and move on.
await Promise.race([
delay(CLOSE_WEBDRIVER_SESSION_TIMEOUT_SECONDS),
new Promise(resolve => this.browser.quit(resolve)),
]);
}

// Now that the driver connection and browser are closed, emit the signal
// that shuts down the driver executable.
// Now that the driver connection and browser are closed (or have timed
// out), emit the signal that shuts down the driver executable.
await this.emitAsync('kill');

this.state = 'FINISHED';
};
}

async function delay(seconds) {
await new Promise((resolve) => setTimeout(resolve, seconds * 1000));
}

// Generate a subclass of LocalWebDriverBase and return it.
function generateSubclass(
browserName, launcherName, driverCommand, getDriverArgs,
Expand Down