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

Uncaught, unspecified 'error' event. #41

Closed
wavded opened this issue Aug 2, 2011 · 9 comments
Closed

Uncaught, unspecified 'error' event. #41

wavded opened this issue Aug 2, 2011 · 9 comments

Comments

@wavded
Copy link

wavded commented Aug 2, 2011

Not sure if I'm doing something wrong or something erronous is happening. This is a stack trace I get from my app running in production... don't have more than that. It is using the JS driver. Any ideas?

{"stack":"Error: Uncaught, unspecified 'error' event.
    at [object Object].emit (events.js:47:15)
    at [object Object].<anonymous> (/var/www/adn/node_modules/pg/lib/client.js:106:12)
    at [object Object].emit (events.js:64:17)
    at Socket.<anonymous> (/var/www/adn/node_modules/pg/lib/connection.js:47:12)
    at Socket.emit (events.js:64:17)
    at Socket._onReadable (net.js:678:14)
    at IOWatcher.onReadable [as callback] (net.js:177:10)","message":"Uncaught, unspecified 'error' event."}
@brianc
Copy link
Owner

brianc commented Aug 3, 2011

I am not sure where the exception is coming from or what query is causing it, but it is being emitted from node-postgres. Node-postgres client and query objects emit error events whenever an error is encountered. When an instance of EventEmitter emits and error event and it does not have any listeners it bubbles up to the process and eventually crashes node. This is by design. What you'll want to do is something like this:

process.on('uncaughtException', function(err) {
  //do something with the global error here...
  //for starters, log it out & send me the output in this github issue ;)
})

@wavded
Copy link
Author

wavded commented Aug 3, 2011

yep I've actually got that setup to email me when that event is triggered, which is what I pasted above. I put some more debugging in to see if I can get more information about the query when it happens again. Could it be the case that I may be trying to use more connections in my pool? I bumped that up.

@brianc
Copy link
Owner

brianc commented Aug 3, 2011

Ah sorry I misunderstood what you were originally saying about the connection. If you use more connections than Postgres allows you'll get a much clearer message from the postgres server saying connection denied like this:

{ length: 78,
  name: 'error',
  severity: 'FATAL',
  code: '53300',
  message: 'sorry, too many clients already',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  file: 'proc.c',
  line: '275',
  routine: 'InitProcess' }

Are you using the callback method of performing queries or the event emitter method? i.e.

this:

client.query('whatever', function(err, result) { /* handle result */});

or this:

var query = client.query('whatever');
query.on('error', function() { /* handle error */ });
query.on('row', function() { /* handle row */});

@wavded
Copy link
Author

wavded commented Aug 3, 2011

i am using the callback method.

@brianc
Copy link
Owner

brianc commented Aug 3, 2011

I'm guessing one of the open sockets in a client which may even be idling in the pool is receiving an error from the socket. Some sort of network blip or connectivity issue. I don't think it's actually related to node-postgres's behavior exactly. It's going to be hard to figure out what's going on without more information on the error. One thing you might want to do is iterate over every client in the pool and attach an 'error' event listener to it so you can intercept the error at a slightly lower level...something like this during your application boot up process:

for(var i = 0; i < pg.defaults.poolSize; i++) {
  pg.connect('your connection info', function(client, err) {
    client.on('error', function(err) {
      console.log(client); //this should dump pretty big json structure out with the state of the client, connection, & socket
      console.log(err);
    })
    client.query("SELECT pg_sleep(2)"); //this will make the client run a single 2 second query so this loop can actually be sure to get a reference to each client in the pool...as they will all be made active by the query
  });
}

This is a really hacky way to do this and makes me realize I need to look at the client-pool API and make it more user friendly. Anyways...this will allow you to attach an error handler to each client instance in the pool & might make debugging easier.

@wavded
Copy link
Author

wavded commented Aug 3, 2011

Looks like it was the too many client error, our postgres server wasn't allowing enough connections... sorry to bug ya, thx for looking into it though.

@wavded
Copy link
Author

wavded commented Aug 3, 2011

One question though. It appears that this module just grabs connections as needed instead of grabbing them all at once. Is that correct? And I'm assuming it releases them all on app shutdown?

@brianc
Copy link
Owner

brianc commented Aug 3, 2011

correct, it grabs them as needed. You have to manually shut the pool down at app shutdown by calling pg.end()

@brianc brianc closed this as completed Aug 3, 2011
@brianc
Copy link
Owner

brianc commented Aug 3, 2011

I'm glad you found a solution. You weren't bugging me in the slightest.

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

2 participants