From 247dfb7d73e134284c1ed8734c6e11772d5f14d3 Mon Sep 17 00:00:00 2001 From: Ramana Venkata Date: Sat, 25 Feb 2023 15:52:05 +0530 Subject: [PATCH] dgram: fix unhandled exception aborting a closed udp socket Fixes: https://github.com/nodejs/node/issues/46750 PR-URL: https://github.com/nodejs/node/pull/46770 Reviewed-By: Matteo Collina Reviewed-By: Colin Ihrig Reviewed-By: Paolo Insogna Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell --- lib/dgram.js | 2 +- test/parallel/test-dgram-abort-closed.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-dgram-abort-closed.js diff --git a/lib/dgram.js b/lib/dgram.js index 4d87299046d52b..854fc238066d5b 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -139,7 +139,7 @@ function Socket(type, listener) { const { signal } = options; validateAbortSignal(signal, 'options.signal'); const onAborted = () => { - this.close(); + if (this[kStateSymbol].handle) this.close(); }; if (signal.aborted) { onAborted(); diff --git a/test/parallel/test-dgram-abort-closed.js b/test/parallel/test-dgram-abort-closed.js new file mode 100644 index 00000000000000..6346b5feae5439 --- /dev/null +++ b/test/parallel/test-dgram-abort-closed.js @@ -0,0 +1,10 @@ +'use strict'; +require('../common'); +const dgram = require('dgram'); + +const controller = new AbortController(); +const socket = dgram.createSocket({ type: 'udp4', signal: controller.signal }); + +socket.close(); + +controller.abort();