Repository navigation
'close' event called before 'end' in paused stream #19166
Description
Activity
endevent triggers at the ending of the readable side of the socket execute completely.
so, it's not necessary that trigger ofcloseandendevents should be sequential.
@DiegoTUI@rohanraj7316 but according to the docs, once the
closeevent is emitted, no more events are emitted, right? It seems that in this case, theendevent is emitted after thecloseevent contradicting the docs.Reacted by almosnowcc @nodejs/streams
can you share the link pls @DiegoTUI
@rohanraj7316 you mean the link to the docs? Here it is:
https://nodejs.org/dist/latest-v9.x/docs/api/stream.html#stream_event_close_1Since we have multiple phases in the Node js event loop.
Close event is not behaving in the way you are
expecting it to be because of the absence of stream callback.When we say event loop there are multiple phases of Event Loop cycle
all the callback corresponding to given event are present in that phase.
but since you are adding the listener to the net modules object it is acting
according to the net modulescloseandendcallbacks. I guess this can
be one of the possible reason. for more details regarding the behavior of
event you can go through this link
@DiegoTUI@rohanraj7316 If I understood correctly, you are suggesting that the fact that the
endandcloseevents come in the wrong order could be due to the fact that I am listening to theendandcloseevents of a net.Socket, and not of a "pure" stream. It doesn't make a lot of sense to me because a Socket is a Duplex stream, and it seems that it relies in the stream's event to produce his own events. The docs of the Socket close event are also pretty clear about this:Emitted once the socket is fully closed. The argument had_error is a boolean which says if the socket was closed due to a transmission error.
If the
closeevent is emitted, then the socket is fully closed. It doesn't make any sense that theendevent is emitted after the socket is fully closed.I think this is a bug. It also goes back to Node 4 at least.
Reacted by Diego Lafuente, Alex Fernández and Artur Klesun- addedstreamIssues and PRs related to Node.js streams.Issues and PRs related to Node.js streams.confirmed-bugIssues and PRs for confirmed bugs.Issues and PRs for confirmed bugs.
on Mar 7, 2018 - added a commit that references this issue
on Jul 27, 2026
I believe I've found an akward behaviour of a stream in paused mode using the
netmodule. In some specific cases, when the source socket ends, thecloseevent is called before theendevent in the destination socket. I've been able to reproduce it simulating the callback queue with nested setImmediates.Server
I've tried with two clients.
Client 1 (bash)
$ echo EVENT | nc localhost 1100Client 2 (node)
It consistently "fails" with both, "fail" meaning that the
closeevent is called before theendevent. According to the documentation this should never happen.NOTE: The number of
setImmediates that you need to nest in order to make it "fail" may vary depending on your environment.