| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a6146c7 commit c9a4603
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,12 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + // Handle a Promise from running code that potentially does Top-Level Await. | ||
| 4 | + // In that case, it makes sense to set the exit code to a specific non-zero | ||
| 5 | + // value if the main code never finishes running. | ||
| 6 | + function handleProcessExit() { | ||
| 7 | + process.exitCode ??= 13; | ||
| 8 | + } | ||
| 9 | + | ||
| 10 | + module.exports = { | ||
| 11 | + handleProcessExit, | ||
| 12 | + }; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,9 @@ const CJSLoader = require('internal/modules/cjs/loader'); | |||
| 8 | 8 | const { Module, toRealPath, readPackageScope } = CJSLoader; | |
| 9 | 9 | const { getOptionValue } = require('internal/options'); | |
| 10 | 10 | const path = require('path'); | |
| 11 | + const { | ||
| 12 | + handleProcessExit, | ||
| 13 | + } = require('internal/modules/esm/handle_process_exit'); | ||
| 11 | 14 | ||
| 12 | 15 | function resolveMainPath(main) { | |
| 13 | 16 | // Note extension resolution for the main entry point can be deprecated in a | |
@@ -53,18 +56,11 @@ function runMainESM(mainPath) { | |||
| 53 | 56 | } | |
| 54 | 57 | ||
| 55 | 58 | async function handleMainPromise(promise) { | |
| 56 | - // Handle a Promise from running code that potentially does Top-Level Await. | ||
| 57 | - // In that case, it makes sense to set the exit code to a specific non-zero | ||
| 58 | - // value if the main code never finishes running. | ||
| 59 | - function handler() { | ||
| 60 | - if (process.exitCode === undefined) | ||
| 61 | - process.exitCode = 13; | ||
| 62 | - } | ||
| 63 | - process.on('exit', handler); | ||
| 59 | + process.on('exit', handleProcessExit); | ||
| 64 | 60 | try { | |
| 65 | 61 | return await promise; | |
| 66 | 62 | } finally { | |
| 67 | - process.off('exit', handler); | ||
| 63 | + process.off('exit', handleProcessExit); | ||
| 68 | 64 | } | |
| 69 | 65 | } | |
| 70 | 66 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -48,6 +48,10 @@ const { | |||
| 48 | 48 | } = require('internal/validators'); | |
| 49 | 49 | const constants = internalBinding('constants').os.signals; | |
| 50 | 50 | ||
| 51 | + const { | ||
| 52 | + handleProcessExit, | ||
| 53 | + } = require('internal/modules/esm/handle_process_exit'); | ||
| 54 | + | ||
| 51 | 55 | const kInternal = Symbol('internal properties'); | |
| 52 | 56 | ||
| 53 | 57 | function assert(x, msg) { | |
@@ -175,6 +179,8 @@ function wrapProcessMethods(binding) { | |||
| 175 | 179 | memoryUsage.rss = rss; | |
| 176 | 180 | ||
| 177 | 181 | function exit(code) { | |
| 182 | + process.off('exit', handleProcessExit); | ||
| 183 | + | ||
| 178 | 184 | if (code || code === 0) | |
| 179 | 185 | process.exitCode = code; | |
| 180 | 186 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -80,3 +80,21 @@ import fixtures from '../common/fixtures.js'; | |||
| 80 | 80 | assert.deepStrictEqual([status, stdout], [1, '']); | |
| 81 | 81 | assert.match(stderr, /Error: Xyz/); | |
| 82 | 82 | } | |
| 83 | + | ||
| 84 | + { | ||
| 85 | + // Calling process.exit() in .mjs should return status 0 | ||
| 86 | + const { status, stdout, stderr } = child_process.spawnSync( | ||
| 87 | + process.execPath, | ||
| 88 | + [fixtures.path('es-modules/tla/process-exit.mjs')], | ||
| 89 | + { encoding: 'utf8' }); | ||
| 90 | + assert.deepStrictEqual([status, stdout, stderr], [0, '', '']); | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + { | ||
| 94 | + // Calling process.exit() in worker thread shouldn't influence main thread | ||
| 95 | + const { status, stdout, stderr } = child_process.spawnSync( | ||
| 96 | + process.execPath, | ||
| 97 | + [fixtures.path('es-modules/tla/unresolved-with-worker-process-exit.mjs')], | ||
| 98 | + { encoding: 'utf8' }); | ||
| 99 | + assert.deepStrictEqual([status, stdout, stderr], [13, '', '']); | ||
| 100 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + process.exit(); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,8 @@ | |||
| 1 | + import { Worker, isMainThread } from 'worker_threads'; | ||
| 2 | + | ||
| 3 | + if (isMainThread) { | ||
| 4 | + new Worker(new URL(import.meta.url)); | ||
| 5 | + await new Promise(() => {}); | ||
| 6 | + } else { | ||
| 7 | + process.exit(); | ||
| 8 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -79,6 +79,7 @@ const expectedModules = new Set([ | |||
| 79 | 79 | 'NativeModule internal/modules/esm/resolve', | |
| 80 | 80 | 'NativeModule internal/modules/esm/initialize_import_meta', | |
| 81 | 81 | 'NativeModule internal/modules/esm/translators', | |
| 82 | + 'NativeModule internal/modules/esm/handle_process_exit', | ||
| 82 | 83 | 'NativeModule internal/process/esm_loader', | |
| 83 | 84 | 'NativeModule internal/options', | |
| 84 | 85 | 'NativeModule internal/perf/event_loop_delay', | |
| Back | FazBrowse Home | New Git URL |
0 commit comments