|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import {EventEmitter} from 'node:events'; |
| 9 | +import type * as Process from 'node:process'; |
| 10 | +import { |
| 11 | + injectGlobalErrorHandlers, |
| 12 | + restoreGlobalErrorHandlers, |
| 13 | +} from '../globalErrorHandlers'; |
| 14 | + |
| 15 | +const errorEvents = [ |
| 16 | + 'uncaughtException', |
| 17 | + 'unhandledRejection', |
| 18 | + 'rejectionHandled', |
| 19 | +] as const; |
| 20 | + |
| 21 | +// Circus runs inside the VM, where the ambient `process` is the sandbox copy |
| 22 | +// made by `createProcessObject` — a different emitter from the `parentProcess` |
| 23 | +// the handlers are installed on. A plain emitter stands in for the parent. |
| 24 | +const makeParentProcess = () => { |
| 25 | + const emitter = new EventEmitter(); |
| 26 | + return {emitter, parentProcess: emitter as unknown as typeof Process}; |
| 27 | +}; |
| 28 | + |
| 29 | +describe('injectGlobalErrorHandlers', () => { |
| 30 | + test('snapshots the listeners of the parent process, not the ambient one', () => { |
| 31 | + const {emitter, parentProcess} = makeParentProcess(); |
| 32 | + const parentListener = () => {}; |
| 33 | + for (const event of errorEvents) { |
| 34 | + emitter.on(event, parentListener); |
| 35 | + } |
| 36 | + |
| 37 | + const originalHandlers = injectGlobalErrorHandlers(parentProcess); |
| 38 | + |
| 39 | + expect(originalHandlers.uncaughtException).toEqual([parentListener]); |
| 40 | + expect(originalHandlers.unhandledRejection).toEqual([parentListener]); |
| 41 | + expect(originalHandlers.rejectionHandled).toEqual([parentListener]); |
| 42 | + |
| 43 | + for (const event of errorEvents) { |
| 44 | + expect(emitter.listeners(event)).not.toContain(parentListener); |
| 45 | + expect(emitter.listeners(event)).toHaveLength(1); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + test('restores the parent process listeners it replaced', () => { |
| 50 | + const {emitter, parentProcess} = makeParentProcess(); |
| 51 | + const parentListener = () => {}; |
| 52 | + for (const event of errorEvents) { |
| 53 | + emitter.on(event, parentListener); |
| 54 | + } |
| 55 | + |
| 56 | + restoreGlobalErrorHandlers( |
| 57 | + parentProcess, |
| 58 | + injectGlobalErrorHandlers(parentProcess), |
| 59 | + ); |
| 60 | + |
| 61 | + for (const event of errorEvents) { |
| 62 | + expect(emitter.listeners(event)).toEqual([parentListener]); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + test('keeps a restored `once` listener one-shot', () => { |
| 67 | + const {emitter, parentProcess} = makeParentProcess(); |
| 68 | + let calls = 0; |
| 69 | + emitter.once('uncaughtException', () => { |
| 70 | + calls++; |
| 71 | + }); |
| 72 | + |
| 73 | + restoreGlobalErrorHandlers( |
| 74 | + parentProcess, |
| 75 | + injectGlobalErrorHandlers(parentProcess), |
| 76 | + ); |
| 77 | + |
| 78 | + emitter.emit('uncaughtException', new Error('first')); |
| 79 | + emitter.emit('uncaughtException', new Error('second')); |
| 80 | + |
| 81 | + expect(calls).toBe(1); |
| 82 | + expect(emitter.listeners('uncaughtException')).toHaveLength(0); |
| 83 | + }); |
| 84 | + |
| 85 | + test('does not leak listeners added inside the sandbox onto the parent process', () => { |
| 86 | + const {emitter, parentProcess} = makeParentProcess(); |
| 87 | + const originalHandlers = injectGlobalErrorHandlers(parentProcess); |
| 88 | + |
| 89 | + // A `setupFiles` script registering on the sandbox `process` must not be |
| 90 | + // restored onto the parent. |
| 91 | + const sandboxListener = () => {}; |
| 92 | + for (const event of errorEvents) { |
| 93 | + process.on(event, sandboxListener); |
| 94 | + } |
| 95 | + |
| 96 | + try { |
| 97 | + restoreGlobalErrorHandlers(parentProcess, originalHandlers); |
| 98 | + |
| 99 | + for (const event of errorEvents) { |
| 100 | + expect(emitter.listeners(event)).toHaveLength(0); |
| 101 | + } |
| 102 | + } finally { |
| 103 | + for (const event of errorEvents) { |
| 104 | + process.removeListener(event, sandboxListener); |
| 105 | + } |
| 106 | + } |
| 107 | + }); |
| 108 | +}); |
0 commit comments