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

end msg to unopened socket #1779

Closed
roccomuso opened this issue Dec 2, 2018 · 5 comments
Closed

end msg to unopened socket #1779

roccomuso opened this issue Dec 2, 2018 · 5 comments

Comments

@roccomuso
Copy link

Connection.prototype.end = function () {
// 0x58 = 'X'
this.writer.add(emptyBuffer)
this._ending = true
return this.stream.write(END_BUFFER, () => {
this.stream.end()
})
}

pg module tries send end message and don't consider if socket is open, app will hang.

@charmander
Copy link
Collaborator

That code isn’t right (see #1386 (comment)), but what will hang?

@fanatid
Copy link

fanatid commented Dec 3, 2018

const pg = require('pg')

;(async () => {
  console.time('time')
  process.on('exit', () => console.timeEnd('time'))

  // connect to PG server which available only through VPN over UDP
  const client = new pg.Client({ connectionString: '...' })
  client.on('error', (err) => console.log(err))

  const tid = setTimeout(() => {
    console.log('timeout!')
    // client.end()
    // client.connection.stream.destroy()
  }, 1000)

  await client.connect()
  console.log('connected')
  clearTimeout(tid)

  client.end()
})().catch((err) => {
  console.error(err.stack || err)
  process.exit(1)
})

with client.connection.stream.destroy():

time node ./pg-end-bug.js 
timeout!
Error: Connection terminated unexpectedly
  ....
time: 1016.979ms

real	0m1.132s
user	0m0.115s
sys	0m0.019s

with client.end():

time node ./pg-end-bug.js 
timeout!
Error: connect ETIMEDOUT 10.130.14.135:5432
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1121:14)
time: 129074.137ms

real	2m9.185s
user	0m0.107s
sys	0m0.020s

@charmander
Copy link
Collaborator

So the request is for client.end() to cancel pending connections as well?

@fanatid
Copy link

fanatid commented Dec 3, 2018

Sorry for unclear description for example.
Yes, issue about pending connection, currently .end() do not terminate connection and as result process will be alive up to 120s (default timeout value in node).

@fanatid
Copy link

fanatid commented Jun 4, 2019

Writing to such socket do not throw error on node@12.3.1. PR #1847 partially solve this problem.

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

3 participants