Skip to content

Commit

Permalink
Fix: IPC-related issue on non-Windows platforms (#119)
Browse files Browse the repository at this point in the history
* Use SIGTERM instead of IPC on non-Windows platforms

* Refactor serve tests for child exit

* [skip ci] formatting(tests)

This makes it so the IPC graceful exit is only used on non-windows platforms because of electron/electron/issues/14821
  • Loading branch information
Desuuuu authored and nklayman committed Oct 25, 2018
1 parent a7b08ca commit 6b6376d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
30 changes: 24 additions & 6 deletions __tests__/commands.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ describe('electron:build', () => {

describe('electron:serve', () => {
process.env.NODE_ENV = 'development'
const isWin = process.platform === 'win32'

test('typescript is disabled when set in options', async () => {
await runCommand('electron:serve', {
Expand Down Expand Up @@ -466,6 +467,7 @@ describe('electron:serve', () => {
expect(fs.watchFile.mock.calls[0][0]).toBe('projectPath/customBackground')
// Child has not yet been killed or unwatched
expect(mockExeca.send).not.toBeCalled()
expect(mockExeca.kill).not.toBeCalled()
expect(mockExeca.removeAllListeners).not.toBeCalled()
// Main process was bundled and Electron was launched initially
expect(webpack).toHaveBeenCalledTimes(1)
Expand All @@ -475,8 +477,13 @@ describe('electron:serve', () => {
watchCb()
childEvents.exit()
// Electron was killed and listeners removed
expect(mockExeca.send).toHaveBeenCalledTimes(1)
expect(mockExeca.send).toHaveBeenCalledWith('graceful-exit')
if (isWin) {
expect(mockExeca.send).toHaveBeenCalledTimes(1)
expect(mockExeca.send).toHaveBeenCalledWith('graceful-exit')
} else {
expect(mockExeca.kill).toHaveBeenCalledTimes(1)
expect(mockExeca.kill).toHaveBeenCalledWith('SIGTERM')
}
// Process did not exit on Electron close
expect(process.exit).not.toBeCalled()
// Main process file was recompiled
Expand Down Expand Up @@ -509,6 +516,7 @@ describe('electron:serve', () => {
expect(fs.watchFile.mock.calls[1][0]).toBe('projectPath/listFile')
// Child has not yet been killed or unwatched
expect(mockExeca.send).not.toBeCalled()
expect(mockExeca.kill).not.toBeCalled()
expect(mockExeca.removeAllListeners).not.toBeCalled()
// Main process was bundled and Electron was launched initially
expect(webpack).toHaveBeenCalledTimes(1)
Expand All @@ -518,8 +526,13 @@ describe('electron:serve', () => {
watchCb['projectPath/listFile']()
childEvents.exit()
// Electron was killed and listeners removed
expect(mockExeca.send).toHaveBeenCalledTimes(1)
expect(mockExeca.send).toHaveBeenCalledWith('graceful-exit')
if (isWin) {
expect(mockExeca.send).toHaveBeenCalledTimes(1)
expect(mockExeca.send).toHaveBeenCalledWith('graceful-exit')
} else {
expect(mockExeca.kill).toHaveBeenCalledTimes(1)
expect(mockExeca.kill).toHaveBeenCalledWith('SIGTERM')
}
// Process did not exit on Electron close
expect(process.exit).not.toBeCalled()
// Main process file was recompiled
Expand All @@ -531,8 +544,13 @@ describe('electron:serve', () => {
watchCb['projectPath/customBackground']()
childEvents.exit()
// Electron was killed and listeners removed
expect(mockExeca.send).toHaveBeenCalledTimes(2)
expect(mockExeca.send).toHaveBeenCalledWith('graceful-exit')
if (isWin) {
expect(mockExeca.send).toHaveBeenCalledTimes(2)
expect(mockExeca.send).toHaveBeenCalledWith('graceful-exit')
} else {
expect(mockExeca.kill).toHaveBeenCalledTimes(2)
expect(mockExeca.kill).toHaveBeenCalledWith('SIGTERM')
}
// Process did not exit on Electron close
expect(process.exit).not.toBeCalled()
// Main process file was recompiled
Expand Down
14 changes: 10 additions & 4 deletions generator/template/src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,15 @@ app.on('ready', async () => {

// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
process.on('message', data => {
if (data === 'graceful-exit') {
if (process.platform === 'win32') {
process.on('message', data => {
if (data === 'graceful-exit') {
app.quit()
}
})
} else {
process.on('SIGTERM', () => {
app.quit()
}
})
})
}
}
17 changes: 13 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,16 @@ module.exports = (api, options) => {
}

// Attempt to kill gracefully
child.send('graceful-exit')
if (process.platform === 'win32') {
child.send('graceful-exit')
} else {
child.kill('SIGTERM')
}

// Kill after 2 seconds if unsuccessful
// Kill unconditionally after 2 seconds if unsuccessful
childExitTimeout = setTimeout(() => {
if (child) {
child.kill()
child.kill('SIGKILL')
}
}, 2000)
}
Expand Down Expand Up @@ -360,6 +364,11 @@ module.exports = (api, options) => {
// Disable Electron process auto restart
childRestartOnExit = 0

let stdioConfig = [null, null, null]

// Use an IPC on Windows for graceful exit
if (process.platform === 'win32') stdioConfig.push('ipc')

child = execa(
require('electron'),
[
Expand All @@ -375,7 +384,7 @@ module.exports = (api, options) => {
// Disable electron security warnings
ELECTRON_DISABLE_SECURITY_WARNINGS: true
},
stdio: [null, null, null, 'ipc']
stdio: stdioConfig
}
)

Expand Down

0 comments on commit 6b6376d

Please sign in to comment.