|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/** |
| 4 | + * Regression tests for commit ebd629ef ("Silence cluster worker events to |
| 5 | + * prevent boot crashes"). |
| 6 | + * |
| 7 | + * That commit rewrote IPCTransport.send() so that every broken-channel |
| 8 | + * failure path is silently swallowed: |
| 9 | + * |
| 10 | + * - process.connected === false -> `return -1` (was: console.error + exit) |
| 11 | + * - process.send(msg, cb) async -> debug logger only (was: synchronous send) |
| 12 | + * - process.send() throws -> debug logger only (was: re-thrown / exit) |
| 13 | + * |
| 14 | + * Result: when the IPC pipe to the PM2 daemon breaks, BPM keeps emitting |
| 15 | + * metrics into the void with zero observable signal. This is the mechanism |
| 16 | + * that aggravates the unbounded-memory growth (#6101) and masks the EBADF |
| 17 | + * channel fault into a confusing silent symptom (#6111). |
| 18 | + * |
| 19 | + * Contract under test: IPCTransport is an EventEmitter. A send failure on a |
| 20 | + * broken channel MUST be surfaced to observers via an 'error' event — not |
| 21 | + * swallowed, and not by killing the whole process (process.exit was itself |
| 22 | + * the boot-crash the commit tried to fix). These tests are expected to FAIL |
| 23 | + * against the current implementation (TDD). |
| 24 | + */ |
| 25 | + |
| 26 | +const assert = require('assert') |
| 27 | +const { IPCTransport } = require('../../transports/IPCTransport') |
| 28 | +const { IPC_MAX_INFLIGHT } = require('../../constants') |
| 29 | + |
| 30 | +describe('IPCTransport — broken channel must surface, not swallow (regression #6101/#6111)', function () { |
| 31 | + let originalSend |
| 32 | + let connectedDescriptor |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + originalSend = process.send |
| 36 | + connectedDescriptor = Object.getOwnPropertyDescriptor(process, 'connected') |
| 37 | + }) |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + process.send = originalSend |
| 41 | + if (connectedDescriptor) { |
| 42 | + Object.defineProperty(process, 'connected', connectedDescriptor) |
| 43 | + } else { |
| 44 | + delete process.connected |
| 45 | + } |
| 46 | + }) |
| 47 | + |
| 48 | + const stubConnected = (value) => { |
| 49 | + Object.defineProperty(process, 'connected', { |
| 50 | + value, |
| 51 | + configurable: true, |
| 52 | + writable: true |
| 53 | + }) |
| 54 | + } |
| 55 | + |
| 56 | + it('emits an "error" event when the async process.send callback fails', function () { |
| 57 | + stubConnected(true) |
| 58 | + // Synchronous callback so the test owns no async work past its lifetime. |
| 59 | + process.send = function (_msg, cb) { |
| 60 | + cb(new Error('channel closed (EPIPE)')) |
| 61 | + } |
| 62 | + |
| 63 | + const transport = new IPCTransport() |
| 64 | + let surfaced = null |
| 65 | + transport.on('error', (err) => { surfaced = err }) |
| 66 | + |
| 67 | + transport.send('axm:monitor', { cpu: 1 }) |
| 68 | + |
| 69 | + // Current code only calls this.logger(...) in the callback — no 'error' |
| 70 | + // event is ever emitted, so `surfaced` stays null and this fails. |
| 71 | + assert.ok(surfaced instanceof Error, |
| 72 | + 'async send failure must be surfaced via an "error" event, not swallowed') |
| 73 | + }) |
| 74 | + |
| 75 | + it('emits an "error" event when the channel is disconnected instead of silently dropping', function () { |
| 76 | + stubConnected(false) |
| 77 | + process.send = function () { /* present but channel is dead */ } |
| 78 | + |
| 79 | + const transport = new IPCTransport() |
| 80 | + let surfaced = null |
| 81 | + transport.on('error', (err) => { surfaced = err }) |
| 82 | + |
| 83 | + transport.send('axm:monitor', { cpu: 1 }) |
| 84 | + |
| 85 | + // The contract is observability — a disconnected channel must surface an |
| 86 | + // 'error' event. The -1 return value is an intentionally retained sentinel |
| 87 | + // (callers like setOptions() depend on it); it is not asserted here. |
| 88 | + assert.ok(surfaced instanceof Error, |
| 89 | + 'disconnected channel must be observable via an "error" event') |
| 90 | + }) |
| 91 | + |
| 92 | + it('emits an "error" event when process.send throws synchronously', function () { |
| 93 | + stubConnected(true) |
| 94 | + process.send = function () { |
| 95 | + throw new Error('Channel closed') |
| 96 | + } |
| 97 | + |
| 98 | + const transport = new IPCTransport() |
| 99 | + let surfaced = null |
| 100 | + transport.on('error', (err) => { surfaced = err }) |
| 101 | + |
| 102 | + transport.send('axm:monitor', { cpu: 1 }) |
| 103 | + |
| 104 | + // Current code swallows the throw into this.logger(...) only. |
| 105 | + assert.ok(surfaced instanceof Error, |
| 106 | + 'thrown send error must be surfaced via an "error" event, not swallowed') |
| 107 | + }) |
| 108 | + |
| 109 | + it('bounds in-flight sends under back-pressure instead of growing unbounded (#6101)', function () { |
| 110 | + stubConnected(true) |
| 111 | + let sendCalls = 0 |
| 112 | + // Saturated-but-connected pipe: process.send accepts the message but the |
| 113 | + // ack callback never fires (libuv write buffer full). The retained |
| 114 | + // callbacks must NOT accumulate without bound. |
| 115 | + process.send = function () { sendCalls++ } |
| 116 | + |
| 117 | + const transport = new IPCTransport() |
| 118 | + const surfaced = [] |
| 119 | + transport.on('error', (err) => { surfaced.push(err) }) |
| 120 | + |
| 121 | + const attempts = IPC_MAX_INFLIGHT + 5 |
| 122 | + for (let i = 0; i < attempts; i++) { |
| 123 | + transport.send('axm:monitor', { i }) |
| 124 | + } |
| 125 | + |
| 126 | + assert.strictEqual(sendCalls, IPC_MAX_INFLIGHT, |
| 127 | + `at most ${IPC_MAX_INFLIGHT} messages may be in flight; excess must be dropped, not queued`) |
| 128 | + assert.strictEqual(transport._inflight, IPC_MAX_INFLIGHT, |
| 129 | + 'in-flight counter must be capped at IPC_MAX_INFLIGHT') |
| 130 | + assert.strictEqual(transport._dropped, attempts - IPC_MAX_INFLIGHT, |
| 131 | + 'every over-cap message must be counted as dropped') |
| 132 | + assert.strictEqual(surfaced.length, 1, |
| 133 | + 'back-pressure must be surfaced exactly once (edge-triggered), not per dropped message') |
| 134 | + assert.ok(surfaced[0] instanceof Error, |
| 135 | + 'back-pressure must be surfaced as an Error') |
| 136 | + }) |
| 137 | +}) |
0 commit comments