esm: do not use 'beforeExit' on the main thread
#47964
+20
−17
Merged