Title: Readable._read is called multiple times synchronously · Issue #24919 · nodejs/node · GitHub
Open Graph Title: Readable._read is called multiple times synchronously · Issue #24919 · nodejs/node
X Title: Readable._read is called multiple times synchronously · Issue #24919 · nodejs/node
Description: Version: v10.10.0, v11.4.0 Platform: Linux 4.4.0-45-generic #66-Ubuntu SMP Wed Oct 19 14:12:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux Subsystem: stream The documentation for readable._read(size) states the following: readable._read() is...
Open Graph Description: Version: v10.10.0, v11.4.0 Platform: Linux 4.4.0-45-generic #66-Ubuntu SMP Wed Oct 19 14:12:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux Subsystem: stream The documentation for readable._read(size) s...
X Description: Version: v10.10.0, v11.4.0 Platform: Linux 4.4.0-45-generic #66-Ubuntu SMP Wed Oct 19 14:12:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux Subsystem: stream The documentation for readable._read(size) s...
Opengraph URL: https://github.com/nodejs/node/issues/24919
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Readable._read is called multiple times synchronously","articleBody":"\u003c!--\r\nThank you for reporting a possible bug in Node.js.\r\n\r\nPlease fill in as much of the template below as you can.\r\n\r\nVersion: output of `node -v`\r\nPlatform: output of `uname -a` (UNIX), or version and 32 or 64-bit (Windows)\r\nSubsystem: if known, please specify the affected core module name\r\n\r\nIf possible, please provide code that demonstrates the problem, keeping it as\r\nsimple and free of external dependencies as you can.\r\n--\u003e\r\n\r\n* **Version**: v10.10.0, v11.4.0\r\n* **Platform**: Linux 4.4.0-45-generic #66-Ubuntu SMP Wed Oct 19 14:12:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux\r\n* **Subsystem**: stream\r\n\r\n\u003c!-- Please provide more details below this comment. --\u003e\r\n\r\n[The documentation](https://nodejs.org/api/stream.html#stream_readable_read_size_1) for `readable._read(size)` states the following:\r\n\r\n\u003e `readable._read()` is guaranteed to be called only once within a synchronous execution, i.e. a microtick.\r\n\r\nHowever the following example demonstrates starvation if that method is able to push all of its data synchronously. The stream implementation ends up calling the read-function repeatedly and won't yield execution as documented.\r\n\r\n```JavaScript\r\nvar Readable = require('stream').Readable;\r\n\r\nlet c = 10;\r\nlet r = new Readable({\r\n read: () =\u003e {\r\n process.nextTick(console.log, 'next-tick');\r\n r.push('a');\r\n }\r\n});\r\n\r\nr.on('data', () =\u003e {});\r\nprocess.nextTick(console.log, 'Got food'); // Never printed.\r\nconsole.log('Requested food');\r\n```\r\n\r\nI believe the issue is caused by the `flow` function calling `stream.read` repeatedly within a while-loop until the stream fails to return data:\r\n\r\nhttps://github.com/nodejs/node/blob/951b1c3e09bae8395dde0d4f5cecb1fde117fa94/lib/_stream_readable.js#L933-L937\r\n\r\nOne option for fixing this might be to make `flow` recurse with next tick:\r\n\r\n```JavaScript\r\nfunction flow(stream) {\r\n const state = stream._readableState;\r\n debug('flow', state.flowing);\r\n flow_(stream, state);\r\n}\r\n\r\nfunction flow_(stream, state) {\r\n if(state.flowing \u0026\u0026 stream.read() !== null)\r\n process.nextTick(flow_, stream, state);\r\n}\r\n```\r\n\r\nAlternatively just updating the documentation might also work. I guess most streams that would encounter the issue would just delay `r.push()` into the next tick as a workaround.\r\n\r\nEven in my case this was just something I spotted while looking into #24918. I don't personally have a use case for such a stream.","author":{"url":"https://github.com/Rantanen","@type":"Person","name":"Rantanen"},"datePublished":"2018-12-09T10:05:57.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/24919/node/issues/24919"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:43b55aed-6364-d64b-cfd4-c31b403abbbc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CB26:1EEA:4A35BFA:691801C:6A500754 |
| html-safe-nonce | 779855b9208f872beece75e943b199363941773243879d2e8b2fb69ca6c6e5c3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQjI2OjFFRUE6NEEzNUJGQTo2OTE4MDFDOjZBNTAwNzU0IiwidmlzaXRvcl9pZCI6IjM5MzkxMzgwOTIwNTc3NTc1MjQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2e13cb5ef284e2be18ca5afdd004d3393f3f6fbf73f5048cbc6a7fec4b8cb217 |
| hovercard-subject-tag | issue:389005885 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/24919/issue_layout |
| twitter:image | https://opengraph.githubassets.com/330e077c2caf34cdc4a17273bb125570de0feb3c3f6ea6acf88f5612641c0235/nodejs/node/issues/24919 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/330e077c2caf34cdc4a17273bb125570de0feb3c3f6ea6acf88f5612641c0235/nodejs/node/issues/24919 |
| og:image:alt | Version: v10.10.0, v11.4.0 Platform: Linux 4.4.0-45-generic #66-Ubuntu SMP Wed Oct 19 14:12:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux Subsystem: stream The documentation for readable._read(size) s... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Rantanen |
| hostname | github.com |
| expected-hostname | github.com |
| None | 56eec25a3430492dd6ec2a480eb33256a0da5192d5714b0db4f5ab5f33203d94 |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 93a615b89b7dcbdb6b678f4772931ecc28ada82a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width