@@ -515,12 +515,15 @@ class HooksProxy {
515515 #isWorkerOwner = false ;
516516
517517 constructor ( ) {
518- const { InternalWorker, hooksPort, hasHooksThread } = require ( 'internal/worker' ) ;
518+ const { InternalWorker, hooksPort } = require ( 'internal/worker' ) ;
519519 const lock = new SharedArrayBuffer ( SHARED_MEMORY_BYTE_LENGTH ) ;
520520 this . #lock = new Int32Array ( lock ) ;
521521
522- if ( ! hasHooksThread ( ) ) {
523- // Main thread is the only one that creates the internal single hooks worker
522+ try {
523+ // The customization hooks thread is only created once. All other threads reuse
524+ // the existing instance. If another thread created it, the constructor will throw
525+ // Fallback is to use the existing hooksPort created by the thread that originally
526+ // spawned the customization hooks thread.
524527 this . #worker = new InternalWorker ( loaderWorkerId , {
525528 stderr : false ,
526529 stdin : false ,
@@ -534,16 +537,21 @@ class HooksProxy {
534537 this . #worker. on ( 'exit' , process . exit ) ;
535538 this . #isWorkerOwner = true ;
536539 this . #portToHooksThread = this . #worker;
537- } else {
538- this . #portToHooksThread = hooksPort ;
540+ } catch ( e ) {
541+ if ( e . code === 'ERR_HOOKS_THREAD_EXISTS' ) {
542+ this . #portToHooksThread = hooksPort ;
543+ } else {
544+ throw e ;
545+ }
539546 }
540547 }
541548
542549 waitForWorker ( ) {
543- // There is one Hooks instance for each worker thread. But only one of these Hooks instances
544- // has an InternalWorker. That was the Hooks instance created for the main thread.
545- // It means for all Hooks instances that are not on the main thread => they are ready because they
546- // delegate to the single InternalWorker anyway.
550+ // There is one Hooks instance for each worker thread. But only one of
551+ // these Hooks instances has an InternalWorker. That was the Hooks instance
552+ // created for the first thread that registers a hook set.
553+ // It means for all Hooks instances that are not on that thread => they are
554+ // done because they delegate to the single InternalWorker.
547555 if ( ! this . #isWorkerOwner) {
548556 return ;
549557 }
0 commit comments