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

Workbench: cleanup the process shutdown routines #1364

Merged
merged 5 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
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
22 changes: 4 additions & 18 deletions workbench/src/main/createPythonFlaskProcess.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { spawn, exec } from 'child_process';
import { spawn, execSync } from 'child_process';

import fetch from 'node-fetch';

Expand Down Expand Up @@ -88,26 +88,12 @@ export async function shutdownPythonProcess(subprocess) {
subprocess.kill();
} else {
const { pid } = subprocess;
exec(`taskkill /pid ${pid} /t /f`);
execSync(`taskkill /pid ${pid} /t /f`);
}
} catch (error) {
// if the process was already killed by some other means
logger.debug(error);
} finally {
Promise.resolve();
}

// If we return too quickly, it seems the electron app is allowed
// to quit before the subprocess is killed, and the subprocess remains
// open. Here we poll a flask endpoint and resolve only when it
// gives ECONNREFUSED.
return fetch(`${HOSTNAME}:${process.env.PORT}/ready`, {
method: 'get',
})
.then(async () => {
await new Promise((resolve) => setTimeout(resolve, 300));
return shutdownPythonProcess(subprocess);
})
.catch(() => {
logger.debug('flask server is closed');
return Promise.resolve();
});
}
12 changes: 9 additions & 3 deletions workbench/tests/binary_tests/puppet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { APP_HAS_RUN_TOKEN } from '../../src/main/setupCheckFirstRun';

jest.setTimeout(240000);
const PORT = 9009;
const WAIT_TO_CLICK = 300; // ms
let ELECTRON_PROCESS;
let BROWSER;

Expand Down Expand Up @@ -133,13 +134,15 @@ beforeEach(() => {

afterEach(async () => {
try {
const pages = await BROWSER.pages();
await Promise.all(pages.map(page => page.close()));
Copy link
Member

Choose a reason for hiding this comment

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

Nice.

await BROWSER.close();
} catch (error) {
console.log(BINARY_PATH);
console.error(error);
// Normally BROWSER.close() will kill this process
ELECTRON_PROCESS.kill();
}
ELECTRON_PROCESS.removeAllListeners();
ELECTRON_PROCESS.kill();
});

test('Run a real invest model', async () => {
Expand All @@ -164,6 +167,7 @@ test('Run a real invest model', async () => {
const downloadModal = await page.waitForSelector('.modal-dialog');
const downloadModalCancel = await downloadModal.waitForSelector(
'aria/[name="Cancel"][role="button"]');
await page.waitForTimeout(WAIT_TO_CLICK); // waiting for click handler to be ready
await downloadModalCancel.click();
// We need to get the modelButton from w/in this list-group because there
// are buttons with the same name in the Recent Jobs container.
Expand Down Expand Up @@ -232,15 +236,17 @@ test('Check local userguide links', async () => {
const downloadModal = await page.waitForSelector('.modal-dialog');
const downloadModalCancel = await downloadModal.waitForSelector(
'aria/[name="Cancel"][role="button"]');
await page.waitForTimeout(WAIT_TO_CLICK); // waiting for click handler to be ready
await downloadModalCancel.click();

const investList = await page.waitForSelector('.invest-list-group');
const modelButtons = await investList.$$('aria/[role="button"]');

await page.waitForTimeout(WAIT_TO_CLICK); // first btn click does not register w/o this pause
for (const btn of modelButtons) {
await btn.click();
const link = await page.waitForSelector('text/User\'s Guide');
await page.waitForTimeout(300); // link.click() not working w/o this pause
await page.waitForTimeout(WAIT_TO_CLICK); // link.click() not working w/o this pause
const hrefHandle = await link.getProperty('href');
const hrefValue = await hrefHandle.jsonValue();
await link.click();
Expand Down