unjs / listhen

👂 Elegant HTTP Listener
Other
434 stars 29 forks source link

Segmentation fault when running in watch mode in Node 20.12.x or greater #173

Closed joshmossas closed 5 months ago

joshmossas commented 5 months ago

Environment

Software Version
NodeJS v20.12.2
Operating System Pop!_OS 22.04 LTS
Listhen v1.7.2

Reproduction

https://github.com/joshmossas/listhen-watch-segfault

Describe the bug

Running listhen in watch mode causes you to get the following error:

Segmentation fault (core dumped)
 ELIFECYCLE  Command failed with exit code 139.

This is true if you use the cli (listhen ./my-file.ts --watch) or if you call listenAndWatch() directly.

Additional context

Downgrading to NodeJS v20.11.1 causes everything to run normally.

I haven't tried to reproduce on other operating systems. So I can only confirm that this is happening in Linux.

Logs

No response

joshmossas commented 5 months ago

I've identified the cause. For some reason dynamically importing @parcel/watcher in src/server/watcher.ts causes a segfault when you try to use the subscribe() function

https://github.com/unjs/listhen/blob/1e73929e0a1d9ec1ce6dd9b6041f19b3e30dcd78/src/server/watcher.ts#L53

If you import subscribe() from @parcel/watcher normally the segfault doesn't occur anymore.

So the change I made to get things working looks like this:

// top of file
import { subscribe, type AsyncSubscription } from "@parcel/watcher";

// registering subscription (line 55)
watcher = await subscribe(
  devServer.cwd,
  ... rest
)

I'm aware this fix isn't ideal because we it no longer dynamically imports from @parcel/watcher-wasm if using @parcel/watcher fails. But at least it provides a starting point for fixing the issue.

joshmossas commented 5 months ago

Okay I'm not able to reproduce on MacOS so this seems to be Linux specific.

pi0 commented 5 months ago

Hi dear @joshmossas. Can you please try await import("@parcel/watcher") in a simple test.mjs file alone to see if can still reproduce?

Lazy loading of watcher is essential for listhen because we don't need it in all paths. We might track this issue from https://github.com/parcel-bundler/watcher or Node.js seems lower level bug.

joshmossas commented 5 months ago

Okay so it looks like the source of the issue is actually this. https://github.com/parcel-bundler/watcher/issues/170

Listhen is using wildcard ignores by default, which is triggering this bug https://github.com/unjs/listhen/blob/1e73929e0a1d9ec1ce6dd9b6041f19b3e30dcd78/src/server/watcher.ts#L77

You can disregard my comment about lazy loading being the issue. In my fork turns out I accidentally imported @parcel/watcher-wasm which doesn't have this bug. :sweat_smile:

I've also reverted to using lazy loading in my fork (with @parcel/watcher-wasm only) and it works.

pi0 commented 5 months ago

Thanks for checking upstream 🙏🏼 okay let's track from there then. i will also subscribe to see what happens.