node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

Using pg-query-stream with query_timeout set, timeout timer doesn't get released

Open iantocristian opened this issue 1 year ago • 2 comments

If I add query_timeout to the example:

const pg = require('pg')
var pool = new pg.Pool({  
  query_timeout: 10000,
})
const QueryStream = require('pg-query-stream')
const JSONStream = require('JSONStream')

//pipe 1,000,000 rows to stdout without blowing up your memory usage
pool.connect((err, client, done) => {
  if (err) throw err
  const query = new QueryStream('SELECT * FROM generate_series(0, $1) num', [1000])
  const stream = client.query(query)
  //release the client when the stream is finished
  stream.on('end', done)
  stream.pipe(JSONStream.stringify()).pipe(process.stdout)
})

readTimeoutTimer is not released because the query callback isn't called.

Process hangs and does not exit until readTimeout expires. Then it will throw the timeout error.

This seems to an oversight in the implementation, but I might be missing something, not sure where the callback should be called to release the timer.

Maybe in _destroy call the callback?

  public _destroy(_err: Error, cb: Function) {
    this.callback?(_err);
    this.cursor.close((err?: Error) => {
      cb(err || _err)
    })
  }

iantocristian avatar May 17 '24 06:05 iantocristian

I have a similar issue, where the query_timeout throw an error in the line https://github.com/brianc/node-postgres/blob/e25428c8dceb86eb4277af90155295fd0188abce/packages/pg/lib/client.js#L533 because when we use pg-query-stream in the line https://github.com/brianc/node-postgres/blob/e25428c8dceb86eb4277af90155295fd0188abce/packages/pg/lib/client.js#L513 is received a Readable stream and then query.callback is undefined after it. We need of some refactoring in this part of the code.

BrenoLopez avatar May 24 '24 13:05 BrenoLopez

Is any solution for this problem?

ArturAralin avatar Aug 10 '24 06:08 ArturAralin