Skip to content

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

Description

@iantocristian

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)
    })
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions