Skip to content

Commit cba561e

Browse files
committed
fix: improve browser recovery logic with enhanced isSystemBusy management and error handling
1 parent 81bebbf commit cba561e

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/core/RequestHandler.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ class RequestHandler {
5959

6060
/**
6161
* Handle browser recovery when connection is lost
62+
*
63+
* Important: isSystemBusy flag management strategy:
64+
* - Direct recovery (recoveryAuthIndex > 0): We manually set and reset isSystemBusy
65+
* - Switch to next account (recoveryAuthIndex = 0): Let switchToNextAuth() manage isSystemBusy internally
66+
* - This prevents the bug where isSystemBusy is set here, then switchToNextAuth() checks it and returns "already in progress"
67+
*
6268
* @returns {boolean} true if recovery successful, false otherwise
6369
*/
6470
async _handleBrowserRecovery(res) {
@@ -77,19 +83,24 @@ class RequestHandler {
7783
this.logger.error(
7884
"❌ [System] Browser WebSocket connection disconnected! Possible process crash. Attempting recovery..."
7985
);
80-
this.authSwitcher.isSystemBusy = true;
86+
8187
const recoveryAuthIndex = this.currentAuthIndex || 0;
82-
let wasRecoveryAttempt = false;
88+
let wasDirectRecovery = false;
8389
let recoverySuccess = false;
8490

8591
try {
8692
if (recoveryAuthIndex > 0) {
87-
wasRecoveryAttempt = true;
93+
// Direct recovery: we manage isSystemBusy ourselves
94+
wasDirectRecovery = true;
95+
this.authSwitcher.isSystemBusy = true;
96+
this.logger.info(`[System] Set isSystemBusy=true for direct recovery to account #${recoveryAuthIndex}`);
97+
8898
await this.browserManager.launchOrSwitchContext(recoveryAuthIndex);
8999
this.logger.info(`✅ [System] Browser successfully recovered to account #${recoveryAuthIndex}!`);
90100
recoverySuccess = true;
91101
} else if (this.authSource.availableIndices.length > 0) {
92102
this.logger.warn("⚠️ [System] No current account, attempting to switch to first available account...");
103+
// Don't set isSystemBusy here - let switchToNextAuth manage it
93104
const result = await this.authSwitcher.switchToNextAuth();
94105
if (!result.success) {
95106
this.logger.error(`❌ [System] Failed to switch to available account: ${result.reason}`);
@@ -107,7 +118,7 @@ class RequestHandler {
107118
} catch (error) {
108119
this.logger.error(`❌ [System] Recovery failed: ${error.message}`);
109120

110-
if (wasRecoveryAttempt && this.authSource.availableIndices.length > 1) {
121+
if (wasDirectRecovery && this.authSource.availableIndices.length > 1) {
111122
this.logger.warn("⚠️ [System] Attempting to switch to alternative account...");
112123
try {
113124
const result = await this.authSwitcher.switchToNextAuth();
@@ -135,7 +146,11 @@ class RequestHandler {
135146
recoverySuccess = false;
136147
}
137148
} finally {
138-
this.authSwitcher.isSystemBusy = false;
149+
// Only reset if we set it (for direct recovery attempt)
150+
if (wasDirectRecovery) {
151+
this.logger.info("[System] Resetting isSystemBusy=false in recovery finally block");
152+
this.authSwitcher.isSystemBusy = false;
153+
}
139154
}
140155

141156
return recoverySuccess;

0 commit comments

Comments
 (0)