Fix broker shutdown hang on unresponsive socket
Summary
Fixes a SessionEnd hang where sendBrokerShutdown could wait indefinitely if the broker accepted the shutdown socket connection but never replied, errored, or closed.
This adds a bounded timeout to the shutdown RPC and uses a single guarded cleanup path for data, error, close, and timeout.
Changes
- Add
timeoutMs = 5000tosendBrokerShutdown - Resolve shutdown on timeout instead of waiting forever
- Destroy the socket during cleanup
- Add a regression test for a broker that accepts
broker/shutdownbut never responds
Testing
node --test tests/broker-lifecycle.test.mjs
npm test
Full suite passed locally: 87 passed, 0 failed.
Fixes #288 .
Confirmed on plugin v1.0.4 (Node 26, macOS). Reproduces as SessionEnd hook ... failed: Hook cancelled on nearly every session exit.
Applied this fix's approach locally — bounded timeout in sendBrokerShutdown that resolves (not rejects) so handleSessionEnd still reaches teardownBrokerSession/clearBrokerSession. Verified against three paths: dead endpoint (~6ms), a broker that connects but never replies (~1s, was previously hanging to the 5s hook kill), and the normal reply path (unaffected).
One minor note: a 1s default bounds the wait well under the 5s hook budget; 5s leaves no cushion before the harness cancels the hook. Either works for the hang itself, but a sub-budget default also fixes the "Hook cancelled" message, not just the indefinite hang.