0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/doc
isaacs b0de1e4a41 stream: Fix unshift() race conditions
Fix #5272

The consumption of a readable stream is a dance with 3 partners.

1. The specific stream Author (A)
2. The Stream Base class (B), and
3. The Consumer of the stream (C)

When B calls the _read() method that A implements, it sets a 'reading'
flag, so that parallel calls to _read() can be avoided.  When A calls
stream.push(), B knows that it's safe to start calling _read() again.

If the consumer C is some kind of parser that wants in some cases to
pass the source stream off to some other party, but not before "putting
back" some bit of previously consumed data (as in the case of Node's
websocket http upgrade implementation).  So, stream.unshift() will
generally *never* be called by A, but *only* called by C.

Prior to this patch, stream.unshift() *also* unset the state.reading
flag, meaning that C could indicate the end of a read, and B would
dutifully fire off another _read() call to A.  This is inappropriate.
In the case of fs streams, and other variably-laggy streams that don't
tolerate overlapped _read() calls, this causes big problems.

Also, calling stream.shift() after the 'end' event did not raise any
kind of error, but would cause very strange behavior indeed.  Calling it
after the EOF chunk was seen, but before the 'end' event was fired would
also cause weird behavior, and could lead to data being lost, since it
would not emit another 'readable' event.

This change makes it so that:

1. stream.unshift() does *not* set state.reading = false
2. stream.unshift() is allowed up until the 'end' event.
3. unshifting onto a EOF-encountered and zero-length (but not yet
end-emitted) stream will defer the 'end' event until the new data is
consumed.
4. pushing onto a EOF-encountered stream is now an error.

So, if you read(), you have that single tick to safely unshift() data
back into the stream, even if the null chunk was pushed, and the length
was 0.
2013-04-11 16:12:48 -07:00
..
about doc: move ul styling to pipe.css 2012-08-23 17:54:23 +02:00
api stream: Fix unshift() race conditions 2013-04-11 16:12:48 -07:00
api_assets Colorize API stabilitity index headers in docs 2012-12-27 18:38:56 -08:00
blog blog: Fix title for v0.8.23 release 2013-04-11 11:06:20 -07:00
community doc: add Google+ French community 2012-12-17 17:21:35 +01:00
download Fix dropped html tag 2012-11-30 18:14:26 -08:00
images website: Sharpen and clean up download page more 2012-08-15 15:52:57 -07:00
logos doc: move ul styling to pipe.css 2012-08-23 17:54:23 +02:00
blog.html doc: fix broken links in blog footer 2013-03-18 14:41:51 +01:00
changelog-foot.html website: download page 2012-08-15 09:09:23 -07:00
changelog-head.html website: download page 2012-08-15 09:09:23 -07:00
cla.html doc: Make the CLA more simple/sane 2012-09-27 13:25:55 -07:00
favicon.ico
full-white-stripe.jpg Use .jpg instead of .bmp for .msi 2012-01-10 14:26:22 -08:00
index.html doc: update instructions on home page 2013-02-28 16:30:46 +01:00
mac_osx_nodejs_installer_logo.png
node.1 node: Add --throw-deprecation 2013-03-06 11:44:29 -08:00
pipe.css doc: more uniform and sleek buttons 2012-08-24 15:47:53 +02:00
robots.txt robots.txt 2012-02-14 14:34:43 -08:00
rss.xml blog: Generate RSS feeds 2012-06-22 08:49:20 -07:00
sh_javascript.min.js
sh_main.js
sh_vim-dark.css doc: use somewhat consistent styling for css assets 2012-08-23 17:54:12 +02:00
sh.css doc: use somewhat consistent styling for css assets 2012-08-23 17:54:12 +02:00
template.html website: download page 2012-08-15 09:09:23 -07:00
thin-white-stripe.jpg Use .jpg instead of .bmp for .msi 2012-01-10 14:26:22 -08:00
trademark-policy.pdf
v0.4_announcement.html s/NPM/npm/ 2011-12-05 16:20:44 -08:00