Nuxt 2.4 will keep the build hanging with intervals and/or open connections

Timkor
6
Timkor
commented 7 months ago

Version

v2.4.1

Reproduction link

https://codesandbox.io/s/lxwr482vwl

Steps to reproduce

Add a module which sets a interval or opens a connection.
Run npm run build

What is expected ?

That Nuxt.js will finish building (so CI will continue)

What is actually happening?

Build keeps hanging

Additional comments?

Would be nice if Nuxt will close the process after building, as it always has done.

Although intervals are cleared easily, modules will be broken. Connections should be opened and tested on build (in dev mode at least), otherwise it would be quite annoying for module users to wait for finishing building in order to receive errors about incorrect credentials.

It won't be possible to add server middleware at the listen hook. So a module like nuxt-session will not be possible. Because it needs a open connection (to Redis for example) before the middleware is added.

bug
0
pimlie
1.1k
pimlie
commented 7 months ago

Why cant you use a combination of Nuxt hooks to fix your module? Cant you listen for e.g. build:done in your module to cleanup all listener you set? You could probably just call process.exit yourself from a build:done hook if thats what you want

I am having trouble to understand why its a Nuxt issue that the modules you use dont clean up after themselves. Calling process.exit by default is bad practice as it hides bugs and incorrectly written code and Nuxt shouldnt support that imo. Even the Node docs state specifically:

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop.

So just make sure there is no pending work left, which again means probably clean up listeners when you dont need them anymore.

0
Syffs
34
Syffs
commented 7 months ago

@pimlie I'm facing the same issue because of database connections: my connections should be able to run regardless of the build tool.
Hence, build tool should be self-sufficient, and there's no reason why you'd have to handle building logic (using build hooks) within the app logic in such case.

0
pimlie
1.1k
pimlie
commented 7 months ago

@syffs Calling process.exit exits the process as soon as possible, so you put your full trust in your database server to notice the connection isnt active anymore and to release all the resources it reserved for that connection. That means if you dont close your database connection nicely you risk to run out of memory, run out of sockets, out of open ports, you name it. Nuxt will never be able to know why you need to have that database connection and when you are done. But you know, so you close it.

It really comes down to a limitation of js in which it differs from other programming languages. In other languages you would probably use a destructor method in a class for this, but that principle contradicts with gc in js. There are multiple stackoverflow question about this, this is the first one I could find: https://stackoverflow.com/questions/29333017/ecmascript-6-class-destructor

0
mefcorvi
0
mefcorvi
commented 7 months ago

I don't think that it's a good idea to open connections or create timeouts/intervals directly in the module function. You should do that inside proper hooks. Making connections during the build to check credentials is a definitely bad idea. You can't be sure that Redis or any other DB is started when nuxt is building.

If you need to create DB connections, then you probably should use "render:setupMiddleware" or "render:done" hooks, but I would suggest creating a separate back-end service that handles DB connections and sessions, it's much more flexible and scalable solution. NUXT is mostly for UI rendering, it's not a full-stack app platform.

0
Timkor
6
Timkor
commented 7 months ago

@pimlie @mefcorvi Feedback noted. And I must say I agree. Though I think it's still legit to fetch and create initial cache files during build (like content and convert it (markdown to html)), but that doesn't have anything to do with this issue though.

Still, I don't think Nuxt should have minor updates which break modules. This will break deployments automatically.

0
Informations
Bug Report โ€ข Open
#c8603 - Created 7 months ago