ERR_USE_AFTER_CLOSE
node
streams
ai_generated
true
Error [ERR_USE_AFTER_CLOSE]: This socket has been ended by the other party
ID: node/err-use-after-close
91%Fix Rate
93%Confidence
50Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 20 | active | — | — | — |
Root Cause
Attempting to write to a socket or stream that has already been closed.
genericWorkarounds
-
90% success Check socket.destroyed before writing
if (!socket.destroyed) socket.write(data)
-
93% success Handle the 'close' and 'end' events to stop writing
socket.on('close', () => { /* stop sending */ })
Dead Ends
Common approaches that don't work:
-
Wrapping every write in try/catch
65% fail
Masks the lifecycle management bug
-
Keeping a global reference to prevent garbage collection
80% fail
Socket is closed by the remote; keeping a reference doesn't reopen it
Error Chain
Frequently confused with: