Skip to content

Commit 5a67c8a

Browse files
fix: update session state management to handle closing state
1 parent 3d728af commit 5a67c8a

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

lib/client.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module.exports = function (dependencies) {
3535

3636
function Client(options) {
3737
this.isDestroyed = false;
38+
this.isSessionClosing = false;
3839
this.config = config(options);
3940
this.logger = defaultLogger;
4041
this.errorLogger = defaultErrorLogger;
@@ -91,11 +92,13 @@ module.exports = function (dependencies) {
9192
return;
9293
}
9394
if (!session.closed) {
95+
this.isSessionClosing = true;
9496
await new Promise(resolve => {
9597
session.close(() => {
9698
resolve();
9799
});
98100
});
101+
this.isSessionClosing = false;
99102
}
100103
this.destroySession(session);
101104
};
@@ -192,6 +195,7 @@ module.exports = function (dependencies) {
192195
// Connect manageChannelsSession.
193196
if (
194197
!this.manageChannelsSession ||
198+
this.isSessionClosing ||
195199
this.manageChannelsSession.closed ||
196200
this.manageChannelsSession.destroyed
197201
) {
@@ -243,7 +247,7 @@ module.exports = function (dependencies) {
243247
}
244248
} else {
245249
// Connect to standard session.
246-
if (!this.session || this.session.closed || this.session.destroyed) {
250+
if (!this.session || this.isSessionClosing || this.session.closed || this.session.destroyed) {
247251
try {
248252
await this.connect();
249253
} catch (error) {
@@ -345,11 +349,12 @@ module.exports = function (dependencies) {
345349
this.config
346350
));
347351

348-
if (this.logger.enabled) {
349-
this.session.on('connect', () => {
352+
this.session.on('connect', () => {
353+
this.isSessionClosing = false;
354+
if (this.logger.enabled) {
350355
this.logger('Session connected');
351-
});
352-
}
356+
}
357+
});
353358

354359
this.session.on('close', () => {
355360
if (this.errorLogger.enabled) {
@@ -424,11 +429,12 @@ module.exports = function (dependencies) {
424429
config
425430
));
426431

427-
if (this.logger.enabled) {
428-
this.manageChannelsSession.on('connect', () => {
432+
this.manageChannelsSession.on('connect', () => {
433+
this.isSessionClosing = false;
434+
if (this.logger.enabled) {
429435
this.logger('ManageChannelsSession connected');
430-
});
431-
}
436+
}
437+
});
432438

433439
this.manageChannelsSession.on('close', () => {
434440
if (this.errorLogger.enabled) {

0 commit comments

Comments
 (0)