Skip to content

Fix: throws in user event handlers are caught and re-emitted as handler-error#850

Merged
cressie176 merged 3 commits into
mainfrom
fix/close-handler-error-swallowed
May 8, 2026
Merged

Fix: throws in user event handlers are caught and re-emitted as handler-error#850
cressie176 merged 3 commits into
mainfrom
fix/close-handler-error-swallowed

Conversation

@cressie176

Copy link
Copy Markdown
Collaborator

Throws in user-supplied event handlers on Connection and Channel are now caught and re-emitted as a handler-error event (with the original error and event name as arguments), rather than propagating as uncaught exceptions or being silently swallowed.

If no handler-error listener is registered, the original behaviour is preserved — the error is rethrown.

Closes #334.

cressie176 and others added 3 commits May 7, 2026 20:46
…er-error (#334)

Wraps all user-facing emit() calls on both Connection and Channel with
safeEmit(). If a handler throws and a handler-error listener is registered,
the error is delivered there via setImmediate (preventing re-entry into
amqplib internals). If no handler-error listener is registered, behaviour
is unchanged.

Previously, throws in channel event handlers (ack, nack, cancel, error,
close) would propagate through acceptLoop and kill the connection. Throws
in connection close handlers were silently swallowed.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…t in README

handler-error listeners now receive (err, event) so the listener knows
which event handler threw. Update README with dedicated handler-error
section, error handler examples, and clearer log messages. Update
CHANGELOG to reflect the new signature.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Apply safeEmit to the three remaining bare this.emit('error') calls in
Connection: closeWithError, onSocketError, and startHeartbeater. Adds
tests for all three paths in both the without and with handler-error
listener describe blocks.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@cressie176
cressie176 merged commit c11bd07 into main May 8, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

connection.close causes process to die and error is uncatchable

1 participant