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();