uNetworking / uWebSockets.js

μWebSockets for Node.js back-ends :metal:
Apache License 2.0
7.86k stars 569 forks source link

Broken with Compiled Exe (Node Single Executable Application SEA) #1104

Open SammyLee40 opened 1 day ago

SammyLee40 commented 1 day ago

Hi,

I was recently trying out UWS and loved the performance gains, but wanted to make sure it worked with .exe. (https://nodejs.org/api/single-executable-applications.html#single-executable-applications)

First, I tried using it with npx webpack. Works great!

Then, I tried using it by injecting the blob into my executable. This works for other .node files perfectly, but with the same uws_win32_x64_120.node we used for our bundle.js that worked- we see it crash our exe with no error.

I've managed to strip away everything and get it down to the one function causing the error, so other than require() or createRequire(), the real problem is dlopen(). For whatever reason, even with the same system, seconds after running the working dlopen on a bundled JS file, the same method fails when run inside an executable. I've debugged to making sure the node file exists and is found properly, but can't go much further without C code, as NodeJS exe refuses to catch the error, crashing instead, making me think some kind of segfault or otherwise is happening on UWS's side.

I've created a working minimal example below.

To run it, just do:

node server.js

which should work and show:

node server.js

Starting server... before UWS __dirname: C:\Users\slee\Downloads\minimal UWS path: C:\Users\slee\Downloads\minimal\uws_win32_x64_120.node Attempting to load UWS native addon... UWS loaded successfully module.exports keys: [ 'App', 'SSLApp', 'H3App', ... 'DEDICATED_DECOMPRESSOR_1KB', 'DEDICATED_DECOMPRESSOR_512B', 'LIBUS_LISTEN_EXCLUSIVE_PORT' ] Starting server... after UWS App instance created successfully Server started on port 3000

Then you can do npx webpack and node bundle.js and see the same output, indicating packing works fine. However, if we compiled the executable by doing:

node -e "require('fs').copyFileSync(process.execPath, 'test.exe')"

npm run sea

npm run postject

then running test.exe shows:

C:\Users\slee\Downloads\minimal>test.exe Starting server... before UWS __dirname: C:\Users\slee\Downloads\minimal UWS path: C:\Users\slee\Downloads\minimal\uws_win32_x64_120.node Attempting to load UWS native addon...

C:\Users\slee\Downloads\minimal>

Just crashing, despite our try/catch, indicating the dlopen or C code is faulting.

Attached are the files you need to reproduce this.

<removed for security reasons>

uNetworkingAB commented 1 day ago

You don't want to be on Windows when debugging things like these. If you swap to Linux (or even just WSL2) you have tons more and better tools to see what is wrong.

But at the same time - never heard of SEA and I know there are many bundlers that don't work with uWS. They often neglect native addons.

uNetworkingAB commented 22 hours ago

Btw, nobody is going to run an .exe attached to your post. I removed it. Please do not attach .exe files here.