question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ipcRenderer.callMain - subsequent calls return non-resolving Promises

See original GitHub issue

When ipcRenderer.callMain gets called two or more times, the first Promise resolves normally, but subsequent calls return non-resolving Promises. I’m using typescript and electron-webpack. I’ve thrown together a quick bug repro repo to ilustrate the problem. repro I did a bit of investigation around the method’s source code and found out that in the second call, the callbacks (onData, onError) were not called. But, when I commented out these lines, the issue disappeared (at least for my test case)

ipc.callMain = (channel, data) => new Promise((resolve, reject) => {
	const {sendChannel, dataChannel, errorChannel} = util.getResponseChannels(channel);

	const cleanup = () => {
-		ipc.off(dataChannel, onData);
-		ipc.off(errorChannel, onError);
+		// ipc.off(dataChannel, onData);
+		// ipc.off(errorChannel, onError);
	};

	const onData = (event, result) => {
		cleanup();
		resolve(result);
	};

	const onError = (event, error) => {
		cleanup();
		reject(deserializeError(error));
	};

	ipc.once(dataChannel, onData);
	ipc.once(errorChannel, onError);

In my opinion the off calls are redundant anyways, because the listeners are attached using once, so they will be removed automatically after first event regardless (and that’s what happens after my change, as far as I know). I feel like the double removal might be the issue here.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
maciejewiczowcommented, Jul 11, 2020

For now, my temporary workaround around the issue is to put the following code in the root file of the part of the project that uses electron-better-ipc

if (ipcMain !== undefined)
    ipcMain.addListener('fix-event-798e09ad-0ec6-5877-a214-d552934468ff', () => {});

if (ipcRenderer !== undefined)
    ipcRenderer.addListener('fix-event-79558e00-29ef-5c7f-84bd-0bcd9a0c5cf3', () => {});

No idea why this changes anything though, just stumbled upon it randomly

1reaction
maciejewiczowcommented, Jun 23, 2020

Okay, I haven’t thought of this. But nonetheless, something strange is going on here. After another bit of poking around I discovered that adding any listener to ipc emitter before first call to callMain also fixes this issue. I have even less of an idea now as to what might be the cause of this behavior.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ipcRenderer - Electron
Returns Promise <any> - Resolves with the response from the main process. Send a message to the main process via channel and expect...
Read more >
IpcMain and IpcRenderer call main.js function from index.html
If your "mainjsfunction" is located in your main process (eg main.js) you can/should use ipc to trigger your function.
Read more >
How to use the electron-better-ipc.ipcRenderer.callMain ...
Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues...
Read more >
Electron IPC Response/Request architecture with TypeScript
These events are called channels within Electron ... the response has arrived. return new Promise(resolve => { ipcRenderer.once(request.
Read more >
electron-better-ipc | Yarn - Package Manager
... built-in IPC is that it enables you to send a message and get the response back in the same call. This would...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found