TooTallNate/node-weak

Weak in electron: works in main, fails in renderer -- why?

Opened this issue · 3 comments

3n-mb commented

The following has been observed. weak can be built and used in electron app, as long as import/require is done in main process. If weak is require-d in renderer, renderer process crashes when registered object is garbage collected.
Why?
And should it work in renderer, or, of cause it shouldn't?

To be honest, I'm not really sure about the implementation details of Electron's renderer process, though I know that they do some magic bridging that might be getting in the way. You might need to check with them if there's something special that needs to be done.

@3n-mb seeing a similar thing. Have you learned anything new in the mean time?

3n-mb commented

@martijnthe I have no info about why this happens. But I have a working horrible hack that allows to have weak-like functionality in renderer. It goes like this:

Workaround

  • Electron's rmi works with the following rule: when renderer gives object to main, main will have a shadow (proxy) of it, as long as a reference in renderer is alive.
  • In main, we make a weak service, exposed to renderer via rmi: added to global on main side, and used in renderer via remote.getGlobal(name).
  • This weak service in main uses weak module, in which objects (as proxy shadows) are given from renderer along with callbacks, defined and executed in renderer.

Electron's rmi has ability to track objects in renderer and react (callback something) when they are gone. May be, one may find in code how it is done, hopefully in js, but the rabbit hole may go all the way to atom.binding.
The hack is horrible only in its double use of reference tracking (in rmi and in weak), and a fact that callbacks are done over the slower boundary, which may not matter for slower code.