mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-02-04 05:34:51 +00:00
socket.io connection event after disconnect event loses all browserWindow references #527
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @MopsieX on GitHub (Jul 3, 2020).
Originally assigned to: @GregorBiswanger on GitHub.
When the socket in main.js gets disconnected it deletes all the requires and then it will connect again. When the connection event fires again it reinitializes all of the requires however the windows[] and window variable aren't ever set in browserWindows.js. To fix it the main.js just needs to copy the references of all the windows before it deletes the require cache for browserWindows.js then after the browserWindows.js gets reinitialized/required the main.js needs to pass the windows to it.
Sorry for the bad explanation. I'm terrible at explaining things. If you have any questions or need clarification let me know.
@Velorien commented on GitHub (Jul 6, 2020):
This seems like a probable cause for #421
@MopsieX commented on GitHub (Jul 6, 2020):
Yup you are correct. I get that same error message you get once it happens. Thanks for linking it and having a picture of the error in your bug report. I forgot to include a screenshot of the error message when the bug happens.
Hopefully 9.31.2 is released today with this fixed.
@MopsieX commented on GitHub (Jul 6, 2020):
@GregorBiswanger @Velorien Unfortunately this isn't fixed in the newest version so the problem is still here.
@GregorBiswanger commented on GitHub (Jul 6, 2020):
Hey Community, we have released version 9.31.2 with the electron-builder fix! In addition, I have improved the socket problems again and updated to Electron 9.0.5.
Please remove in your
objfolder theHostandDesktopfolder and try again with the newest Electron.NET API and CLI in 9.31.2.@MopsieX commented on GitHub (Jul 6, 2020):
Thanks I will do that!
@MopsieX commented on GitHub (Jul 6, 2020):
@GregorBiswanger It disconnects less often now but on reconnect the property windows never gets populated with the existing windows which means the app needs to be restarted.
@MopsieX commented on GitHub (Jul 6, 2020):
I think I fixed the bug in the source. I'm testing it now but I need to wait for a disconnect.
@MopsieX commented on GitHub (Jul 7, 2020):
Never mind I was wrong. It's a lot harder to fix than I thought it was. I've learned a lot about how Electron.Net works so tomorrow I should have more luck fixing it.
@freosc commented on GitHub (Jul 13, 2020):
I added
socket.removeAllListeners();to the socket.on('disconnect', function (reason) function in main.js
But that doesn't seem to help.
After 10 socket disconnect/reconnect I get:
I'm not sure if it's related to the removeAllListeners or not. It's hard to debug this code and reproduce 10 disconnects.
@HermesNew commented on GitHub (Jul 16, 2020):
We also encountered the same issue, this issue is more urgent.
@HermesNew commented on GitHub (Jul 16, 2020):
@MopsieX Do you fixed the bug?
@HermesNew commented on GitHub (Jul 21, 2020):
@GregorBiswanger How long will this bug be fixed?
@MopsieX commented on GitHub (Jul 24, 2020):
I haven't fixed it. I got super busy and haven't had more time to try to fix it.
@rakista112 commented on GitHub (Jul 30, 2020):
@MopsieX could you share your plan with us so we can help?
@danatcofo commented on GitHub (Oct 5, 2020):
@MopsieX et all... found a way to externally kill the ports and force a reconnect.
http://www.nirsoft.net/utils/cports.html
My first pass on a fix is basically to do a connect once method. basically replacing
startSocketApiBridge()with the following code in main.js. This doesn't really work however. I'm testing by having a ping/pong host hook call that happens regularly (every 30 seconds) and then use currports app to kill the tcp connection externally.Anyone have any other ideas. Gonna continue to pound on this problem.
@Delpire commented on GitHub (Jan 13, 2021):
@GregorBiswanger is this issue really closed? It was unclear to me based on the last few comments of the merge.
I am seeing an issue where after a period of time, my menu items quit working, and this issue seemed like it may have been the root cause. I could be completely wrong, and if you think so, I can open a new issue. I haven't been able to determine root cause, but it does seem related to a socket disconnect, at least every time I've noticed it, the command prompt has shown a socket disconnect. Losing access to the browser window might explain why the menu items no longer work.
@danatcofo commented on GitHub (Jan 14, 2021):
The issue this fixed was basically fixing the eventing link between the back end and front end being broken when the socket got disconnected and then reconnected. So if you attempted to send a message to a specific window, it never made it there because it didn't exist anymore in the browserWindows array.
If your experiencing a disconnect after a while, and you do not see a reconnect, then that is your issue, it's not related to this on first blush. If however you are seeing the reconnect it may be related and would require further investigation and testing.
This change didn't mess with any connect/disconnect base functionality, only with the consumers of those events.
@Delpire commented on GitHub (Jan 14, 2021):
@danatcofo I am seeing it reconnect. The app continues to work, and the Exit menu item I have works. My other menu items, for which I have written code to do things, stop working. They aren't particularly exciting, they just use explorer.exe to launch a folder or a help file. Those menu items are the only ones that break on the disconnect/reconnect. The only odd thing I may be doing is that I have the callbacks for those menu items tied to a static method on a static class rather than having some sort of instance I create.
@GregorBiswanger commented on GitHub (Jan 14, 2021):
One problem I see is that a .NET library is currently in use for the socket.io support. Unfortunately, it is no longer maintained.
We need an alternative that also supports the latest version of socket.io (3.x) and that we could use without great effort. I'm sure we will get some socket problems improved.
@danatcofo commented on GitHub (Jan 15, 2021):
I haven't run into any menu issues myself @Delpire. though I don't use a static method for consumption. I basically do all of my handlers as inline arrow functions where the internals of them do stuff. generally the internals will call instance methods but the actual menu action handler is an arrow function.
@Delpire commented on GitHub (Jan 16, 2021):
@GregorBiswanger fyi:
@danatcofo - I went ahead and tested this out by removing all my static calls. Now I am just creating the browser window and its menu items in an async method called from Startup.cs'
public void Configure(IApplicationBuilder app, IWebHostEnvironment env). Basically I am doing exactly what the example Electron .NET repo is doing.I tested with several different menu items.
Click = () => {...}Click = async () => {...}When the app launches, all menu items work correctly no matter how many times I use them. Then the moment I get the follow error, they all stop working and do nothing.
The only menu item that works at that point is my Exit menu item, which does not use the Click property. I also thought maybe it was because my BrowserWindow object was falling out of scope, so I went ahead and added it as a private field to my Startup.cs class. Same issue. Disconnection/Reconnection completely breaks my menu items.
Here is an example of what I am doing:
I am using the latest release 9.31.2. I should also mention that I believe I saw that
Electron.WindowManager.BrowserWindowswas empty after a disconnect and reconnect.At this point I am thinking my only work around would be using the ElectronHostHook thing to keep all of this code in the main Electron process, which to some extent defeats a lot of the benefits of Electron .NET
@danatcofo commented on GitHub (Jan 17, 2021):
hmm, well sounds like we have bug still then. I'm assuming you're using windows here rather than macOS as you would be updating the app menu rather than the window menu in that case. Honestly I didn't test your scenario, only tested the socket communication being maintained between the back end and the front end. Looks like there is some more work to do then here. I'll take a look at it soon as I can.
@Delpire commented on GitHub (Jan 19, 2021):
@danatcofo yes I am using Windows.
I can confirm that the issue is still there in master as well. I pulled down the master branch and added a menu item to ElectronNET.WebApp and built it. Once it disconnects and reconnects the menu item is broken. This branch includes your updates to the Typescript files from this PR
https://github.com/ElectronNET/Electron.NET/pull/486
@Delpire commented on GitHub (Jan 20, 2021):
I have some new interesting information. It seems the reason for the disconnect matters and that different machines act differently. I have tested this on 3-4 different machines. On the first machine I saw the issue, it was a physical machine, and it took a while for the problem to occur. On my VM, it happens very frequently. It will basically always happen within 15 minutes or so of launching the application.
On a different physical machine, I left the application running and never saw the disconnect even after 5 or 6 hours. I did however notice a different disconnect:
Even after that, my menu item still works correctly. So it seems that getting
Got disconnect! Reason: ping timeoutis fine, but gettingGot disconnect! Reason: transport closebreaks things.@Delpire commented on GitHub (Jan 26, 2021):
I just thought I would mention that my work around is to take advantage of the ElectronHostHook and Electron's IPC. I can put all my code to create my menu items in some Typescript in an ElectronHostHook, and then if I need a file path that I get at runtime I can pass the file path via electron's IPC. I could use the HookService, but I am worried about the reliability of the socket dependency. So I am going from the ASP .NET Core code to the ipcRenderer via JSInterop, and then the ipcRender tells the ipcMain to keep track of a global variable with the folder path that the menu item's click method uses. Until I feel confident in the communication between the ASP .NET Core process and the main electron process, this route, while more indirect, seems robust.
So far I have not seen any problems with this approach. When the socket inevitably disconnects and reconnects all my menus are still working correctly.
@danatcofo @GregorBiswanger - Can you guys think of any gotcha's or issues with this approach?
@danatcofo commented on GitHub (Jan 28, 2021):
@Delpire I went ahead and opened up a fresh ticket #553 for the menu item issue... Go ahead and comment any extra information your might have there and if I got anything wrong, on the ticket as well.
One think I'm interested in is if this also happens for other menuitems such as the tray, app or dock menu's (now that we have dock menus) especially as mac generally doesn't support browser window menues. However I don't know of an app like CurrPorts on windows that allows me to forcibly close a port in Mac like that does.
@Delpire commented on GitHub (Jan 28, 2021):
@danatcofo thank you! I have added some more information. I will watch that issue moving forward.