File tree 3 files changed +41
-4
lines changed
test/integration/gh-issues
3 files changed +41
-4
lines changed Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ class Client extends EventEmitter {
37
37
this . _Promise = c . Promise || global . Promise
38
38
this . _types = new TypeOverrides ( c . types )
39
39
this . _ending = false
40
+ this . _ended = false
40
41
this . _connecting = false
41
42
this . _connected = false
42
43
this . _connectionError = false
@@ -144,6 +145,7 @@ class Client extends EventEmitter {
144
145
145
146
clearTimeout ( this . connectionTimeoutHandle )
146
147
this . _errorAllQueries ( error )
148
+ this . _ended = true
147
149
148
150
if ( ! this . _ending ) {
149
151
// if the connection is ended without us calling .end()
@@ -630,7 +632,7 @@ class Client extends EventEmitter {
630
632
this . _ending = true
631
633
632
634
// if we have never connected, then end is a noop, callback immediately
633
- if ( ! this . connection . _connecting ) {
635
+ if ( ! this . connection . _connecting || this . _ended ) {
634
636
if ( cb ) {
635
637
cb ( )
636
638
} else {
Original file line number Diff line number Diff line change @@ -108,9 +108,6 @@ class Connection extends EventEmitter {
108
108
}
109
109
110
110
attachListeners ( stream ) {
111
- stream . on ( 'end' , ( ) => {
112
- this . emit ( 'end' )
113
- } )
114
111
parse ( stream , ( msg ) => {
115
112
var eventName = msg . name === 'error' ? 'errorMessage' : msg . name
116
113
if ( this . _emitMessage ) {
Original file line number Diff line number Diff line change
1
+ 'use strict'
2
+ const helper = require ( '../test-helper' )
3
+
4
+ const suite = new helper . Suite ( )
5
+
6
+ // https://github.com/brianc/node-postgres/issues/2716
7
+ suite . testAsync ( 'client.end() should resolve if already ended' , async ( ) => {
8
+ const client = new helper . pg . Client ( )
9
+ await client . connect ( )
10
+
11
+ // this should resolve only when the underlying socket is fully closed, both
12
+ // the readable part ("end" event) & writable part ("close" event).
13
+
14
+ // https://nodejs.org/docs/latest-v16.x/api/net.html#event-end
15
+ // > Emitted when the other end of the socket signals the end of
16
+ // > transmission, thus ending the readable side of the socket.
17
+
18
+ // https://nodejs.org/docs/latest-v16.x/api/net.html#event-close_1
19
+ // > Emitted once the socket is fully closed.
20
+
21
+ // here: stream = socket
22
+
23
+ await client . end ( )
24
+ // connection.end()
25
+ // stream.end()
26
+ // ...
27
+ // stream emits "end"
28
+ // not listening to this event anymore so the promise doesn't resolve yet
29
+ // stream emits "close"; no more events will be emitted from the stream
30
+ // connection emits "end"
31
+ // promise resolved
32
+
33
+ // This should now resolve immediately, rather than wait for connection.on('end')
34
+ await client . end ( )
35
+
36
+ // this should resolve immediately, rather than waiting forever
37
+ await client . end ( )
38
+ } )
You can’t perform that action at this time.
0 commit comments