Skip to content

Commit e4b88df

Browse files
authored
check if socket is still disconnecting and allow connect (#6110)
* check if socket is still disconnecting and allow connect Fixes #6103. * check connectClock in teardown
1 parent 48b94eb commit e4b88df

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

assets/js/phoenix/socket.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export default class Socket {
126126
this.defaultEncoder = Serializer.encode.bind(Serializer)
127127
this.defaultDecoder = Serializer.decode.bind(Serializer)
128128
this.closeWasClean = false
129+
this.disconnecting = false
129130
this.binaryType = opts.binaryType || "arraybuffer"
130131
this.connectClock = 1
131132
if(this.transport !== LongPoll){
@@ -237,10 +238,14 @@ export default class Socket {
237238
*/
238239
disconnect(callback, code, reason){
239240
this.connectClock++
241+
this.disconnecting = true
240242
this.closeWasClean = true
241243
clearTimeout(this.fallbackTimer)
242244
this.reconnectTimer.reset()
243-
this.teardown(callback, code, reason)
245+
this.teardown(() => {
246+
this.disconnecting = false
247+
callback && callback()
248+
}, code, reason)
244249
}
245250

246251
/**
@@ -255,7 +260,7 @@ export default class Socket {
255260
console && console.log("passing params to connect is deprecated. Instead pass :params to the Socket constructor")
256261
this.params = closure(params)
257262
}
258-
if(this.conn){ return }
263+
if(this.conn && !this.disconnecting){ return }
259264
if(this.longPollFallbackMs && this.transport !== LongPoll){
260265
this.connectWithFallback(LongPoll, this.longPollFallbackMs)
261266
} else {
@@ -418,6 +423,7 @@ export default class Socket {
418423
onConnOpen(){
419424
if(this.hasLogger()) this.log("transport", `${this.transport.name} connected to ${this.endPointURL()}`)
420425
this.closeWasClean = false
426+
this.disconnecting = false
421427
this.establishedConnections++
422428
this.flushSendBuffer()
423429
this.reconnectTimer.reset()
@@ -450,13 +456,16 @@ export default class Socket {
450456
if(!this.conn){
451457
return callback && callback()
452458
}
459+
let connectClock = this.connectClock
453460

454461
this.waitForBufferDone(() => {
462+
if(connectClock !== this.connectClock){ return }
455463
if(this.conn){
456464
if(code){ this.conn.close(code, reason || "") } else { this.conn.close() }
457465
}
458466

459467
this.waitForSocketClosed(() => {
468+
if(connectClock !== this.connectClock){ return }
460469
if(this.conn){
461470
this.conn.onopen = function (){ } // noop
462471
this.conn.onerror = function (){ } // noop

0 commit comments

Comments
 (0)