Window.open does not work when main process has forked children


#1

Yeaah, bit of a strange one.

I Try to open a new window from within the render process, but In case my main process forks child processes, i cannot open a new window.

 <a onclick="window.open(`http://www.google.nl`, 'editor')">;

If on the main process I open a child process this way, it does NOT work:

 this._child = fork(this.opt.path, [...process.execArgv, `--settings=${childArgv}`], childOpt);

But like this it does work.

 this._child = spawn('node', [this.opt.path, ...process.execArgv, `--settings=${childArgv}`], childOpt);

I really can’t seem to figure out why it would matter!

Anyone any ideas?

Many thanks


Whats up with fork / spawn within Electron?
Whats up with fork / spawn within Electron?
#2

So, I posted a question earlier (Window.open does not work when main process has forked children).

But the problem really seems to happen within spawn/forking of child processes…

If i do NOT package my app, I have to use spawn to make a child process work, otherwise electron cannot open a secondary window (the problem where the first mentioned question is about)

but if i DO package the app, I have to use fork otherwise node cannot find the child file path. But if I use fork, then I cannot open a secondary window with window.open(url)

So the question really is, what is up with Electron and child processes?? Is there some kind of quirk that I have to know??

Thanks!


#3

Are you packaging the app with the asar flag? If you are, the way I’ve understood it is… the child process spawns (for lack of a better way to describe it) “outside of electron”. Only electron can read into the asar files. External processes can’t read into the structure of the asar.


#4

Hi jdfwarrior, thanks for the reply :slight_smile:

Time has past and the problem still exists (I haven’t pushed to production yet).

I indeed package with asar, but I still don’t understand why fork would work when spawn fails, shouldn’t they both have the same problem then?

next to that, I cannot understand why I would have trouble opening a second window from HTML/Javascript, because it doesn’t read anything from my app code itself, it just opens a google page and it fails.

I will try packaging without asar to see if I can get any deeper to understanding why there is a difference.


#5

Spawn creates a completely new process that will run whatever you tell it. Fork will fork the current process (electron) which would allow it to read into the asar.

As far as opening a window in Javascript, that should easy. From the renderer process you should just be able to do this (didn’t test or try this)…

const { remote } = require('electron')
const { BrowserWindow } = remote
let newWindow = new BrowserWindow({
    width: 800, 
    height: 600
})
newWindow.loadUrl('index2.html')

#6

Dude your a hero. Just tried with the new BrowserWindow and it works where window.open fails! And the diff between spawn and fork makes a lot of sense…

Just wondering if Electron will need a relative path then instead of absolute to keep within the asar package…

Will try tonight!

I will buy you a beer (y)


#7

As it seems you know your way around Electron better then most people, I get you a carton of liquer if you know the fix for this one to!!

Thanks @jdfwarrior!!


#8

What a trip… I thought @jdfwarrior solution fixed it, but that was just because I had a second ‘server’ process running in another console window… (The process that forks child_processes). So it was just because I tried everyting at that time that it seems to work

So, the problem is:

When you FORK a process, inside the main electron process and give the child_process {env: process.env}, then the electron process cannot open a second browser window (not even if it loads only google). Note that these processes have absolutly nothing to do with the second window. This problems always happens, also when you give no custom process.env variables at anytime at all.

If you use SPAWN, there is no problem, probably because its not really forking a new NODE process it first goes through the underlying shell (or something)

This happens both in dev mode or if you do a produciton build (ASAR or no ASAR)…

So the solution is:
Do not set all env variables, but just hand-pick them and add them. This is not a permenant solution because I/you cannot always know what environment variables are set…

I guess its because Electron sets some custom env variables that a second browser window cannot handle. If I run exactly the same code in de browser / nodejs the problem does not exists

This has cost me way to long…

Thanks everyone.

I will grab a beer and maybe make a proof of concept as issue for Electron