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

client.release and pitfall with client.end are not documented in pg.Client’s API #2101

Open
js-choi opened this issue Feb 10, 2020 · 1 comment

Comments

@js-choi
Copy link

@js-choi js-choi commented Feb 10, 2020

The API documentation for pg.Client does not document client.release. The method is documented elsewhere in the overview of pooling and the API documentation for pg.Pool (see #1302 (comment)), but, due to my inattentive blindness, I had missed that the examples used client.release, not client.end. It would be good if client.release itself was documented as a method on the API documentation for pg.Client, with its own section—even if it simply referred to the section about releaseCallback on the API documentation for pg.Pool.

I ran into this when I had assumed that I could call client.end to release a pool’s connection back to the pool, before I even had realized that client.release existed. Calling pool.end after calling client.end will hang the process (see #1414 (comment)). It would be good if this pitfall was also documented for pg.Client in client.end’s section:

const pg = require('pg');

async function f (callback) {
  const pool = new pg.Pool;
  const connection = await pool.connect();
  await callback(connection);
  await connection.end(); // This should be `await connection.release();`.
  await pool.end(); // Commenting this line would also prevent the hang.
}

(async () => {
  await f(async connection => {
    console.log('X');
  });
  console.log('Y'); // Never runs unless line 8 is commented.
})();
@js-choi js-choi changed the title client.release and pitfall with client.end are not documented in pg.Client client.release and pitfall with client.end are not documented in pg.Client’s API Feb 10, 2020
@brianc
Copy link
Owner

@brianc brianc commented Feb 11, 2020

I agree this is confusing. I waffled back and forth internally on monkey-patching over client.end on a pooled client to return it to the pool, versus closing it, since you almost never want to close a pooled client. I'll work on making the docs more explicit here. It might be useful to add a client.release noop function on the base client as well just to avoid it exploding if you call that on a non-pooled client. I also think calling client.end on a pooled client should throw a warning or something in your console....but I'll need to think about that bit a bit more. Anyways...I'll work on making the docs better...thanks for your feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.