Skip to content

Commit

Permalink
copy-from: fix interaction with pg optional timeout mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromew committed Sep 13, 2021
1 parent 25a0d0d commit b6c5c19
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ Since this isn't a module with tons of installs and dependent modules I hope we

## changelog

### version 6.0.2 - published 2021-09-13

- copy-from : fix interaction with `pg` optional timeout mechanism

### version 6.0.1 - published 2021-08-23

- Bugfix for node 14+. The order of _destroy / _final calls are different before and after node 14 which caused an issue with the COPY FROM _destroy implementation that appeared in version 6.0.0.
Expand Down
13 changes: 13 additions & 0 deletions copy-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ class CopyStreamQuery extends Writable {
connection.query(this.text)
}

callback() {
// this callback is empty but defining it allows
// `pg` to discover it and overwrite it
// with its timeout mechanism when query_timeout config is set
}

_write(chunk, enc, cb) {
this.chunks.push({ chunk: chunk, encoding: enc })
if (this._gotCopyInResponse) {
Expand Down Expand Up @@ -104,6 +110,9 @@ class CopyStreamQuery extends Writable {
}

handleError(e) {
// clear `pg` timeout mechanism
this.callback()

if (this.cb_destroy) {
const cb = this.cb_destroy
this.cb_destroy = null
Expand Down Expand Up @@ -142,6 +151,10 @@ class CopyStreamQuery extends Writable {
// Note: `pg` currently does not call this callback when the backend
// sends an ErrorResponse message during the query (for example during
// a CopyFail)

// clear `pg` timeout mechanism
this.callback()

this.cb_ReadyForQuery()
this.connection = null
}
Expand Down
32 changes: 30 additions & 2 deletions test/copy-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const { spawn } = require('child_process')
const copy = require('../').from

describe('copy-from', () => {
function getClient() {
const client = new pg.Client()
function getClient(config) {
const client = new pg.Client(config)
client.connect()
return client
}
Expand Down Expand Up @@ -159,6 +159,34 @@ describe('copy-from', () => {
query.end()
})

it('`pg` query_timeout should be properly canceled upon error - issue #125', (done) => {
const fromClient = getClient({ query_timeout: 500 })
fromClient.query('CREATE TEMP TABLE numbers(num int)')
const txt = 'COPY numbers FROM STDIN'
const query = copy(txt)
query.on('error', function (err) {
fromClient.end()
done()
})
fromClient.query(query)
query.write('A')
query.end()
})

it('`pg` query_timeout should be properly canceled upon success - issue #125', (done) => {
const fromClient = getClient({ query_timeout: 1000 })
fromClient.query('CREATE TEMP TABLE numbers(num int)')
const txt = 'COPY numbers FROM STDIN'
const query = copy(txt)
query.on('finish', function (err) {
fromClient.end()
done()
})
fromClient.query(query)
query.write('1')
query.end()
})

describe('stream compliance', () => {
describe('successful stream', () => {
it("emits 1 'finish' (writable stream)", (done) => {
Expand Down

0 comments on commit b6c5c19

Please sign in to comment.