-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
when streaming, the connection can time out #849
Comments
Looking at the node-postgres code, it is clear that this error is the result of a connection explicitly ending: https://github.com/brianc/node-postgres/blob/v4.3.0/lib/client.js#L175-L189 This would not be something under the purview of knex or pool2; you will likely need to investigate your network or database configuration, and/or verify there's no separate process potentially killing connections (pg_terminate_backend, for example) |
Apparently node-postgres emits errors on the query object when there is a query active, so the error binding on the client object is insufficient. Thanks pg. I'm not even sure where to bind that handler after reading the docs. |
So I suspect though that connection is being closed by knex or pool2, notice earlier around 5942 where pool2 attempts to reclaim the connection, i belive the unhangled error is just a symptom |
I rewrote my comments above a few times but that is already ruled out; pool2 cannot reclaim a connection that's "checked out" / idle; the log messages above are just routine cleanup of other resources that had been allocated and were cleaned out of the pool while the above was happening. |
brianc set me straight about pg's behavior: brianc/node-postgres#795 (comment) So it appears that the "stream mode" call to pg is missing an error handler somewhere. Presumably it's not using the callback interface, since then it wouldn't be streaming. I do notice here: https://github.com/tgriesser/knex/blob/master/lib/dialects/postgres/index.js#L107-L115 |
To the matter at hand, another look shows me this: Which seems to be indicating that 3/3 resources in the pool are available. Significantly, this includes the resource that should be checked out, because you're using it. Another oversight in the stream handling perhaps? |
Think I found the culprit: https://github.com/tgriesser/knex/blob/master/lib/runner.js#L84-L95 The stream-promise is being returned after the call to Promise.using, meaning that the connection is being cleaned up before the stream query is executed. It should be returning the runner.client.stream call inside of line 93, I think. I'll push a tweak shortly and hopefully you can verify that it behaves correctly. |
If you could, try the 'issue-849' branch and see if it solves your problem? I don't quite want to merge to master since I have no idea where or how to write a test for this in the current structure... |
To the other problem, the crash, it does not appear that pipes propagate errors, so we should definitely be binding an error event on the created query object, not on the proxy-stream, but I'm having trouble wrapping my head around all aspects of the flow here so I'm far from certain about how this should be handled. (Node does bind an error handler in the |
fix looks good, solves my problem, also as to errers and streams, you are correct they don't propagate errors, usual response is something like var destStream = getDestStream();
stream.on('error', function(e) {destStream.emit('error', e)}).pipe(destStream); |
Glad to hear it. I'm gonna leave this issue open until @tgriesser or someone with fuller knowledge of knex can address the dangling bits. |
I have a very large table in postgres, and if I consume it slowly enough it will time out, reproduce with
If I run that with DEBUG=* then eventually I get
The text was updated successfully, but these errors were encountered: