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

ipcRenderer.callMain - subsequent calls return non-resolving Promises #35

Closed
maciejewiczow opened this issue Jun 22, 2020 · 11 comments · Fixed by #41
Closed

ipcRenderer.callMain - subsequent calls return non-resolving Promises #35

maciejewiczow opened this issue Jun 22, 2020 · 11 comments · Fixed by #41

Comments

@maciejewiczow
Copy link

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.

@sindresorhus
Copy link
Owner

In my opinion the off calls are redundant anyways,

No. Without the cleanup, if data is called, the error one is never cleaned up.

@maciejewiczow
Copy link
Author

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.

@danwt
Copy link

danwt commented Jun 25, 2020

I have the same issue

@maciejewiczow
Copy link
Author

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

@pixelart7
Copy link

I am having similar issue, and I can confirm that the workaround is working.

@mferris77
Copy link

Also confirming the workaround solves this issue. This is the code at the top of my file where I invoke electron-better-ipc:

const { app, remote, ipcMain, ipcRenderer } = require("electron");
const { ipcRenderer: ipc } = require("electron-better-ipc");

// fix for electron-better-ipc
// from https://github.com/sindresorhus/electron-better-ipc/issues/35
if (ipcMain !== undefined)
  ipcMain.addListener(
    "fix-event-798e09ad-0ec6-5877-a214-d552934468ff",
    () => {}
  );

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

@banben
Copy link

banben commented Aug 15, 2020

Have the same problem too.

@tjklemz
Copy link

tjklemz commented Sep 17, 2020

Same. Makes the library unusable. :( The workaround did not work for me.

@cannc4
Copy link

cannc4 commented Nov 11, 2020

I can confirm that the workaround works. Odd solution though :)

@macharborguy
Copy link

Confirmed both the bug as well as the workaround being successful.

For those who cannot get the workaround, well, working, make sure that the addListener method is chained after the variable containing the specific IPC.

so if you alias your ipcMain or ipcRenderer methods and rename them to simply 'ipc', you will need to add the listeners to 'ipc'

@gushogg-blake
Copy link

Note that you need the applicable part of code in both environments - renderer and main. I have the following:

In src/main.js (main):

let {
	app,
	// etc
	ipcMain,
} = require("electron");

let {ipcMain: ipc} = require("electron-better-ipc");

// HACK for https://github.com/sindresorhus/electron-better-ipc/issues/35
ipcMain.addListener("fix-event-798e09ad-0ec6-5877-a214-d552934468ff", () => {});

In src/app.js (renderer):

// HACK for https://github.com/sindresorhus/electron-better-ipc/issues/35
require("electron").ipcRenderer.addListener("fix-event-79558e00-29ef-5c7f-84bd-0bcd9a0c5cf3", () => {});

t13m added a commit to t13m/electron-better-ipc that referenced this issue Jun 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants