Skip to content

Conversation

@msyfls123
Copy link
Contributor

Since process.mainModule.require('electron') maybe be slow, we could possibly get undefined _nodeElectronHandle, so just treat it as a Promise.
image

@ghost
Copy link

ghost commented Apr 28, 2021

CLA assistant check
All CLA requirements met.

Copy link
Member

@pavelfeldman pavelfeldman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I can repro the issue, so I'll be able to add a test for it.


// Below is async.
const handle = await this._nodeElectronHandle!.evaluateHandle(({ BrowserWindow }, windowId) => BrowserWindow.fromId(windowId), windowId).catch(e => {});
const handle = await (await this._nodeElectronHandle)!.evaluateHandle(({ BrowserWindow }, windowId) => BrowserWindow.fromId(windowId), windowId).catch(e => {});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of mentioned race, this._nodeElectronHandle can still be undefined here.

private _nodeSession: CRSession;
private _nodeExecutionContext: js.ExecutionContext | undefined;
_nodeElectronHandle: js.JSHandle<any> | undefined;
_nodeElectronHandle: Promise<js.JSHandle<any> | undefined>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Promise<js.JSHandle<any>> | undefined;

Copy link
Contributor Author

@msyfls123 msyfls123 Apr 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very sure whether '_nodeElectronHandle' will be reached before '_init' has finished or not, I just find '_onPage' and 'close' in server part.

It looks that '_init' will always finish before any '_onPage' when I have tested this PR in my application.

If we wanna ensure '_nodeElectronHandle' to be a Promise, maybe we could extract its initialization to a new method like 'initNodeHandle', and await launch process at top of it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Electron support uses two independent connections, one going to Node and another going to the browser. These connections race naturally and if you throttle the Node one, you can see how browser talks to us before node handle is initialized. You can add await new Promise(f => setTimeout(f, 1000)); before await this._nodeSession.send('Runtime.enable', {}).catch(e => {}); in the init to trigger it. You last push should resolve it! Let's see if it fixes my local test and if it does, we merge it. I'll follow up with the test and a small refactoring.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pavelfeldman Hope it works! I found some eslint issues in my commit, just fix and amend it to the only commit.

@pavelfeldman pavelfeldman merged commit 018f314 into microsoft:master May 1, 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 this pull request may close these issues.

2 participants