Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Https.request sporadically fails with "Cannot call method 'emit' of undefined" error #670

Closed

Description

@twills

This is observed with node 0.4.0 on cygwin. I am calling https.request to connect to a server, and sporadically am experiencing the following exception:

node.js:116
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: Cannot call method 'emit' of undefined
at CleartextStream. (http.js:1135:9)
at CleartextStream.emit (events.js:42:17)
at Socket.onerror (tls.js:809:17)
at Socket.emit (events.js:42:17)
at Array. (net.js:799:27)
at EventEmitter._tickCallback (node.js:108:26)

I did some debugging and I believe that what is happening is that the server I am connecting to is sometimes not doing an orderly shutdown of the SSL conversation. This is causing an ECONNABORTED error at my node client. The catch is that the response has already been received and hence some cleanup of the node socket structure has already occurred within node, leaving it with nothing to emit the error event to.

At a code level, take a look at http.js. The crash I am seeing is happening at line 1148, where the code tries to emit the error event on the request object, which is null. It is null because, prior to the error event being emitted on the socket, the listener for the end event on the response (line 1244) was invoked, which cleans up the socket (detachSocket at line 1256). So the code as currently implemented is not expecting that an error event on the socket can be received after the full response has been received.

Activity

  1. ry commented on Feb 15, 2011

    @ry

    i think you're not listening for the error event.

  2. twills commented on Feb 15, 2011

    @twills
    Author

    I believe I am. My code basically looks like the following. Should I be doing something differently?

    req = https.request(options, fnCB);
    req.end();
    
    req.on('error', function(err) {
        console.log(err);
    });    
    
  3. webholics commented on Feb 17, 2011

    @webholics

    Same happens in my code and I'm also listening to the request error event.

  4. ry commented on Feb 18, 2011

    @ry

    please check again on v0.4 HEAD, we've made a lot of changes recently

  5. webholics commented on Feb 18, 2011

    @webholics

    great seems to work in current v0.4 HEAD!

  6. ry commented on Feb 18, 2011

    @ry

    closing. reopen if it persists.

  7. twills commented on Feb 18, 2011

    @twills
    Author

    I'm no longer seeing the issue with v0.4 HEAD either... Although I didn't have a reproducible test case to begin with, so I can't say with 100% certainty that it is fixed. It's not obvious to me from looking at the source that the recent code changes would have fixed this. Someone who is closer to the code I referenced above might want to take a look and try to deduce whether this issue could still happen with the latest code.

  8. twills commented on Mar 16, 2011

    @twills
    Author

    This issue is definitely still happening for me on v0.4 HEAD. The current stack trace follows. I wish I had a reproducible test case to provide, but the server I am connecting to is not under my control and I am not exactly sure what is causing this. My theory is still that it is due to the server occasionally not doing a proper shutdown of the SSL connection.

    TypeError: Cannot call method 'emit' of undefined
    at CleartextStream. (http.js:1167:9)
    at CleartextStream.emit (events.js:42:17)
    at Socket.onerror (tls.js:830:17)
    at Socket.emit (events.js:42:17)
    at Array.0 (net.js:799:27)
    at EventEmitter._tickCallback (node.js:108:26)

  9. webholics commented on Mar 17, 2011

    @webholics

    I have still the same problem while talking with a Python Gevent SSL server.

  10. webholics commented on Mar 17, 2011

    @webholics

    Addition: It works with the 0.4.2 release but not with the v4.0 head anymore.

  11. twills commented on Mar 17, 2011

    @twills
    Author

    So you are saying that you do not see the problem with node 0.4.2? If that is the case, I will try 0.4.2 and see if it is fixed for me as well.

  12. webholics commented on Mar 18, 2011

    @webholics

    Yes exactly try if it works for you with 0.4.2.

  13. reopened this on Mar 18, 2011
  14. ry commented on Mar 18, 2011

    @ry

    need a reproducible test. also we just landed an important fix
    66570c1

    please try again with v0.4 HEAD or v0.4.3 which I will release today.

  15. webholics commented on Mar 18, 2011

    @webholics

    No unfortunately not. Still not fixed for me while testing an https connection with a Python Gevent server. The server complains: "error: [Errno 104] Connection reset by peer"

  16. 23 remaining items

  17. webholics commented on Apr 14, 2011

    @webholics

    @marsch I played again with https://gist.github.com/915603 and added your return statement before assert(0); in http.js

    Now I'm able to catch the error with response.socket.on('error',..). It returns the following error:

    { stack: [Getter/Setter],
      arguments: undefined,
      type: undefined,
      message: 'ECONNRESET, Connection reset by peer',
      errno: 104,
      code: 'ECONNRESET',
      syscall: 'read' }
    

    Maybe this helps?

  18. added a commit that references this issue on Apr 14, 2011
    8417870
  19. ry commented on Apr 14, 2011

    @ry

    please try the above patch

  20. webholics commented on Apr 14, 2011

    @webholics

    Great, seems fixed for me now, too. I can't reproduce the error anymore.

  21. twills commented on Apr 15, 2011

    @twills
    Author

    I've updated to v0.4 HEAD and am still occasionally seeing this error. Stack trace follows. Once again, unfortunately I do not control the server side of the conversation, so I am not exactly sure what is happening or how to reproduce it, but it does seem that there is still an error condition out there which is not being handled.

    TypeError: Cannot call method 'emit' of undefined
    at CleartextStream. (http.js:1202:9)
    at CleartextStream.emit (events.js:64:17)
    at Socket.onerror (tls.js:888:17)
    at Socket.emit (events.js:64:17)
    at Array. (net.js:828:27)
    at EventEmitter._tickCallback (node.js:126:26)

  22. satyamshekhar commented on May 24, 2011

    @satyamshekhar

    I get the same error on node v0.4.6

    Stack trace: TypeError: Cannot call method 'emit' of undefined
    at CleartextStream. (http.js:1202:9)
    at CleartextStream.emit (events.js:64:17)
    at Socket.onerror (tls.js:888:17)
    at Socket.emit (events.js:64:17)
    at Array. (net.js:824:27)
    at EventEmitter._tickCallback (node.js:126:26)

  23. sh1mmer commented on Oct 24, 2011

    @sh1mmer

    Can someone try this with 0.5 HEAD? Otherwise I'd like to close it.

  24. aseemk commented on Nov 10, 2011

    @aseemk

    +1

  25. bnoordhuis commented on Feb 17, 2012

    @bnoordhuis
    Member

    Closing, stale (and fixed). If anyone is still experiencing issues, please report them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions