Skip to content

Commit

Permalink
fix: infinite await+infinite loop bug (#17)
Browse files Browse the repository at this point in the history
* fix comment

* fix

* fix await issue on socket close

* timeout await if stuck in loop due to incompatible server or closed socket
  • Loading branch information
lesleyrs authored Jun 27, 2024
1 parent afe3812 commit e2cad7e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/js/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3438,7 +3438,7 @@ class Game extends Client {
this.pressedContinueOption = true;
}
} else if (action === 1773) {
// loc examine
// inv obj examine
const obj: ObjType = ObjType.get(a);
let examine: string;

Expand Down Expand Up @@ -7816,7 +7816,7 @@ class Game extends Client {
}

if (count > this.npcCount) {
throw new Error(`eek! ${this.username} Too many npc!`);
throw new Error(`eek! ${this.username} Too many npcs`);
}

this.npcCount = 0;
Expand Down
8 changes: 7 additions & 1 deletion src/js/jagex2/io/ClientStream.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import LinkList from '../datastruct/LinkList';
import Linkable from '../datastruct/Linkable';
import {sleep} from '../util/JsUtil';

export type Socket = {
host: string;
Expand Down Expand Up @@ -217,7 +218,12 @@ class WebSocketReader {
private async readSlowByte(len: number): Promise<number> {
this.event = this.queue.removeHead() as WebSocketEvent | null;
while (this.total < len) {
await new Promise((resolve): ((value: PromiseLike<((data: WebSocketEvent | null) => void) | null>) => void) => (this.callback = resolve));
await Promise.race([
new Promise((resolve): ((value: PromiseLike<((data: WebSocketEvent | null) => void) | null>) => void) => (this.callback = resolve)),
sleep(2000).then((): void => {
throw new Error('WebSocketReader timed out or closed while reading.');
})
]);
}
return this.event ? this.event.read : this.readSlowByte(len);
}
Expand Down

0 comments on commit e2cad7e

Please sign in to comment.