TypeError: Cannot read property 'name' of null
First off, thanks for the library! Overall it's been incredibly performant and straightforward to use. I am running into one issue which may be an issue with the library, though it's a bit hard to follow the protocol for me so my usage may be off as well.
I'm running into an uncaught error during the parseComplete handler in client.js (https://github.com/brianc/node-postgres/blob/v6.0.1/lib/client.js#L131).
I am doing a direct connect against the pool and then utilizing pg-query-stream
pool.connect().then(function(client){
stream = client.query(new QueryStream(query, null));
stream.on('error', function(streamError){
stream.close(function(closeError){
// What to do with a close error? At this point stream is already hosed :shrug:
client.release(streamError);
}
});
stream.on('close', function(){
client.release();
});
});
At some point during my app's life cycle, I get an uncaught error
TypeError: Cannot read property 'name' of null
at null.<anonymous> (/var/app/current/node_modules/pg/lib/client.js:139:26)
at emit (events.js:107:17)
at Socket.<anonymous> (/var/app/current/node_modules/pg/lib/connection.js:121:12)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Socket.Readable.push (_stream_readable.js:126:10)
at TCP.onread (net.js:540:20)
So obviously activeQuery is becoming null, and I am trying to narrow down why I would receive a parseComplete event when the active query is null.
According to my logs, the stream is not emitting an error, and seems to be closing normally. Therefore it seems like the connection client is getting a readyForQuery or end event, and then right after that getting the parseComplete event. Any idea why this would be happening, or see any issues with my usage? Thanks for any help you can give! I'll keep looking into it as well.
I wonder, if the same error would occur within pg-promise, if you follow the example...
At least this would help ruling out some reasons of why this is happening...
@greghart interesting error - any way you could include the query & some data so I could try to reproduce it here? Maybe using generate_series or something if you need lots of data?
I'm having a similar issue. For me it seems to be related to parameterized queries.
.query('SELECT $1', ['a'])
Do you have any further findings @greghart
I may have found something promising. I tried to use a new client object with the original in the prototype chain. I did this because I didn't want client.release() to be called from child queries within a transaction.
I did something like this: (using lodash _.create)
const new_client = _.create(client, {
release: function () { /* noop */ }
})
new_client.query('SELECT 1')
I did this because I didn't want
client.release()to be called from child queries within a transaction.
Call client.release() in a predictable manner, then? Bluebird’s resource management might help.
I've been having the same issue but only on my live site, once every few hours. My logs would always show that I received 2 requests to run the same query within about 5 ms - always 1 after another (no other queries in between). I don't know if the parameters are the same because I don't log them.
One would then get executed and the second would crash. It would usually happen again within a minute if I attempted to restart that application without restarting all the others that communicated with it. Basically, some combination of requests caused the issue to appear.
I haven't been able to replicate this offline. To me at this point, it looks like a race condition and maybe some kind of reference error (destroying the activeQuery on the wrong client).
I switched to pg-native and the problem appears to have stopped (over 12 hours with no issues).
Having this issue. I had switched away from pg-native cause it was giving headache as well.
Only happens on first-start. The second time pg is used, the error goes away. The "race" condition is that it happens always at first.
So part of our production push right now is to hit the servers ourselves first before we let users use them so they don't get this error. Which is a hack of a fix.
Anyone was able to fix this?
Just had this issue occur with connect-pg-simple configured pretty much as described in the advanced example of https://github.com/voxpelli/node-connect-pg-simple#usage.
Do any of y'all have a small script I can use that will likely reproduce this? Also are you using pg or pg-native? This is really perplexing but really crappy & I want to get it fixed if possible. I've used node-postgres for years on many systems in production and never seen this...so I'm not sure what exactly it could be.
@nemo gah that fix is gross - sorry you have to do that. You're using normal pg and what exact version of node are you using?
My application (very rarely) has experienced this problem as well. We have unfortunately been unable to distill it down to a simple script/app that can reliably reproduce, but general observation so far seems to indicate this happens when there is a connection issue with the database.
@brianc : I can completely understand the need to be able to reproduce in order to fix - but what are your thoughts on a short term fix to at least check to make sure activeQuery is a valid object before trying to reference the name property? Given the inability for client applications to handle this error in any way... and its drastic consequences - would be great if some form of incremental improvement could be delivered...
https://github.com/brianc/node-postgres/blob/master/lib/client.js#L241 indicates access to the name attribute allows to avoid the client from re-parsing the query. I would much rather take extraneous reparsing when the alternative is uncaught exception 😇
@andyatmiami - what version of pg & pg-pool are you using? Are you using pg-cursor or pg-query-stream?
"pg": "6.1.2"
"pg-pool": "1.6.0"
"node": "6.11.1"
We use parameterized queries exclusively.
FWIW:
I have a dev I am working with this afternoon who has had minimal success reproducing this - not yet reliable and not yet distilled into a simple app to run. At a high level (which I am working on refining) - we have 1 wildly expensive query (takes seconds to run), and then hammer a high load of other queries which are not long-running in parallel. Sometimes (and this is with Postgres completely stable) we see the activeQuery error fire...
Yah that's wretched that its happening. I really hope you can figure out a reproducible test case. I'd also suggest trying with the most recent version of pg & pg-pool. They both received pretty substantial internals upgrades from 6.x -> 7.x & 1.x -> 2.x respectively.
@brianc : Thanks for responsiveness in issue - will post back any updates/learnings as they come available!
I get the same error and also sometimes a different stack which I am guessing points to the same root cause
self.activeQuery.handleCommandComplete(msg, con)
^
TypeError: Cannot read property 'handleCommandComplete' of null
at Connection.<anonymous> (/home/app/node_modules/pg/lib/client.js:239:21)
at emitOne (events.js:96:13)
at Connection.emit (events.js:188:7)
at Socket.<anonymous> (/home/app/node_modules/pg/lib/connection.js:118:12)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
at TCP.onread (net.js:547:20)
events.js:160
throw er; // Unhandled 'error' event
I see there was pull request from @spollack (https://github.com/brianc/node-postgres/pull/961) around this . @spollack, @jshepard it was mentioned in the PR that there was an another library causing this, do you mind sharing your findings ?
@vedant15 I think our issue was caused by a connection leak in pre 2.0 versions of node-any-db-pool. We ended up back porting the fix from 2.0 in a fork here
@vedant15 sorry i wish i had written down better notes on this. I can tell you however that we have been running PR #961 in prod over the past year, and as far as i recall, have had no recurrences of this crash.
@spollack @jshepard thanks for the quick response.
Were you guys also seeing this issue only during high loads and with statement timeouts ?
@vedant15 pretty sure we only saw it during statement timeouts.
ok @jshepard , I am able to reproduce this issue on local (not always), but only when I put a considerable load on the application running queries with statement timeouts .
Thanks for all the info !