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

Cannot call method 'writeHead' of undefined #609

Closed
Charuru opened this issue Nov 1, 2011 · 39 comments
Closed

Cannot call method 'writeHead' of undefined #609

Charuru opened this issue Nov 1, 2011 · 39 comments

Comments

@Charuru
Copy link

Charuru commented Nov 1, 2011

I'm getting this in 0.8.6

I'm not sure which browser connected or caused the problem, this is just something I found in my err.log trying to figure out the reason for a crash.

/home/ubuntu/www/node_modules/socket.io/lib/transports/jsonp-polling.js:87
  this.response.writeHead(200, {
                ^
TypeError: Cannot call method 'writeHead' of undefined
    at JSONPPolling.doWrite (/home/ubuntu/www/node_modules/socket.io/lib/transports/jsonp-polling.js:87:17)
    at JSONPPolling.write (/home/ubuntu/www/node_modules/socket.io/lib/transports/http-polling.js:120:8)
    at JSONPPolling.packet (/home/ubuntu/www/node_modules/socket.io/lib/transport.js:515:15)
    at JSONPPolling.error (/home/ubuntu/www/node_modules/socket.io/lib/transport.js:498:8)
    at JSONPPolling.onData (/home/ubuntu/www/node_modules/socket.io/lib/transports/jsonp-polling.js:68:10)
    at IncomingMessage.<anonymous> (/home/ubuntu/www/node_modules/socket.io/lib/transports/http.js:60:12)
    at IncomingMessage.emit (events.js:61:17)
    at HTTPParser.onMessageComplete (http.js:133:23)
    at Socket.ondata (http.js:1008:22)
    at Socket._onReadable (net.js:681:27)

Back to 0.8.5 for now.

@3rd-Eden
Copy link
Contributor

3rd-Eden commented Nov 1, 2011

@tommypowerz
Copy link

getting the same error

@dymsza
Copy link

dymsza commented Nov 12, 2011

Hey,

in my opinion this is source of problem

https://github.com/LearnBoost/socket.io/blob/master/lib/transports/http.js#L72

so the response is set only for request req.method != 'POST' but onData is called for req.method == 'POST'

@mogest
Copy link

mogest commented Nov 21, 2011

+1. I'll try to take a look at it tonight; surprised more people haven't run into this.

@ghost
Copy link

ghost commented Nov 25, 2011

Have the same problem. Happens usually after system is up for a while

@lekoder
Copy link

lekoder commented Nov 28, 2011

Have same issue with 0.8.7, but I'm unable to replicate it reliably. Happens on heavy load, with 400 clients connected and 20+ connecting/disconecting per second.

@survivant
Copy link

I had the issue. I found out that I forgot to encode the message in the POST method. Here a example

T 192.168.10.1:57908 -> 192.168.10.141:3000 [AP]
POST /socket.io/1/jsonp-polling/1897756582168726773?i=0 HTTP/1.1.
Host: 192.168.10.141:3000.
Connection: keep-alive.
Accept: /.
User-Agent: NING/1.0.
Content-Length: 65.
.
"5:1+::{"name":"nickname","args":["test_1324996401409"]}"

GOOD WAY HERE

Content-Length: 111.
.
d=%225%3A1%2B%3A%3A%7B%5C%22name%5C%22%3A%5C%22nickname%5C%22%2C%5C%22args%5C%22%3A%5B%5C%22test%5C%22%5D%7D%22

Hope that can help.

@antonienko
Copy link

I also have this issue, but it's only after a few days of execution of my node/socket.io program, still don't know the exact reason.

Survivant, I am not sure that I'm following your answer. Are you saying that we should encode the arguments to the socket.send function?

Thanks for the help.

@antonienko
Copy link

Anybody have any news about this bug?

@rauchg
Copy link
Contributor

rauchg commented Jan 10, 2012

I'm currently looking into this.

@antonienko
Copy link

Great! Gracias!

@andremoniz
Copy link

Having the same problem here.

DEBUG: /root/node/node_modules/socket.io/lib/transports/jsonp-polling.js:87

DEBUG: this.response.writeHead(200, {

DEBUG:
DEBUG: ^

DEBUG: TypeError: Cannot call method 'writeHead' of undefined
at JSONPPolling.doWrite (/root/node/node_modules/socket.io/lib/transports/jsonp-polling.js:87:17)
at JSONPPolling.write (/root/node/node_modules/socket.io/lib/transports/http-polling.js:120:8)
at JSONPPolling.packet (/root/node/node_modules/socket.io/lib/transport.js:515:15)
at JSONPPolling.error (/root/node/node_modules/socket.io/lib/transport.js:498:8)
at JSONPPolling.onData (/root/node/node_modules/socket.io/lib/transports/jsonp-polling.js:68:10)
at IncomingMessage. (/root/node/node_modules/socket.io/lib/transports/http.js:60:12)
at IncomingMessage.emit (events.js:61:17)
at HTTPParser.onMessageComplete (http.js:133:23)
at Socket.ondata (http.js:1029:22)
at Socket._onReadable (net.js:687:27)

@DefV
Copy link

DefV commented Jan 20, 2012

+1 on this bug, happens randomly, sometimes 5 min, sometimes 20 hours.

@hwndept
Copy link

hwndept commented Feb 14, 2012

+1, have same problems.

@sokac
Copy link

sokac commented Feb 22, 2012

The quick fix is to remove jsonp-polling from transports.
e.g.
socket.set('transports', [ 'websocket', 'htmlfile', 'xhr-polling' ]);

@hwndept
Copy link

hwndept commented Feb 22, 2012

thank you men

@DavidChouinard
Copy link

Any ETA on this? It's still an issue.

@user-dotcom
Copy link

+1 I'm seeing this on 0.8.7 similar sort of behaviour to what DefV describes.

@DavidChouinard
Copy link

Might be unrelated, but my upgrade to 0.9.0 has dramatically increased the occurrences of this error.

@rauchg
Copy link
Contributor

rauchg commented Feb 28, 2012

Do you see this only with jsonp ? Or also xhr-polling ?

@rauchg
Copy link
Contributor

rauchg commented Feb 28, 2012

Also is it correlated to another exception beforehand in your logs ?

@hwndept
Copy link

hwndept commented Feb 28, 2012

i have disabled xhr-polling and jsop-polling and now my server works fine.

@user-dotcom
Copy link

@guille I'm only seeing this on jsonp

@DavidChouinard
Copy link

@guille Still haven't tested if the issue arrises on jsonp or xhr, but I can confirm it's unrelated to 0.9.0.

@rauchg
Copy link
Contributor

rauchg commented Feb 29, 2012

@DavidChouinard Thanks for the update. I'll keep you posted on my findings

@bang590
Copy link

bang590 commented Mar 5, 2012

i have the same problem, work fine after disable jsonp-polling

@DavidChouinard
Copy link

It seems that this is caused by a chain reaction possibly set off by a high number of jsonp-polling connections. I get bursts of 100-200 of this exception.

@zackster
Copy link

+1 on this issue, but only on IE7, and only since I added a cookie-based authentication check before rendering the page...

@johnsmith123
Copy link

+1 on this issue, my first time using socket.io and this error occurs whenever i visit using a nokia/c3 (old webkit-based browser). source is straight from the socket.io homepage

@tommypowerz
Copy link

having the same problem..
the only fix is to disable jsonp transport at the moment i guess....

/node_modules/socket.io/lib/transports/jsonp-polling.js:88
this.response.writeHead(200, {
^
TypeError: Cannot call method 'writeHead' of undefined
at JSONPPolling.doWrite (/node_modules/socket.io/lib/transports/jsonp-polling.js:88:17)
at JSONPPolling.write (/node_modules/socket.io/lib/transports/http-polling.js:132:8)
at JSONPPolling.packet (/node_modules/socket.io/lib/transport.js:515:15)
at JSONPPolling.error (/node_modules/socket.io/lib/transport.js:498:8)
at JSONPPolling.onData (/node_modules/socket.io/lib/transports/jsonp-polling.js:69:10)
at IncomingMessage. (/node_modules/socket.io/lib/transports/http.js:65:12)
at IncomingMessage.emit (events.js:64:17)
at HTTPParser.onMessageComplete (http.js:137:23)
at Socket.ondata (http.js:1410:22)
at TCP.onread (net.js:374:27)

@3rd-Eden
Copy link
Contributor

3rd-Eden commented May 2, 2012

Or you can just ignore this issue and keep it enabled? Just use the uncaughtException handler
from node to prevent the process from crashing on these kind of silly issues.

On Wednesday, May 2, 2012 at 12:31 PM, tommypowerz wrote:

having the same problem..
the only fix is to disable jsonp transport at the moment i guess....

/node_modules/socket.io/lib/transports/jsonp-polling.js:88 (http://socket.io/lib/transports/jsonp-polling.js:88)
this.response.writeHead(200, {
^
TypeError: Cannot call method 'writeHead' of undefined
at JSONPPolling.doWrite (/node_modules/socket.io/lib/transports/jsonp-polling.js:88:17 (http://socket.io/lib/transports/jsonp-polling.js:88:17))
at JSONPPolling.write (/node_modules/socket.io/lib/transports/http-polling.js:132:8 (http://socket.io/lib/transports/http-polling.js:132:8))
at JSONPPolling.packet (/node_modules/socket.io/lib/transport.js:515:15 (http://socket.io/lib/transport.js:515:15))
at JSONPPolling.error (/node_modules/socket.io/lib/transport.js:498:8 (http://socket.io/lib/transport.js:498:8))
at JSONPPolling.onData (/node_modules/socket.io/lib/transports/jsonp-polling.js:69:10 (http://socket.io/lib/transports/jsonp-polling.js:69:10))
at IncomingMessage. (/node_modules/socket.io/lib/transports/http.js:65:12 (http://socket.io/lib/transports/http.js:65:12))
at IncomingMessage.emit (events.js:64:17)
at HTTPParser.onMessageComplete (http.js:137:23)
at Socket.ondata (http.js:1410:22)
at TCP.onread (net.js:374:27)


Reply to this email directly or view it on GitHub:
#609 (comment)

@vedratna-velani
Copy link

it is broken here cf76b13#lib/transports/jsonp-polling.js

After this commit. The flow is like this

  1. Line 65 on http.js makes call to 'self.onData' (Note that respnse is already ended before this call) that comes to jsonp-polling.js Line 65
  2. Now if JSON.parse throws any exception it calls this.error('parse', 'reconnect') that goes to transport.js Line 497 which in turn calls this.packet Line 514 on same file.
  3. this.packet calls this.write that goes to http-polling.js Line 131 which in turn calls this.doWrite that goes to jsonp-polling.js Line 82 and here it tries to wrtie something on response which will be undefined because response is already sent back on step (1)

So the root cause is we should not call this.error('parse', 'reconnect') on JSON.parse exception on OnData call. instead we should write some debug error for the same.

Though I am not able to reproduce this on my local box we got this on production environment but I am still interested to know in which use case JSON.parse throws an error before fixing it. Can anybody tell me how to reproduce this locally or can anybody post the JSON data coming through jsonp-polling that causes this issue?

Note: This happens only in case of jsonp-polling because it is inside overridden 'onData' method of jsonp-polling

@vedratna
Copy link

anybody any luck? as mentioned above can anybody tell for which kind of data JSON.parse throws an exception or how to reproduce it on local box?

@daneproject
Copy link

I also have this issue. I would love to have it fixed.

@dymsza
Copy link

dymsza commented Jun 8, 2012

+1

@mbabuskov
Copy link

I have the same issue with 0.9.6. when using JSONP.

I am not using cookie-based auth, so that is not related as zackster contemplated above.

@bferreira
Copy link

+1 also in 0.9.6

@ececon
Copy link

ececon commented Oct 4, 2012

I get exactly this issue on the following scenario. I use JMeter to do some load testing, so my server is configured to accept only the jsonp-polling and on JMeter I script the messages one by one, manually. At some point I had a typo in one of the POST messages, which looked like this:

"5:::{"name":"myServerMethod","args":[{"param1":43,"param2":{"member1":["TestValue1"],"member2":[]},"isNew":false}

This caused the exact same exception from this thread, and the server crashed. When I fixed the message above in my test script, everything worked fine:

"5:::{"name":"myServerMethod","args":[{"param1":43,"param2":{"member1":["TestValue1"],"member2":[]},"isNew":false}]}"

(note the trailing characters that were missing in my original message).

@bferreira
Copy link

Thank you for sharing ececon!

I'm still waiting for one of those strange messages to appear. What we did was to patch it and now we are waiting since we are getting messages from to many sources:
// this.error('parse', 'reconnect');
this.log.warn("Failed while parsing as JSON:'" + data + "'");

It's ok to get an error, just makes no sense that it crashes the server.

@daensch2k
Copy link

i had the same issue. But if you have something like
var server = http.createServer(function(req,res)){
drop the var in the http.createServer function... so try
response.writeHead(200, {
instead of
this.response.writeHead(200, { or server.response.writeHead(200, {

works fine for me in Node 0.10.25 - hope that can help

darrachequesne pushed a commit that referenced this issue Jul 4, 2024
This issue was closed.
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