@@ -350,32 +350,34 @@ inline void FileHandle::Close() {
350350
351351 AfterClose ();
352352
353- if (ret < 0 ) {
354- // Do not unref this
355- env ()->SetImmediate ([detail](Environment* env) {
353+ // Even though we closed the file descriptor, we still throw an error
354+ // if the FileHandle object was not closed before garbage collection.
355+ // Because this method is called during garbage collection, we will defer
356+ // throwing the error until the next immediate queue tick so as not
357+ // to interfere with the gc process.
358+ //
359+ // This exception will end up being fatal for the process because
360+ // it is being thrown from within the SetImmediate handler and
361+ // there is no JS stack to bubble it to. In other words, tearing
362+ // down the process is the only reasonable thing we can do here.
363+ env ()->SetImmediate ([detail](Environment* env) {
364+ HandleScope handle_scope (env->isolate ());
365+
366+ // If there was an error while trying to close the file descriptor,
367+ // we will throw that instead.
368+ if (detail.ret < 0 ) {
356369 char msg[70 ];
357370 snprintf (msg, arraysize (msg),
358371 " Closing file descriptor %d on garbage collection failed" ,
359372 detail.fd );
360- // This exception will end up being fatal for the process because
361- // it is being thrown from within the SetImmediate handler and
362- // there is no JS stack to bubble it to. In other words, tearing
363- // down the process is the only reasonable thing we can do here.
364373 HandleScope handle_scope (env->isolate ());
365374 env->ThrowUVException (detail.ret , " close" , msg);
366- });
367- return ;
368- }
375+ return ;
376+ }
369377
370- // Even though we closed the file descriptor, we still throw an error
371- // if the FileHandle object was not closed before garbage collection.
372- // Because this method is called during garbage collection, we will defer
373- // throwing the error until the next immediate queue tick so as not
374- // to interfere with the gc process.
375- env ()->SetImmediate ([](Environment* env) {
376378 THROW_ERR_INVALID_STATE (
377379 env,
378- " FileHandle object was not closed before garbage collection. "
380+ " A FileHandle object was closed during garbage collection. "
379381 " This used to be allowed with a deprecation warning but is now "
380382 " considered an error. Please close FileHandle objects explicitly." );
381383 });
0 commit comments