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

Implements dialog when closing terminal if process is running #7331

Open
wants to merge 4 commits into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions app/index.ts
Expand Up @@ -18,7 +18,7 @@ remoteInitialize();
import {resolve} from 'path';

// Packages
import {app, BrowserWindow, Menu, screen} from 'electron';
import {app, BrowserWindow, dialog, Event, Menu, screen} from 'electron';
import {gitDescribe} from 'git-describe';
import isDev from 'electron-is-dev';
import * as config from './config';
Expand All @@ -32,6 +32,8 @@ import * as AppMenu from './menus/menu';
import {newWindow} from './ui/window';
import * as windowUtils from './utils/window-utils';
import parseUrl from 'parse-url';
import type Session from './session';
import sessionHasRunningChildren from './utils/session-has-children';

const windowSet = new Set<BrowserWindow>([]);

Expand Down Expand Up @@ -140,7 +142,24 @@ app.on('ready', () =>
void hwin.loadURL(url);

// the window can be closed by the browser process itself
hwin.on('close', () => {
hwin.on('close', (evt) => {
for (const s of hwin.sessions.values()) {
const session: Session = s;
if (sessionHasRunningChildren(session)) {
const opt = dialog.showMessageBoxSync(hwin, {
title: 'Close window & all terminals?',
buttons: ['Close terminal', 'Cancel'],
message: `There is still a process running in a hyper terminal. Closing the terminal will kill it`
});
if (opt == 1) {
evt.preventDefault();
return;
} else {
hwin.clean();
windowSet.delete(hwin);
}
}
}
hwin.clean();
windowSet.delete(hwin);
});
Expand Down
15 changes: 15 additions & 0 deletions app/utils/session-has-children.ts
@@ -0,0 +1,15 @@
import {readFileSync} from 'fs';
import type Session from '../session';

export default function sessionHasRunningChildren(session: Session): boolean {
if (process.platform == 'linux' && session.pty != null) {
try {
const childProcInfoPath = `/proc/${session.pty.pid}/task/${session.pty.pid}/children`;
return readFileSync(childProcInfoPath, 'utf8').length >= 1;
} catch (error) {
return false;
}
} else {
return false;
}
}
15 changes: 15 additions & 0 deletions test/unit/window-utils.test.ts
@@ -1,6 +1,7 @@
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable @typescript-eslint/no-unsafe-call */
import test from 'ava';
import sessionHasRunningChildren from '../../app/utils/session-has-children';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const proxyquire = require('proxyquire').noCallThru();

Expand Down Expand Up @@ -89,3 +90,17 @@ test('positionIsValid() returns false when position isnt valid', (t) => {

t.false(result);
});

test('Closing the terminal should show dialog', (t) => {
const shouldClose = sessionHasRunningChildren({pty: {pid: 1}});
t.true(shouldClose);
});

test('Has a invalid pid', (t) => {
t.false(sessionHasRunningChildren({pty: {pid: -99999}}));
});

test('Should not show dialog', (t) => {
const shouldClose = sessionHasRunningChildren({pty: null});
t.false(shouldClose);
});