Skip to content

Commit 9614ac8

Browse files
committed
Add guard checks for isInPushedAnalysis to prevent undefined errors
Fixes race condition where async socket events fire after isInPushedAnalysis is deleted during object destruction.
1 parent 6fdf3de commit 9614ac8

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

src/Goban/OGSConnectivity.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ export abstract class OGSConnectivity extends GobanInteractive {
548548
}
549549
const move = move_obj.move;
550550

551-
if (this.isInPushedAnalysis()) {
551+
if (this.isInPushedAnalysis !== undefined && this.isInPushedAnalysis()) {
552552
this.leavePushedAnalysis();
553553
}
554554

@@ -1166,7 +1166,7 @@ export abstract class OGSConnectivity extends GobanInteractive {
11661166
(this.isPlayerOwner() && msg_override && msg_override.controller)) &&
11671167
this.done_loading_review
11681168
) {
1169-
if (this.isInPushedAnalysis()) {
1169+
if (this.isInPushedAnalysis !== undefined && this.isInPushedAnalysis()) {
11701170
return;
11711171
}
11721172

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (C) Online-Go.com
3+
*/
4+
5+
(global as any).CLIENT = true;
6+
7+
import WS from "jest-websocket-mock";
8+
import { TestGoban } from "../../src/index";
9+
import { GobanSocket } from "engine";
10+
11+
let last_port = 38880;
12+
13+
async function createGobanWithSocket(game_id: number): Promise<any[]> {
14+
const port = ++last_port;
15+
const server = new WS(`ws://localhost:${port}`, { jsonProtocol: true });
16+
const client = new GobanSocket(`ws://localhost:${port}`, { dont_ping: true, quiet: true });
17+
const instance = new TestGoban({ game_id, server_socket: client });
18+
(instance as any).post_config_constructor();
19+
await server.connected;
20+
return [server, client, instance];
21+
}
22+
23+
describe("OGSConnectivity isInPushedAnalysis guard", () => {
24+
test("move handler does not log error when isInPushedAnalysis is deleted", async () => {
25+
const [server, client, instance] = await createGobanWithSocket(123);
26+
27+
const moveHandler = (instance as any).socket_event_bindings.find(
28+
(binding: any) => binding[0] === "game/123/move",
29+
)?.[1];
30+
31+
delete (instance as any).isInPushedAnalysis;
32+
33+
const consoleErrorSpy = jest.spyOn(console, "error").mockImplementation(() => {});
34+
moveHandler({ game_id: 123, move: "aa" });
35+
36+
expect(consoleErrorSpy).not.toHaveBeenCalledWith(
37+
expect.objectContaining({
38+
message: expect.stringContaining("isInPushedAnalysis is not a function"),
39+
})
40+
);
41+
42+
consoleErrorSpy.mockRestore();
43+
client.disconnect();
44+
server.close();
45+
});
46+
47+
test("syncReviewMove does not throw when isInPushedAnalysis is deleted", () => {
48+
const instance = new TestGoban({ review_id: 123 });
49+
50+
(instance as any).done_loading_review = true;
51+
(instance as any).isPlayerController = () => true;
52+
(instance as any).socket = { send: () => {} };
53+
54+
delete (instance as any).isInPushedAnalysis;
55+
56+
expect(() => {
57+
(instance as any).syncReviewMove();
58+
}).not.toThrow();
59+
60+
instance.destroy();
61+
});
62+
});

0 commit comments

Comments
 (0)