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

http: OutgoingMessage change writable after end #14024

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
1 change: 1 addition & 0 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
this.connection.uncork();

this.finished = true;
this.writable = false;

// There is the first message on the outgoing queue, and we've sent
// everything to the socket.
Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-http-outgoing-finish-writable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');

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 add a little comment explaining what this test is about here?

const server = http.createServer(common.mustCall(function(req, res) {
assert(res.writable, 'Res should be writable when it is received \
and opened.');
assert(!res.finished, 'Res shouldn\'t be finished when it is received \
and opened.');
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 use assert.strictEqual without a message here and in the rest of the tests?

res.end();
assert(!res.writable, 'Res shouldn\'t be writable after it was closed.');
assert(res.finished, 'Res should be finished after it was closed.');

server.close();
}));

server.listen(0);

server.on('listening', common.mustCall(function() {
http.request({
port: server.address().port,
method: 'GET',
path: '/'
}).end();
Copy link
Member

Choose a reason for hiding this comment

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

What http.request  returns is an OutgoingMessage as well, we might want to change that it there as well.

}));
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';
const common = require('../common');
const http = require('http');
const OutgoingMessage = require('_http_outgoing').OutgoingMessage;

const server = http.createServer(common.mustCall(function(req, res) {
console.log('Got a request, piping an OutgoingMessage to it.');
const outgointMessage = new OutgoingMessage();
outgointMessage.pipe(res);
Copy link
Member

Choose a reason for hiding this comment

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

OutgoingMessage should really be write-only, and not be piped. The fact that OutgoingMessage has a pipe  method is related to Node.js history, and it should not be used. Why Does this use case matter to you?

Copy link
Contributor Author

@Kasher Kasher Jul 10, 2017

Choose a reason for hiding this comment

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

I agree. OutgoingMessage is just an example of a type that inherits from Stream. A better practice would be to create a new type that inherits from Stream and use this in the test. I would change this.

I chose OutgoingMessage since if I pipe from it, the outcome will be very similar to what happens when I use request npm .

I guess this should either be disabled (by throwing an exception as you suggested), or it should work.
Here is what they do in request npm:

  1. They call to pipe on a type that inherits from Stream (the type is called Request):
    https://github.com/request/request/blob/master/request.js#L1489
  2. Upon data on an IncomingResponse, they emit this on the Request:
    https://github.com/request/request/blob/master/request.js#L1082

For more detailed explanation you can read my reply: #14024 (comment)


setTimeout(() => {
console.log('Closing response.');
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 remove the console.log statements?

res.end();
outgointMessage.emit('data', 'some data');

// If we got here - 'write after end' wasn't raised and the test passed.
setTimeout(() => server.close(), 10);
}, 10);
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 going to be very flaky. Can you refactor the test to not rely on timers?

Copy link
Contributor Author

@Kasher Kasher Jul 10, 2017

Choose a reason for hiding this comment

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

Sure thing. I totally agree with you. Basically I needed setTimeout with a timeout of zero (or process.nextTick). I'll change to one of these.


setInterval(() => {
console.log('Emitting some data to outgointMessage');
outgointMessage.emit('data', 'some data');
}, 1).unref();

}));

server.listen(0);

server.on('listening', common.mustCall(function() {
http.request({
port: server.address().port,
method: 'GET',
path: '/'
}).end();
}));