{"action":"created","issue":{"url":"https://api.github.com/repos/nodejs/node/issues/21740","repository_url":"https://api.github.com/repos/nodejs/node","labels_url":"https://api.github.com/repos/nodejs/node/issues/21740/labels{/name}","comments_url":"https://api.github.com/repos/nodejs/node/issues/21740/comments","events_url":"https://api.github.com/repos/nodejs/node/issues/21740/events","html_url":"https://github.com/nodejs/node/issues/21740","id":340005015,"node_id":"MDU6SXNzdWUzNDAwMDUwMTU=","number":21740,"title":"HTTP2: Calling `res.end` after `res.writeHead(204)` cause an error","user":{"login":"RidgeA","id":7588876,"node_id":"MDQ6VXNlcjc1ODg4NzY=","avatar_url":"https://avatars2.githubusercontent.com/u/7588876?v=4","gravatar_id":"","url":"https://api.github.com/users/RidgeA","html_url":"https://github.com/RidgeA","followers_url":"https://api.github.com/users/RidgeA/followers","following_url":"https://api.github.com/users/RidgeA/following{/other_user}","gists_url":"https://api.github.com/users/RidgeA/gists{/gist_id}","starred_url":"https://api.github.com/users/RidgeA/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RidgeA/subscriptions","organizations_url":"https://api.github.com/users/RidgeA/orgs","repos_url":"https://api.github.com/users/RidgeA/repos","events_url":"https://api.github.com/users/RidgeA/events{/privacy}","received_events_url":"https://api.github.com/users/RidgeA/received_events","type":"User","site_admin":false},"labels":[{"id":647282877,"node_id":"MDU6TGFiZWw2NDcyODI4Nzc=","url":"https://api.github.com/repos/nodejs/node/labels/http2","name":"http2","color":"c2e0c6","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-07-10T20:59:50Z","updated_at":"2018-07-10T23:14:41Z","closed_at":null,"author_association":"NONE","body":"<!--\r\nThank you for reporting an issue.\r\n\r\nThis issue tracker is for bugs and issues found within Node.js core.\r\nIf you require more general support please file an issue on our help\r\nrepo. https://github.com/nodejs/help\r\n\r\n\r\nPlease fill in as much of the template below as you're able.\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 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 are able.\r\n-->\r\n\r\n* **Version**: v10.6.0\r\n* **Platform**: Linux localhost.localdomain 4.17.3-200.fc28.x86_64 #1 SMP Tue Jun 26 14:17:07 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux (Fedora 28)\r\n* **Subsystem**: http2\r\n\r\n<!-- Enter your issue details below this comment. -->\r\nHi!\r\n\r\nI'm playing around with http2 module and server push and faced with a strange error.\r\n\r\nInvoking of `res.end()` method after `res.writeHeader(204)` causes error:\r\n\r\n```\r\ninternal/http2/compat.js:377\r\n  this.sendTrailers(this[kResponse][kTrailers]);\r\n                                   ^\r\n\r\nTypeError: Cannot read property 'Symbol(trailers)' of undefined\r\n    at ServerHttp2Stream.onStreamTrailersReady (internal/http2/compat.js:377:36)\r\n    at ServerHttp2Stream.emit (events.js:182:13)\r\n    at Http2Stream.onStreamTrailers [as ontrailers] (internal/http2/core.js:311:15)\r\n```\r\n\r\nCode to reproduce:\r\n\r\n```\r\n\"use strict\";\r\n\r\nconst http2 = require('http2');\r\nconst fs = require('fs');\r\nconst cert = {\r\n  key: fs.readFileSync('./cert/key.pem'),\r\n  cert: fs.readFileSync('./cert/cert.pem')\r\n};\r\nconst html =\r\n`\r\n<!doctype html>\r\n<head>\r\n    <title>HTTP/2 Test</title>\r\n    <link rel=\"stylesheet\" href=\"/styles.css\" type=\"text/css\">\r\n    <script src=\"/bundle.js\" async></script>\r\n</head>\r\n<body>\r\n<h1>Hello</h1>\r\n</body>\r\n</html>\r\n`;\r\n\r\nhttp2.createSecureServer(cert, (req, res) => {\r\n\r\n  const {url} = req;\r\n\r\n  if (['/index.html', '/'].includes(url)) {\r\n    res.writeHead(200, {'content-type':'text/html'});\r\n    res.end(html);\r\n  } else {\r\n    res.writeHead(204);\r\n    res.end();\r\n  }\r\n\r\n}).listen(8443, err => {\r\n  if (!err) {\r\n    console.log(`Server started at ${8443}.`)\r\n  }\r\n});\r\n```\r\n\r\nAfter digging into the issue, I found out that if we set 204, 205 and 304 status codes the stream are closed automatically - see https://github.com/nodejs/node/blob/master/lib/internal/http2/core.js#L2266 and https://github.com/nodejs/node/blob/master/lib/internal/http2/core.js#L2281\r\n\r\nCalling of `req.end` method after that causes an error (see above). \r\n\r\nAccording to the documentation `req.end()`  MUST be called for each request: https://nodejs.org/api/http2.html#http2_response_end_data_encoding_callback\r\nbut it is impossible due to closing the stream caused by calling `writeHeader` method.\r\n\r\nI believe, in order to keep backward compatibility with `http`/`https` modules, the `res.end` method shouldn't throw an error in such case."},"comment":{"url":"https://api.github.com/repos/nodejs/node/issues/comments/403994929","html_url":"https://github.com/nodejs/node/issues/21740#issuecomment-403994929","issue_url":"https://api.github.com/repos/nodejs/node/issues/21740","id":403994929,"node_id":"MDEyOklzc3VlQ29tbWVudDQwMzk5NDkyOQ==","user":{"login":"addaleax","id":899444,"node_id":"MDQ6VXNlcjg5OTQ0NA==","avatar_url":"https://avatars2.githubusercontent.com/u/899444?v=4","gravatar_id":"","url":"https://api.github.com/users/addaleax","html_url":"https://github.com/addaleax","followers_url":"https://api.github.com/users/addaleax/followers","following_url":"https://api.github.com/users/addaleax/following{/other_user}","gists_url":"https://api.github.com/users/addaleax/gists{/gist_id}","starred_url":"https://api.github.com/users/addaleax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/addaleax/subscriptions","organizations_url":"https://api.github.com/users/addaleax/orgs","repos_url":"https://api.github.com/users/addaleax/repos","events_url":"https://api.github.com/users/addaleax/events{/privacy}","received_events_url":"https://api.github.com/users/addaleax/received_events","type":"User","site_admin":false},"created_at":"2018-07-10T23:14:41Z","updated_at":"2018-07-10T23:14:41Z","author_association":"MEMBER","body":"@nodejs/http2"}}
{"actor":{"display_login":"addaleax"}}