@@ -211,11 +211,12 @@ var LibraryPThread = {
211211 // worker pool as an unused worker.
212212 worker . pthread_ptr = 0 ;
213213
214- #if ENVIRONMENT_MAY_BE_NODE
214+ #if ENVIRONMENT_MAY_BE_NODE && PROXY_TO_PTHREAD
215215 if ( ENVIRONMENT_IS_NODE ) {
216- // Once a pthread has finished and the worker becomes idle, mark it
217- // as weakly referenced so that its existence does not prevent Node.js
218- // from exiting.
216+ // Once the proxied main thread has finished, mark it as weakly
217+ // referenced so that its existence does not prevent Node.js from
218+ // exiting. This has no effect if the worker is already weakly
219+ // referenced.
219220 worker . unref ( ) ;
220221 }
221222#endif
@@ -286,7 +287,7 @@ var LibraryPThread = {
286287 cancelThread ( d [ 'thread' ] ) ;
287288 } else if ( cmd === 'loaded' ) {
288289 worker . loaded = true ;
289- #if ENVIRONMENT_MAY_BE_NODE
290+ #if ENVIRONMENT_MAY_BE_NODE && PTHREAD_POOL_SIZE
290291 // Check that this worker doesn't have an associated pthread.
291292 if ( ENVIRONMENT_IS_NODE && ! worker . pthread_ptr ) {
292293 // Once worker is loaded & idle, mark it as weakly referenced,
@@ -571,6 +572,19 @@ var LibraryPThread = {
571572 else postMessage ( { 'cmd' : 'cleanupThread' , 'thread' : thread } ) ;
572573 } ,
573574
575+ _emscripten_thread_set_strongref : function ( thread ) {
576+ // Called when a thread needs to be strongly referenced.
577+ // Currently only used for:
578+ // - keeping the "main" thread alive in PROXY_TO_PTHREAD mode;
579+ // - crashed threads that needs to propagate the uncaught exception
580+ // back to the main thread.
581+ #if ENVIRONMENT_MAY_BE_NODE
582+ if ( ENVIRONMENT_IS_NODE ) {
583+ PThread . pthreads [ thread ] . ref ( ) ;
584+ }
585+ #endif
586+ } ,
587+
574588 $cleanupThread : function ( pthread_ptr ) {
575589#if PTHREADS_DEBUG
576590 dbg ( 'cleanupThread: ' + ptrToString ( pthread_ptr ) )
@@ -674,14 +688,16 @@ var LibraryPThread = {
674688 msg . moduleCanvasId = threadParams . moduleCanvasId ;
675689 msg . offscreenCanvases = threadParams . offscreenCanvases ;
676690#endif
677- // Ask the worker to start executing its pthread entry point function.
678691#if ENVIRONMENT_MAY_BE_NODE
679692 if ( ENVIRONMENT_IS_NODE ) {
680- // Mark worker as strongly referenced once we start executing a pthread,
681- // so that Node.js doesn't exit while the pthread is running.
682- worker . ref ( ) ;
693+ // Mark worker as weakly referenced once we start executing a pthread,
694+ // so that its existence does not prevent Node.js from exiting. This
695+ // has no effect if the worker is already weakly referenced (e.g. if
696+ // this worker was previously idle/unused).
697+ worker . unref ( ) ;
683698 }
684699#endif
700+ // Ask the worker to start executing its pthread entry point function.
685701 worker . postMessage ( msg , threadParams . transferList ) ;
686702 return 0 ;
687703 } ,
0 commit comments