| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b61efcc commit 8cab32a
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -520,14 +520,6 @@ class HooksProxy { | |||
| 520 | 520 | } | |
| 521 | 521 | } | |
| 522 | 522 | ||
| 523 | - #beforeExitHandler = () => { | ||
| 524 | - debug('beforeExit main thread', this.#lock, this.#numberOfPendingAsyncResponses); | ||
| 525 | - if (this.#numberOfPendingAsyncResponses !== 0) { | ||
| 526 | - // The worker still has some work to do, let's wait for it before terminating the process. | ||
| 527 | - this.#worker.ref(); | ||
| 528 | - } | ||
| 529 | - }; | ||
| 530 | - | ||
| 531 | 523 | async makeAsyncRequest(method, ...args) { | |
| 532 | 524 | this.#waitForWorker(); | |
| 533 | 525 | ||
@@ -543,11 +535,9 @@ class HooksProxy { | |||
| 543 | 535 | // come AFTER the last task in the event loop has run its course and there would be nothing | |
| 544 | 536 | // left keeping the thread alive (and once the main thread dies, the whole process stops). | |
| 545 | 537 | // However we want to keep the process alive until the worker thread responds (or until the | |
| 546 | - // event loop of the worker thread is also empty). So we add the beforeExit handler whose | ||
| 547 | - // mission is to lock the main thread until we hear back from the worker thread. The `if` | ||
| 548 | - // condition is there so we only add the event handler once (if there are already pending | ||
| 549 | - // async responses, the previous calls have added the event listener). | ||
| 550 | - process.on('beforeExit', this.#beforeExitHandler); | ||
| 538 | + // event loop of the worker thread is also empty), so we ref the worker until we get all the | ||
| 539 | + // responses back. | ||
| 540 | + this.#worker.ref(); | ||
| 551 | 541 | } | |
| 552 | 542 | ||
| 553 | 543 | let response; | |
@@ -556,16 +546,13 @@ class HooksProxy { | |||
| 556 | 546 | await AtomicsWaitAsync(this.#lock, WORKER_TO_MAIN_THREAD_NOTIFICATION, this.#workerNotificationLastId).value; | |
| 557 | 547 | this.#workerNotificationLastId = AtomicsLoad(this.#lock, WORKER_TO_MAIN_THREAD_NOTIFICATION); | |
| 558 | 548 | ||
| 559 | - // In case the beforeExit handler was called during the await, we revert its actions. | ||
| 560 | - this.#worker.unref(); | ||
| 561 | - | ||
| 562 | 549 | response = receiveMessageOnPort(asyncCommChannel.port1); | |
| 563 | 550 | } while (response == null); | |
| 564 | 551 | debug('got async response from worker', { method, args }, this.#lock); | |
| 565 | 552 | ||
| 566 | 553 | if (--this.#numberOfPendingAsyncResponses === 0) { | |
| 567 | 554 | // We got all the responses from the worker, its job is done (until next time). | |
| 568 | - process.off('beforeExit', this.#beforeExitHandler); | ||
| 555 | + this.#worker.unref(); | ||
| 569 | 556 | } | |
| 570 | 557 | ||
| 571 | 558 | const body = this.#unwrapMessage(response); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -191,4 +191,20 @@ describe('Loader hooks', { concurrency: true }, () => { | |||
| 191 | 191 | assert.strictEqual(code, 42); | |
| 192 | 192 | assert.strictEqual(signal, null); | |
| 193 | 193 | }); | |
| 194 | + | ||
| 195 | + it('should be fine to call `process.removeAllListeners("beforeExit")` from the main thread', async () => { | ||
| 196 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ | ||
| 197 | + '--no-warnings', | ||
| 198 | + '--experimental-loader', | ||
| 199 | + 'data:text/javascript,export function load(a,b,c){return new Promise(d=>setTimeout(()=>d(c(a,b)),99))}', | ||
| 200 | + '--input-type=module', | ||
| 201 | + '--eval', | ||
| 202 | + 'setInterval(() => process.removeAllListeners("beforeExit"),1).unref();await import("data:text/javascript,")', | ||
| 203 | + ]); | ||
| 204 | + | ||
| 205 | + assert.strictEqual(stderr, ''); | ||
| 206 | + assert.strictEqual(stdout, ''); | ||
| 207 | + assert.strictEqual(code, 0); | ||
| 208 | + assert.strictEqual(signal, null); | ||
| 209 | + }); | ||
| 194 | 210 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments