-
-
Save deepak1556/59f981cd8a55cda92beadd6bf97330a9 to your computer and use it in GitHub Desktop.
Broken event loop in the renderer process on windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const net = require('net'); | |
let counter = 0; | |
const pipename = '\\\\.\\pipe\\testpipe'; | |
process.send('Child process created'); | |
const errCb = () => { | |
process.send('Error creating named pipe client'); | |
}; | |
const socket = net.createConnection(pipename, () => { | |
socket.removeListener('error', errCb); | |
registerSocket(); | |
}); | |
socket.once('error', errCb); | |
socket.on('close', () => { | |
process.send('renderer closed the socket.'); | |
}); | |
function registerSocket() { | |
process.send("Connected to named pipe as client"); | |
socket.on('data', (data) => { | |
process.send(`rec'd from server: ${data}`); | |
setTimeout(function() { | |
socket.write(`ping ${counter++}`); | |
}, 1000); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Hello One!</title> | |
</head> | |
<body> | |
<h1>Hello World!</h1> | |
<!-- You can also require other files to run in this process --> | |
<script src="./renderer.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Modules to control application life and create native browser window | |
const {app, BrowserWindow} = require('electron') | |
const path = require('path') | |
const { fork } = require('child_process') | |
let child = null; | |
function createWindow () { | |
// Create the browser window. | |
const mainWindow = new BrowserWindow({ | |
width: 800, | |
height: 600, | |
webPreferences: { | |
nodeIntegration: true, | |
sandbox: false, | |
contextIsolation: false | |
} | |
}) | |
// and load the index.html of the app. | |
mainWindow.loadFile('index.html') | |
mainWindow.webContents.on('did-start-loading', () => { | |
console.log('Renderer will start navigating'); | |
if (child) { | |
console.log('Kill forked process'); | |
child.kill('SIGKILL'); | |
child = null; | |
} | |
}) | |
mainWindow.on('close', () => { | |
console.log('App exiting'); | |
if (child) { | |
console.log('Kill forked process'); | |
child.kill('SIGKILL'); | |
child = null; | |
} | |
}) | |
mainWindow.webContents.on('did-finish-load', () => { | |
console.log('Renderer finished loading'); | |
if (!child) { | |
child = fork(`${__dirname}/child.js`, { execArgv: ['--inspect=5870']}) | |
child.on('message', (msg) => { | |
console.log(`Forked process: ${msg}`) | |
}) | |
child.on('close', () => { | |
console.log('forked process exited') | |
}) | |
} | |
}) | |
} | |
// This method will be called when Electron has finished | |
// initialization and is ready to create browser windows. | |
// Some APIs can only be used after this event occurs. | |
app.whenReady().then(() => { | |
createWindow() | |
app.on('activate', function () { | |
// On macOS it's common to re-create a window in the app when the | |
// dock icon is clicked and there are no other windows open. | |
if (BrowserWindow.getAllWindows().length === 0) createWindow() | |
}) | |
}) | |
// Quit when all windows are closed, except on macOS. There, it's common | |
// for applications and their menu bar to stay active until the user quits | |
// explicitly with Cmd + Q. | |
app.on('window-all-closed', function () { | |
if (process.platform !== 'darwin') app.quit() | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This file is required by the index.html file and will | |
// be executed in the renderer process for that window. | |
// No Node.js APIs are available in this process because | |
// `nodeIntegration` is turned off. Use `preload.js` to | |
// selectively enable features needed in the rendering | |
// process. | |
const net = require('net'); | |
let counter = 0; | |
const pipename = '\\\\.\\pipe\\testpipe'; | |
let serverSocket = null; | |
let namedPipeServer = net.createServer(); | |
const errCb = () => { | |
console.log('Error creating named pipe server'); | |
}; | |
namedPipeServer.on('error', errCb); | |
namedPipeServer.listen(pipename, () => { | |
if (namedPipeServer) { | |
namedPipeServer.removeListener('error', errCb); | |
} | |
}) | |
let handle = setTimeout(() => { | |
if (namedPipeServer) { | |
namedPipeServer.close(); | |
namedPipeServer = null; | |
} | |
console.log('Client took more than 60s to connect.'); | |
}, 60 * 1000); | |
if (namedPipeServer) { | |
namedPipeServer.on('connection', (socket) => { | |
console.log("Connected to named pipe as server"); | |
clearTimeout(handle); | |
if (namedPipeServer) { | |
namedPipeServer.close(); | |
namedPipeServer = null; | |
} | |
serverSocket = socket; | |
registerSocket(serverSocket); | |
}); | |
} | |
function registerSocket(socket) { | |
socket.on('data', (data) => { | |
console.log(`rec'd from client: ${data}`); | |
setTimeout(function() { | |
socket.write(`pong ${counter++}`); | |
}, 1000); | |
}); | |
socket.write('pong!'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment