From 55a6bf2c37bd012e3e2d6db57eb0003b67b8129c Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Tue, 26 Jul 2022 23:49:32 +0200 Subject: [PATCH] use fs.readFile instead of setImmediate Signed-off-by: Matteo Collina --- src/index.ts | 3 ++- test/index.ts | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 6e97f5d..c57e363 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import bindings from 'bindings'; import { EventEmitter } from 'events'; import type Module from 'module'; +import fs from 'fs'; const { SynchronousWorkerImpl, UV_RUN_DEFAULT, @@ -123,7 +124,7 @@ class SynchronousWorker extends EventEmitter { async stop(): Promise { return this[kStoppedPromise] ??= new Promise(resolve => { this[kHandle].signalStop(); - setImmediate(() => { + fs.readFile(__filename, () => { this[kHandle].stop(); resolve(); }); diff --git a/test/index.ts b/test/index.ts index 2b486c5..10f8735 100644 --- a/test/index.ts +++ b/test/index.ts @@ -194,4 +194,18 @@ describe('SynchronousWorker allows running Node.js code', () => { }); await w.stop(); }); + + it('properly handles immediates when FreeEnvironment() is called on a shared event loop', async() => { + const w = new SynchronousWorker({ + sharedEventLoop: true, + sharedMicrotaskQueue: true + }); + + setImmediate(() => { + setImmediate(() => { + setImmediate(() => {}); + }); + }); + await w.stop(); + }); });