Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError occur with http2ServerRequest.socket.destroy() #22855

Closed
sogaani opened this issue Sep 14, 2018 · 5 comments
Closed

TypeError occur with http2ServerRequest.socket.destroy() #22855

sogaani opened this issue Sep 14, 2018 · 5 comments

Comments

@sogaani
Copy link

sogaani commented Sep 14, 2018

I met following error when call http2ServerRequest.socket.destroy();

internal/http2/core.js:1467
  const ret = stream[kHandle].trailers(headersList);
                              ^

TypeError: Cannot read property 'trailers' of undefined
    at Immediate.finishSendTrailers (internal/http2/core.js:1467:31)
    at runCallback (timers.js:695:11)
    at tryOnImmediate (timers.js:665:5)
    at processImmediate (timers.js:647:5)

The minimal code to reproduce.

const http2 = require('http2');
const {
    HTTP2_HEADER_PATH,
    HTTP2_HEADER_METHOD,
} = http2.constants;

const app = http2.createServer((req, res) => {
    res.end('hello');
    setImmediate( ()=>{ req.socket.destroy(); });
});

app.listen(8080, () => {
    const session = http2.connect('http://localhost:8080');
    const request = session.request({ [HTTP2_HEADER_PATH]: '/', [HTTP2_HEADER_METHOD]: 'get' });
    request.once('response', (headers, flags) => {
        let data = '';
        request.on('data', (chunk) => { data += chunk; });
        request.on('end', () => { console.log(data); });
    });
    request.end();
});

node: version

$ node -v
v10.10.0

I guess we should check if stream destroyed in finishSendTrailers, or we should not use setImmeidiate to call finishSendTrailers.
Currently we can call http2Session.destroy(); which delete some property while finishSendTrailers is listed in nodejs eventloop and it lead to an error.

@lovinglyy
Copy link
Contributor

Can reproduce in the same version. I also tested in a experimental http2 version, node v8.9.4 and I couldn't reproduce, checking if it's destroyed looks concise in my view, we could also directly check if the stream is defined, as the issue is mainly reproducible by calling socket.destroy() with setImmediate.

@apapirovski
Copy link
Member

Is there a specific reason you need to call setImmediate( ()=>{ req.socket.destroy(); });? Like that's not a clean way to close the connection...

@sogaani
Copy link
Author

sogaani commented Sep 14, 2018

I found this issue with Express test case. Express call request.socket.destroy() in finalize function when error occur in request handler or middleware and already send header and body frame.
I'm not sure what is clean way to close connection, but I don't think it is strange close connection when server internal error occurs and already send header and body frame.

Example.

var app = express();

app.get('/', function(req, res){
  res.end('yay');
  throw new Error('boom');
});

@mcollina
Copy link
Member

I'm working on a fix.

@mcollina
Copy link
Member

Fixed in #22896.

targos pushed a commit that referenced this issue Sep 20, 2018
Fixes: #22855
PR-URL: #22896
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
kjin pushed a commit to kjin/node that referenced this issue Oct 3, 2018
Fixes: nodejs#22855
PR-URL: nodejs#22896
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
kjin pushed a commit to kjin/node that referenced this issue Oct 16, 2018
Fixes: nodejs#22855
PR-URL: nodejs#22896
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
BethGriggs pushed a commit that referenced this issue Oct 17, 2018
Fixes: #22855
Backport-PR-URL: #22850
PR-URL: #22896
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants