René's URL Explorer Experiment


Title: Write after end when piping a ClientRequest to ServerResponse · Issue #14023 · nodejs/node · GitHub

Open Graph Title: Write after end when piping a ClientRequest to ServerResponse · Issue #14023 · nodejs/node

X Title: Write after end when piping a ClientRequest to ServerResponse · Issue #14023 · nodejs/node

Description: Version: v4.8.3 Platform: Linux Hey, I believe there is a minor issue in _http_outgoing.js code. In the constructor, the property writable is changed to true (https://github.com/nodejs/node/blob/v4.x/lib/_http_outgoing.js#L64) : this.wri...

Open Graph Description: Version: v4.8.3 Platform: Linux Hey, I believe there is a minor issue in _http_outgoing.js code. In the constructor, the property writable is changed to true (https://github.com/nodejs/node/blob/v4...

X Description: Version: v4.8.3 Platform: Linux Hey, I believe there is a minor issue in _http_outgoing.js code. In the constructor, the property writable is changed to true (https://github.com/nodejs/node/blob/v4...

Opengraph URL: https://github.com/nodejs/node/issues/14023

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Write after end when piping a ClientRequest to ServerResponse","articleBody":"* **Version**: v4.8.3\r\n* **Platform**: Linux\r\n\r\nHey,\r\n\r\nI believe there is a minor issue in `_http_outgoing.js` code. In the constructor, the property `writable` is changed to `true` (https://github.com/nodejs/node/blob/v4.x/lib/_http_outgoing.js#L64) :\r\n`this.writable = true;`\r\nAs of my understanding, this property is set in the constructor since `OutgoingMessage` inherits from `Stream`, and in `Stream`'s code, this property might be checked to validate that a stream is writable.\r\nHowever, this property is never changed to `false` in `_http_outgoing.js`. Even when the message is closed (i.e. the `end` method was called), this property remains `true`. \r\n\r\nThe class `OutgoingMessage` maintains another property, `finished`, which indicates whether the `OutgoingMessage` was finished already or not. In the constructor `finished` is set to `false`, and later on, in the `end` method, it is set to true. I believe that `finished` and `writable` should have opposite values, since when a `OutgoingMessage` is closed - it **is not** `writable` anymore and it **is** `finished` .\r\n\r\nThis minor issue doesn't have much effect, but it might raise a lot of `write after end` errors, especially when piping. I can easily catch such errors so the process won't crash, but I guess the error shouldn't be raised in the first place.\r\n\r\nThe following simple code reproduces this (note that I use the `request` npm):\r\n\r\nServer:\r\n```\r\n'use strict';\r\nconst http = require('http');\r\nconst port = 9876;\r\nconst request = require('request');\r\n\r\n// ***************** Server ******************\r\n\r\n// URL of some big file that we want to stream to the client.\r\nconst bigFileUrl = \"http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_native_60fps_normal.mp4\";\r\n\r\nconst requestHandler = (req, res) =\u003e {\r\n    console.log(\"Server: Got a new request.\");\r\n\r\n    // Creating a server request to some file\r\n    let requestOptions = {\r\n        url: bigFileUrl\r\n    };\r\n    let serverRequest = request(requestOptions);\r\n    // Piping the data from the server's request to the client's response\r\n    serverRequest.pipe(res);\r\n\r\n    // When the client's request is ended, closing the client's response and the server's request\r\n    req.on('close', () =\u003e {\r\n        console.log(\"Server: Client request was closed, closing server's request and client's response.\");\r\n        res.end();\r\n        serverRequest.abort();\r\n    });\r\n}\r\n\r\nconst server = http.createServer(requestHandler);\r\n\r\nserver.listen(port, (err) =\u003e {\r\n    console.log('server is listening on ', port);\r\n});\r\n```\r\n\r\nClient:\r\n```\r\n'use strict';\r\nconst http = require('http');\r\nconst port = 9876;\r\nconst request = require('request');\r\nconst stream = require('stream');\r\n\r\n// ***************** Client ******************\r\n\r\nfunction randomInt(low, high) {\r\n    return Math.floor(Math.random() * (high - low) + low);\r\n}\r\n\r\nlet requestId = 0;\r\nlet concurrentRequests = 0;\r\n\r\nsetInterval(function() {\r\n    const currentRequestId = ++requestId;\r\n    const timeout = randomInt(0, 3000);\r\n    console.log(\"Client: Initiating a new request with id \", currentRequestId, \". Closing after \", timeout, \r\n    \t\t\t\"ms. There are currently \", (++concurrentRequests), \" concurrent requests.\");\r\n\r\n    let req = request({\r\n        url: \"http://localhost:\" + port\r\n    });\r\n    // piping the response to some stream. Doesn't really matter to which stream.\r\n    let outputStream = new stream.Writable({\r\n\t  write: function(chunk, encoding, next) {\r\n\t    next();\r\n\t  }\r\n\t});\r\n    req.pipe(outputStream);\r\n\r\n    setTimeout(function() {\r\n        console.log(\"Client: Closing request number\", currentRequestId, \". There are currently \", (--concurrentRequests), \" concurrent requests.\");\r\n        req.abort();\r\n    }, timeout);\r\n\r\n}, 100);\r\n```\r\n\r\nSimple output (of the server):\r\n\r\n```\r\nnode server_write_after_end.js \r\nserver is listening on  9876\r\nServer: Got a new request.\r\nServer: Got a new request.\r\nServer: Got a new request.\r\nServer: Got a new request.\r\nServer: Got a new request.\r\nServer: Client request was closed, closing server's request and client's response.\r\nServer: Client request was closed, closing server's request and client's response.\r\nServer: Got a new request.\r\nServer: Got a new request.\r\nServer: Got a new request.\r\nServer: Client request was closed, closing server's request and client's response.\r\nServer: Got a new request.\r\nServer: Got a new request.\r\nServer: Got a new request.\r\nServer: Got a new request.\r\nServer: Got a new request.\r\nServer: Client request was closed, closing server's request and client's response.\r\nstream.js:74\r\n      throw er; // Unhandled stream error in pipe.\r\n      ^\r\n\r\nError: write after end\r\n    at ServerResponse.OutgoingMessage.write (_http_outgoing.js:442:15)\r\n    at Request.ondata (stream.js:31:26)\r\n    at emitOne (events.js:77:13)\r\n    at Request.emit (events.js:169:7)\r\n    at IncomingMessage.\u003canonymous\u003e (/home/roee/dev/flash-testing/node_modules/request/request.js:1088:12)\r\n    at emitOne (events.js:77:13)\r\n    at IncomingMessage.emit (events.js:169:7)\r\n    at IncomingMessage.Readable.read (_stream_readable.js:368:10)\r\n    at flow (_stream_readable.js:759:26)\r\n    at resume_ (_stream_readable.js:739:3)\r\n```\r\n\r\nAs you can see from the stack, we reach `stream.js` line 31. In line 30 (https://github.com/nodejs/node/blob/v4.x/lib/stream.js#L30) we can find the condition that should have failed:\r\n`if (dest.writable) { ... } `\r\nSo, if my `OutgoingMessage`'s `writable` property was indeed changed to false once it was closed, the error wouldn't have been raised at all.\r\n\r\nNote that this probably happens due to race (between the client's request that was closed and the server's request, or actually the server-request's response, that got some new data), hence the code I attached doesn't reproduce this immediately and the output may not be the same as I attached.\r\n\r\nI've opened a PR with a fix to this issue:\r\n#14024 \r\nAfter applying this fix, my application works as expected and I never get any `write after end` errors in my scenario.\r\n\r\nAny help will be appreciated.\r\nThanks!\r\n\r\n\r\n\r\n\r\n\r\n","author":{"url":"https://github.com/Kasher","@type":"Person","name":"Kasher"},"datePublished":"2017-07-01T11:17:29.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/14023/node/issues/14023"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:e805f216-e186-4cb1-ca6f-0272431fbece
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id9438:2395E2:2BE844:3C80FE:6A5064C4
html-safe-nonced95261d81424e09eab96efad0544cae0820ab45157df81c079ce219909d71420
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NDM4OjIzOTVFMjoyQkU4NDQ6M0M4MEZFOjZBNTA2NEM0IiwidmlzaXRvcl9pZCI6IjgxODY4MDc4NzI0NTk0MDAzODgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacae39536f33616dc718696c9065fd03ed35b93d26bdae90a5059f7ea75400f5f4
hovercard-subject-tagissue:239943395
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/14023/issue_layout
twitter:imagehttps://opengraph.githubassets.com/04e7a0eeb4d0b331c98208be1cc5ba039064c17952ad7bb24d8276c0b22b8286/nodejs/node/issues/14023
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/04e7a0eeb4d0b331c98208be1cc5ba039064c17952ad7bb24d8276c0b22b8286/nodejs/node/issues/14023
og:image:altVersion: v4.8.3 Platform: Linux Hey, I believe there is a minor issue in _http_outgoing.js code. In the constructor, the property writable is changed to true (https://github.com/nodejs/node/blob/v4...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameKasher
hostnamegithub.com
expected-hostnamegithub.com
Noned6dc8294eb500fa36bbc57472d61fe87c522f9c3c1d64f70f4926f66a66a7efb
turbo-cache-controlno-preview
go-importgithub.com/nodejs/node git https://github.com/nodejs/node.git
octolytics-dimension-user_id9950313
octolytics-dimension-user_loginnodejs
octolytics-dimension-repository_id27193779
octolytics-dimension-repository_nwonodejs/node
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id27193779
octolytics-dimension-repository_network_root_nwonodejs/node
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release7ac0ad2f2c7e4b9056617355fd9e33e22b0c8df9
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/nodejs/node/issues/14023#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fnodejs%2Fnode%2Fissues%2F14023
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fnodejs%2Fnode%2Fissues%2F14023
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=nodejs%2Fnode
Reloadhttps://github.com/nodejs/node/issues/14023
Reloadhttps://github.com/nodejs/node/issues/14023
Reloadhttps://github.com/nodejs/node/issues/14023
Please reload this pagehttps://github.com/nodejs/node/issues/14023
nodejs https://github.com/nodejs
nodehttps://github.com/nodejs/node
Please reload this pagehttps://github.com/nodejs/node/issues/14023
Notifications https://github.com/login?return_to=%2Fnodejs%2Fnode
Fork 36k https://github.com/login?return_to=%2Fnodejs%2Fnode
Star 118k https://github.com/login?return_to=%2Fnodejs%2Fnode
Code https://github.com/nodejs/node
Issues 1.3k https://github.com/nodejs/node/issues
Pull requests 979 https://github.com/nodejs/node/pulls
Actions https://github.com/nodejs/node/actions
Projects https://github.com/nodejs/node/projects
Security and quality 0 https://github.com/nodejs/node/security
Insights https://github.com/nodejs/node/pulse
Code https://github.com/nodejs/node
Issues https://github.com/nodejs/node/issues
Pull requests https://github.com/nodejs/node/pulls
Actions https://github.com/nodejs/node/actions
Projects https://github.com/nodejs/node/projects
Security and quality https://github.com/nodejs/node/security
Insights https://github.com/nodejs/node/pulse
Write after end when piping a ClientRequest to ServerResponsehttps://github.com/nodejs/node/issues/14023#top
httpIssues or PRs related to the http subsystem.https://github.com/nodejs/node/issues?q=state%3Aopen%20label%3A%22http%22
https://github.com/Kasher
Kasherhttps://github.com/Kasher
on Jul 1, 2017https://github.com/nodejs/node/issues/14023#issue-239943395
https://github.com/nodejs/node/blob/v4.x/lib/_http_outgoing.js#L64https://github.com/nodejs/node/blob/v4.x/lib/_http_outgoing.js#L64
https://github.com/nodejs/node/blob/v4.x/lib/stream.js#L30https://github.com/nodejs/node/blob/v4.x/lib/stream.js#L30
#14024https://github.com/nodejs/node/pull/14024
httpIssues or PRs related to the http subsystem.https://github.com/nodejs/node/issues?q=state%3Aopen%20label%3A%22http%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.