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

Could not write to the response multiple times #6

Closed
trosos opened this issue Apr 24, 2016 · 5 comments
Closed

Could not write to the response multiple times #6

trosos opened this issue Apr 24, 2016 · 5 comments
Assignees
Labels

Comments

@trosos
Copy link

trosos commented Apr 24, 2016

I am having trouble with using two consecutive write()s into a response.

Steps to reproduce:

  • In node, run the following:
require("node-fastcgi").createServer(function (req, res) {
    res.writeHead(200, {"Content-Type": "text/html"});
    res.write("a");
    res.end("b");
}).listen("/tmp/socket");
  • In another terminal, run the http server:
$ node_modules/.bin/fastcgi --port=8888 --socket=/tmp/socket &
  • In yet another terminal, perform the http request:
$ wget -O - http://localhost:8888/ 2>/dev/null

Expected results:
I would expect wget to output "ab".

Actual results:
wget outputs only "a".

Note 1:
If I used nginx with disabled buffering ("fastcgi_buffering off;") instead of "node_modules/.bin/fastcgi", the fastcgi server would crash with:

events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: write EPIPE
    at exports._errnoException (util.js:870:11)
    at WriteWrap.afterWrite (net.js:769:14)

and wget would output only "a".

Note 2:
On the other hand, if I delay the call to res.end with setTimeout, everything works as expected:

require("node-fastcgi").createServer(function (req, res) {
    res.writeHead(200, {"Content-Type": "text/html"});
    res.write("a");
    setTimeout(function () {
        res.end("b");
    });
}).listen("/tmp/socket");
@fbbdev fbbdev self-assigned this May 12, 2016
@fbbdev fbbdev added the bug label May 12, 2016
@fbbdev
Copy link
Owner

fbbdev commented May 18, 2016

I'm trying to debug this and I'm seeing some bad interaction between my module and node's http module. I've been able to get two writes working, but then three writes won't work. I don't understand exactly what's going on, and since my current implementation is a total mess I fear I'll have to rewrite it completely to fix this bug.

In the meantime you can work around this by buffering writes and submitting them in a single call to res.end, or you can put the call to res.end in the callback of the last call to res.write (e.g. res.write("a", res.end.bind(res, "B"))).

fbbdev added a commit that referenced this issue May 18, 2016
@fbbdev
Copy link
Owner

fbbdev commented May 19, 2016

I think it's resolved now. Would you test the latest version from the master branch? It's a complete rewrite and I need to get some feedback before releasing it.

@fbbdev fbbdev closed this as completed May 20, 2016
@trosos
Copy link
Author

trosos commented May 29, 2016

Thank you and sorry for the delay.

Now it seems to work except the fact that the body seems to be "chunked"-encoded. Repeating the experiment from my first post, I get:

$ wget -O - http://localhost:8888/ 2>/dev/null | hd
00000000  31 0d 0a 61 0d 0a 31 0d  0a 62 0d 0a 30 0d 0a 0d  |1..a..1..b..0...|
00000010  0a                                                |.|
00000011

while I expect wget to output only "ab".

This may be a client issue, however using nginx's fastcgi client I get analogous results.

@fbbdev
Copy link
Owner

fbbdev commented May 30, 2016

Thank you for reporting this, it's my fault. I created a new issue: #8

@fbbdev fbbdev reopened this May 30, 2016
@fbbdev fbbdev closed this as completed May 30, 2016
@fbbdev
Copy link
Owner

fbbdev commented May 30, 2016

It should be fixed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants