Stream on 'close' event

344 views
Skip to first unread message

Dominic Tarr

unread,
Jun 1, 2012, 1:54:08 AM6/1/12
to nod...@googlegroups.com
what should a writable stream do if it's underlieing communication layer closes?
I see that .pipe listens on the end event of both source and dest streams,

https://github.com/joyent/node/blob/master/lib/stream.js#L109

what should the WritableStream do if write is called after 'close' is
emitted? emit an 'error'?

how should someone writing to a stream know that the stream is closed?
listen on 'close'? or 'error'? or check stream.writable befor writing?

what is the best way?

cheers, Dominic

Axel Kittenberger

unread,
Jun 1, 2012, 3:57:42 AM6/1/12
to nod...@googlegroups.com
I'm not too much into the node specifics of streams, but I say.

If its an expected end, call close.
If its an unexpected end, call error then close,
if someone calls write after close, throw an error. Since this should
never happen in a correctly written application, if its okay it
terminates if this is not catched.
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en

Dominic Tarr

unread,
Jun 1, 2012, 6:15:33 AM6/1/12
to nod...@googlegroups.com
that sounds very reasonable

I pretty much never call write directly. I always let pipe do that for
me, and pipe cleans up after close, so that would work. and write will
never be called after 'close'.

I've been extremely enthusiastic about the Stream api for nearly a
year now, but have only just realized the purpose of 'close'.

I've written an informal spec up here, based on my understanding of
`Stream` and `pipe`

https://gist.github.com/2850882

I am just trying to document what a user stream must implement in
order to fully work with Steam#pipe, so this is a tad more general
than http://nodejs.org/api/stream.html

comments appreciated.

Mikeal Rogers

unread,
Jun 1, 2012, 2:11:34 PM6/1/12
to nod...@googlegroups.com
FYI, we're refactoring this in 0.9.

If I remember correctly it goes something like.

No more "destroy", "destorySoon" and "close". Only "destroy".

I can't remember if we decided that:
- "error" will always call "destroy"
or
- a stream is guaranteed to eventually emit either "error" or "destroy"

maybe isaac remembers.

it's hard to reason about close right now because it means a few different things on different streams and overlaps with all the destroy stuff.

Dominic Tarr

unread,
Jun 3, 2012, 12:54:38 AM6/3/12
to nod...@googlegroups.com
thanks, I think I've figured out the intent here, will be looking forward to 0.9
Reply all
Reply to author
Forward
0 new messages