-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: double client.end() hang (#2717)
* fix: double client.end() hang fixes #2716 `client.end()` will resolve early if the connection is already dead, rather than waiting for an "end" event that will never arrive. * fix: client.end() resolves when socket is fully closed
- Loading branch information
1 parent
adbe86d
commit 5703791
Showing
3 changed files
with
41 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict' | ||
const helper = require('../test-helper') | ||
|
||
const suite = new helper.Suite() | ||
|
||
// https://github.com/brianc/node-postgres/issues/2716 | ||
suite.testAsync('client.end() should resolve if already ended', async () => { | ||
const client = new helper.pg.Client() | ||
await client.connect() | ||
|
||
// this should resolve only when the underlying socket is fully closed, both | ||
// the readable part ("end" event) & writable part ("close" event). | ||
|
||
// https://nodejs.org/docs/latest-v16.x/api/net.html#event-end | ||
// > Emitted when the other end of the socket signals the end of | ||
// > transmission, thus ending the readable side of the socket. | ||
|
||
// https://nodejs.org/docs/latest-v16.x/api/net.html#event-close_1 | ||
// > Emitted once the socket is fully closed. | ||
|
||
// here: stream = socket | ||
|
||
await client.end() | ||
// connection.end() | ||
// stream.end() | ||
// ... | ||
// stream emits "end" | ||
// not listening to this event anymore so the promise doesn't resolve yet | ||
// stream emits "close"; no more events will be emitted from the stream | ||
// connection emits "end" | ||
// promise resolved | ||
|
||
// This should now resolve immediately, rather than wait for connection.on('end') | ||
await client.end() | ||
|
||
// this should resolve immediately, rather than waiting forever | ||
await client.end() | ||
}) |