Skip to content

Commit 1d5906f

Browse files
committed
fix(client): correctly catch some canceled promise
1 parent cc717e0 commit 1d5906f

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

client/lib/CoreCommandQueue.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,7 @@ export default class CoreCommandQueue {
5050
}
5151

5252
public clearPending(): void {
53-
while (this.items.length) {
54-
const next = this.items.shift();
55-
const cancel = new CanceledPromiseError(`Canceling pending ${next.command} command`);
56-
cancel.stack = next.stack;
57-
next.reject();
58-
}
53+
this.items.length = 0;
5954
}
6055

6156
// PRIVATE

commons/Queue.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export default class Queue {
3030

3131
public stop(): void {
3232
while (this.queue.length) {
33-
const next = this.queue.pop();
33+
const next = this.queue.shift();
34+
if (!next) continue;
3435

3536
this.reject(next, new CanceledPromiseError('Canceling Queue Item'));
3637
}

core/server/ConnectionToClient.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import ICoreEventPayload from '@secret-agent/core-interfaces/ICoreEventPayload';
1010
import IWaitForOptions from '@secret-agent/core-interfaces/IWaitForOptions';
1111
import IAgentMeta from '@secret-agent/core-interfaces/IAgentMeta';
1212
import Log from '@secret-agent/commons/Logger';
13+
import { CanceledPromiseError } from '@secret-agent/commons/interfaces/IPendingWaitEvent';
1314
import Session from '../lib/Session';
1415
import Tab from '../lib/Tab';
1516
import GlobalPool from '../lib/GlobalPool';
@@ -38,6 +39,8 @@ export default class ConnectionToClient extends TypedEventEmitter<{
3839
// json converts args to null which breaks undefined argument handlers
3940
const args = payload.args.map(x => (x === null ? undefined : x));
4041

42+
const session = meta?.sessionId ? Session.get(meta.sessionId) : undefined;
43+
4144
let data: any;
4245
let isError = false;
4346
try {
@@ -58,6 +61,10 @@ export default class ConnectionToClient extends TypedEventEmitter<{
5861
}
5962
}
6063
} catch (error) {
64+
// if we're closing, don't emit errors
65+
if ((this.isClosing || session?.isClosing) && error instanceof CanceledPromiseError) {
66+
return;
67+
}
6168
isError = true;
6269
data =
6370
error instanceof Error
@@ -69,10 +76,7 @@ export default class ConnectionToClient extends TypedEventEmitter<{
6976
: new Error(`Unknown error occurred ${error}`);
7077
}
7178

72-
let commandId: number;
73-
if (meta?.sessionId) {
74-
commandId = Session.get(meta.sessionId)?.sessionState?.lastCommand?.id;
75-
}
79+
const commandId = session?.sessionState?.lastCommand?.id;
7680

7781
const response: ICoreResponsePayload = {
7882
responseId: messageId,

0 commit comments

Comments
 (0)