Skip to content

Commit

Permalink
Merge pull request #982 from ubports/kill
Browse files Browse the repository at this point in the history
Kill
  • Loading branch information
NeoTheThird authored Oct 30, 2019
2 parents 79aa916 + 78f67c4 commit fa90f8f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 31 deletions.
8 changes: 0 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"download": "^7.1.0",
"electron-prompt": "^1.4.0",
"popper.js": "^1.14.3",
"ps-tree": "^1.2.0",
"checksum": "^0.1.1",
"commander": "^2.9.0",
"electron-open-link-in-browser": "^1.0.2",
Expand All @@ -61,7 +62,6 @@
"promise-android-tools": "^1.0.7",
"request": "^2.79.0",
"system-image-node-module": "^1.0.10",
"terminate": "^2.1.2",
"ubports-api-node-module": "^2.0.1",
"winston": "^3.1.2"
}
Expand Down
12 changes: 8 additions & 4 deletions src/devices.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ function assembleInstallSteps(steps) {
error.includes("No such device")
) {
mainEvent.emit("user:connection-lost", runStep);
} else if (error.includes("Killed")) {
reject(); // Used for exiting the installer
} else {
utils.errorToUser(error, step.type);
}
Expand Down Expand Up @@ -316,10 +318,12 @@ function assembleInstallSteps(steps) {
function install(steps) {
var installPromises = assembleInstallSteps(steps);
// Actually run the steps
installPromises.reduce(
(promiseChain, currentFunction) => promiseChain.then(currentFunction),
Promise.resolve()
);
installPromises
.reduce(
(promiseChain, currentFunction) => promiseChain.then(currentFunction),
Promise.resolve()
)
.catch(() => {}); // errors can be ignored here, since this is exclusively used for killing the promise chain
}

module.exports = {
Expand Down
30 changes: 14 additions & 16 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
const cli = require("commander");
const electron = require("electron");
const electronPug = require("electron-pug");
const terminate = require("terminate");

const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
Expand Down Expand Up @@ -188,9 +187,8 @@ ipcMain.on("option", (event, targetVar, value) => {
mainEvent.on("user:error", err => {
try {
if (mainWindow) mainWindow.webContents.send("user:error", err);
else utils.die(err);
else process.exit(1);
} catch (e) {
utils.log.error(e);
process.exit(1);
}
});
Expand Down Expand Up @@ -359,11 +357,6 @@ function createWindow() {
global.packageInfo.version +
"!"
);
utils.log.info(
"This is " +
(global.packageInfo.updateAvailable ? "not " : "") +
"the latest stable version!"
);
mainWindow = new BrowserWindow({
width: cli.cli ? 0 : cli.debug ? 1600 : 800,
height: cli.cli ? 0 : 600,
Expand All @@ -381,11 +374,15 @@ function createWindow() {
devices.waitForDevice();
}
})
.catch(e => utils.errorToUser(e, "Failed to start adb server"));
.catch(e => {
if (!e.includes("Killed"))
utils.errorToUser(e, "Failed to start adb server");
});
api
.getDeviceSelects()
.then(out => {
mainWindow.webContents.send("device:wait:device-selects-ready", out);
if (mainWindow)
mainWindow.webContents.send("device:wait:device-selects-ready", out);
})
.catch(e => {
utils.log.error("getDeviceSelects error: " + e);
Expand All @@ -404,9 +401,7 @@ function createWindow() {
);
mainWindow.webContents.send("user:update-available");
})
.catch(() => {
utils.log.debug("This is the latest version.");
});
.catch(() => {}); // Ignore errors, since this is non-essential
});

mainWindow.loadURL(
Expand All @@ -431,12 +426,15 @@ function createWindow() {
app.on("ready", createWindow);

app.on("window-all-closed", function() {
adb.killServer().catch();
utils.log.info("Good bye!");
adb
.killServer()
.then(utils.killSubprocesses)
.catch(utils.killSubprocesses);
if (process.platform !== "darwin") {
utils.log.info("Good bye!");
setTimeout(() => {
app.quit();
terminate(process.pid, console.error);
process.exit(0);
}, 1000);
}
});
Expand Down
26 changes: 24 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const checksum = require("checksum");
const mkdirp = require("mkdirp");
const cp = require("child_process");
const getos = require("getos");
const psTree = require("ps-tree");
global.packageInfo = require("../package.json");

if (!fs.existsSync(getUbuntuTouchDir())) {
Expand Down Expand Up @@ -229,12 +230,32 @@ let toolpath = global.packageInfo.package
platforms[os.platform()]
)
: path.join(__dirname, "..", "platform-tools", platforms[os.platform()]);
let processes = [];
function execTool(tool, args, callback) {
exec(
let pid = exec(
[path.join(toolpath, tool)].concat(args).join(" "),
{ options: { maxBuffer: 1024 * 1024 * 2 } },
{
maxBuffer: 1024 * 1024 * 2
},
callback
);
processes.push(pid);
pid.on("exit", () => {
processes.splice(processes.indexOf(pid), 1);
});
}

// Since child_process.exec spins up a shell on posix, simply killing the process itself will orphan its children, who then will be adopted by pid 1 and continue running as zombie processes until the end of time.
function killSubprocesses() {
if (process.platform === "win32") {
processes.forEach(child => child.kill());
} else {
processes.forEach(pid => {
psTree(pid.pid, function(err, children) {
cp.spawn("kill", ["-9"].concat(children.map(p => p.PID)));
});
});
}
}

function isSnap() {
Expand Down Expand Up @@ -370,6 +391,7 @@ module.exports = {
log: log,
isSnap: isSnap,
execTool: execTool,
killSubprocesses: killSubprocesses,
getUbuntuTouchDir: getUbuntuTouchDir,
sendBugReport: sendBugReport,
getUpdateAvailable: getUpdateAvailable,
Expand Down

0 comments on commit fa90f8f

Please sign in to comment.