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

http2: fix end without read #20621

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,6 @@ class Http2ServerRequest extends Readable {
stream[kRequest] = this;

// Pause the stream..
stream.pause();
stream.on('data', onStreamData);
stream.on('trailers', onStreamTrailers);
stream.on('end', onStreamEnd);
stream.on('error', onStreamError);
Expand Down Expand Up @@ -308,8 +306,12 @@ class Http2ServerRequest extends Readable {
_read(nread) {
const state = this[kState];
if (!state.closed) {
state.didRead = true;
process.nextTick(resumeStream, this[kStream]);
if (!state.didRead) {
state.didRead = true;
this[kStream].on('data', onStreamData);
} else {
process.nextTick(resumeStream, this[kStream]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the nextTick is needed here, as resume() happens in a nextTick anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That part is carried over from before (didn't change) and used to definitely be necessary. We have a test for it even which breaks otherwise: parallel/test-http2-compat-serverrequest-pipe.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could probably have a look at the git blame for the line to find the corresponding PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #15503 — I don't fully recall where exactly we have an async call to pause but we do somewhere.

Copy link
Member

@mcollina mcollina May 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably something we would have to look into. It's a pretty old one: #15702.

}
} else {
this.emit('error', new ERR_HTTP2_INVALID_STREAM());
}
Expand Down
8 changes: 4 additions & 4 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,8 @@ function onStreamClose(code) {
// it completely.
stream.push(null);

// Same as net.
if (stream.readableLength === 0) {
stream.read(0);
}
if (!stream[kState].didRead && !stream._readableState.resumeScheduled)
stream.resume();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is slightly different than read(0). Can you please explain why this is needed?
I would prefer if we didn't use _readableState here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is equivalent to what we do in http and is needed because in cases where the user doesn't intend to consume the data (indicated by never having read and not having a pending resume call), we still want to be able to properly destroy the stream.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a comment about this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do later today.

}
}

Expand Down Expand Up @@ -1796,6 +1794,8 @@ class Http2Stream extends Duplex {
const ret = this[kHandle].trailers(headersList);
if (ret < 0)
this.destroy(new NghttpError(ret));
else
this[kMaybeDestroy]();
}

get closed() {
Expand Down
15 changes: 9 additions & 6 deletions test/parallel/test-http2-client-upload-reject.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ fs.readFile(loc, common.mustCall((err, data) => {
const server = http2.createServer();

server.on('stream', common.mustCall((stream) => {
stream.on('close', common.mustCall(() => {
assert.strictEqual(stream.rstCode, 0);
}));

stream.respond({ ':status': 400 });
stream.end();
// Wait for some data to come through.
setImmediate(() => {
stream.on('close', common.mustCall(() => {
assert.strictEqual(stream.rstCode, 0);
}));

stream.respond({ ':status': 400 });
stream.end();
});
}));

server.listen(0, common.mustCall(() => {
Expand Down
44 changes: 44 additions & 0 deletions test/parallel/test-http2-compat-client-upload-reject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

// Verifies that uploading data from a client works

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const http2 = require('http2');
const fs = require('fs');
const fixtures = require('../common/fixtures');

const loc = fixtures.path('person-large.jpg');

assert(fs.existsSync(loc));

fs.readFile(loc, common.mustCall((err, data) => {
assert.ifError(err);

const server = http2.createServer(common.mustCall((req, res) => {
setImmediate(() => {
res.writeHead(400);
res.end();
});
}));

server.listen(0, common.mustCall(() => {
const client = http2.connect(`http://localhost:${server.address().port}`);

const req = client.request({ ':method': 'POST' });
req.on('response', common.mustCall((headers) => {
assert.strictEqual(headers[':status'], 400);
}));

req.resume();
req.on('end', common.mustCall(() => {
server.close();
client.close();
}));

const str = fs.createReadStream(loc);
str.pipe(req);
}));
}));