From 2c806bf9947f4af4071a05b6b4efeeae97f70046 Mon Sep 17 00:00:00 2001 From: MayaTheShy Date: Fri, 20 Feb 2026 04:03:23 -0500 Subject: [PATCH] refactor: enhance state loop error handling with consecutive error tracking for improved resilience --- server/Turtle.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/server/Turtle.js b/server/Turtle.js index 798b6a4..922d19c 100644 --- a/server/Turtle.js +++ b/server/Turtle.js @@ -288,9 +288,8 @@ export class Turtle extends EventEmitter { /** * Run the current state's act() generator in a loop */ - async _runStateLoop() { + async _runStateLoop(consecutiveErrors = 0) { const state = this._state; - let consecutiveErrors = 0; const MAX_CONSECUTIVE_ERRORS = 5; try { @@ -312,10 +311,10 @@ export class Turtle extends EventEmitter { if (isTimeout && consecutiveErrors < MAX_CONSECUTIVE_ERRORS) { console.warn(`[Turtle ${this.id}] Timeout in ${state.name} (${consecutiveErrors}/${MAX_CONSECUTIVE_ERRORS}), retrying...`); - // Wait a bit then restart the state loop + // Wait a bit then restart the state loop, passing the error count await new Promise(r => setTimeout(r, 2000)); if (this._state === state) { - this._runStateLoop(); // Restart the loop with the same state + this._runStateLoop(consecutiveErrors); } } else { console.error(`[Turtle ${this.id}] State error in ${state.name}:`, error.message);