Skip to content

Pool: release called twice for the same resource #111

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

Closed
senotrusov opened this issue Mar 21, 2012 · 3 comments
Closed

Pool: release called twice for the same resource #111

senotrusov opened this issue Mar 21, 2012 · 3 comments

Comments

@senotrusov
Copy link

pg = require('pg').native

conString = 'tcp://january@localhost/january' # password is stored in ~/.pgpass

pg.connect conString, (err, client) ->
  console.log err if err

  client.query "SELECT now() as now", (err, result) ->
    if err
      console.log err
    else
      console.log result.rows[0].now

      process.nextTick ->
        client.query "SELECT now() as now", (err, result) ->
          if err
            console.log err
          else
            console.log result.rows[0].now

That code produce no error indication if generic-pool logging is disabled (which is node-postgres default), but if I enable generic-pool logging and add console.log('drain'); into client.on('drain', function() in node-postgres/pg/lib/index.js then I've got the following output:

INFO pool tcp://january@localhost/january - dispense() clients=1 available=0
VERBOSE pool tcp://january@localhost/january - dispense() - creating obj - count=1
Wed, 21 Mar 2012 18:52:38 GMT
drain
VERBOSE pool tcp://january@localhost/january - timeout: 1332355988309
INFO pool tcp://january@localhost/january - dispense() clients=0 available=1
Wed, 21 Mar 2012 18:52:38 GMT
drain
ERROR pool tcp://january@localhost/january - release called twice for the same resource: Error
    at Object.release (/home/foo/Dropbox/workflow/january/node_modules/pg/node_modules/generic-pool/lib/generic-pool.js:282:61)
    at Connection.<anonymous> (/home/foo/Dropbox/workflow/january/node_modules/pg/lib/index.js:73:14)
    at Connection.emit (events.js:64:17)
    at Connection._pulseQueryQueue (/home/foo/Dropbox/workflow/january/node_modules/pg/lib/native/index.js:78:54)
    at Connection.<anonymous> (/home/foo/Dropbox/workflow/january/node_modules/pg/lib/native/index.js:161:18)
    at Connection.emit (events.js:64:17)

Also, no error occurs if I does not use process.nextTick. I use it to simulate async call to a different (not postgres) data source which may occurs in some cases.

So, am I need to call pg.connect upon each database query? Or maybe it is better to manually manage database connections? I don't like that two options - in a typical HTTP request serving environment maybe that issue can be somehow resolved on your library level?

Thanks.

@brianc
Copy link
Owner

brianc commented Mar 21, 2012

Please see the wiki under Client -> pauseDrain / resumeDrain

https://github.com/brianc/node-postgres/wiki/Client

Also, this code:

https://github.com/brianc/node-postgres/blob/master/lib/client.js#L198

You need to call client.pauseDrain() and client.resumeDrain() respectively if you're going to run one query, do some other async stuff, and then run another query.

This is especially important if you're using the pool and want to do something like this:

  • get a client
  • start a transaction
  • run a query
  • read a file
  • run another query

In an async nature.

@brianc brianc closed this as completed Mar 21, 2012
@senotrusov
Copy link
Author

I saw pauseDrain() in wiki, but Brian, think of another question - is it acceptable if my code causes an error somewhere deep in pool and there are no indication of that? No exceptions, no errors, just a line to the disabled by default log? Are the other issues to consider?

@brianc
Copy link
Owner

brianc commented Mar 21, 2012

node-pool is logging an error condition but not propagating that error outwards. If you look at https://github.com/brianc/node-postgres/blob/master/lib/index.js#L30 you'll see there's no place there where I'm suppressing or not propagating any errors from node-pool. It is handy to know returning a resource to the pool twice results in an error...but whether or not to actually emit an error event (or something similar) about this would be left up to the node-pool developer to decide. I think it might be a nice feature for the pool to actually emit an error instead of just logging it - I could pass that error up to user code and quell the concern you're raising here.

brianc pushed a commit that referenced this issue Dec 27, 2019
* Test queued checkout after a connection failure

Co-authored-by: Johannes Würbach <[email protected]>

* Fix queued checkout after a connection failure

Co-authored-by: Johannes Würbach <[email protected]>
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