Sitelet https://github.com/nodejs/node/issues/47929
Skip to content

node 20.1.0 --loader seems trigger beforeExit when use await import #47929

Description

@wacdev

Version

20.1.0

Platform

macos

What do you see instead?

My module will close the redis connection at the time of beforeExit, and the await import module (19.9.0) will not trigger beforeExit before.

In addition, the exit event is not triggered when use await import module

Activity

  1. added
    confirmed-bugIssues and PRs for confirmed bugs.
    loadersIssues and PRs related to ES module loaders.
    on May 9, 2023
  2. MoLow commented on May 9, 2023

    @MoLow
    Member

    also reported here - with a minimal repro

    // file.js
    process.on('beforeExit', () => {
      console.log('beforeExit')
    })
    import('./anyfile.js')
    console.log('THIS CONSOLE LOG IS PREINTED')
    ./node  file.js                                                                                              ✔
    THIS CONSOLE LOG IS PREINTED
    beforeExit
    ./node --experimental-loader="data:text/javascript,export default true"  file.js                             ✔
    (node:62415) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time
    (Use `node --trace-warnings ...` to show where the warning was created)
    THIS CONSOLE LOG IS PREINTED
    beforeExit
    beforeExit
    beforeExit
  3. targos commented on May 10, 2023

    @targos
    Member

    This is probably because the ESM loader uses process.on('beforeExit') in a way that asynchronous work is scheduled in the callback. We should probably refactor that part anyway, I don't like that the internals use a public API that can be tampered with (for example with process.removeAllListeners('beforeExit');)

    Refs: https://nodejs.org/dist/latest-v20.x/docs/api/process.html#event-beforeexit

    /cc @aduh95

  4. aduh95 commented on May 10, 2023

    @aduh95
    Contributor

    The underlying issue being that Atomics.waitAsync does not keep the event loop alive, which has been reported to them already. That being said, we should be able to work around this without triggering beforeExit events on the main thread (on the loader thread though, I don’t think we can work around it, and calling process.removeAllListeners('beforeExit') from the loader thread needs to be forbidden probably.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues and PRs for confirmed bugs.loadersIssues and PRs related to ES module loaders.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions