Skip to content

Commit d3dd4f4

Browse files
Copilotna-trium-144
andcommitted
Fix code review issues: prevent infinite recursion and add timeout to interrupt
Co-authored-by: na-trium-144 <100704180+na-trium-144@users.noreply.github.com>
1 parent 4730d8a commit d3dd4f4

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

app/terminal/javascript/runtime.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,22 @@ export function JavaScriptProvider({ children }: { children: ReactNode }) {
124124
// Create a new worker
125125
initializeWorker();
126126

127-
// Wait for initialization - use a different approach
128-
await new Promise<void>((resolve) => {
127+
// Wait for initialization with timeout
128+
const maxRetries = 50; // 5 seconds total
129+
let retries = 0;
130+
131+
await new Promise<void>((resolve, reject) => {
129132
const checkInterval = setInterval(() => {
133+
retries++;
134+
if (retries > maxRetries) {
135+
clearInterval(checkInterval);
136+
reject(new Error("Worker initialization timeout"));
137+
return;
138+
}
139+
130140
if (workerRef.current) {
131-
// Try to initialize and restore
132-
postMessage<InitPayloadFromWorker>({
141+
// Try to restore state
142+
postMessage<{ success: boolean }>({
133143
type: "restoreState",
134144
}).then(() => {
135145
clearInterval(checkInterval);

public/javascript.worker.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ async function runJavaScript(id, payload) {
4646
// Save the successfully executed command for state recovery
4747
executedCommands.push(code);
4848
} catch (e) {
49-
console.log(e);
49+
// Use self.console to avoid recursion with our console proxy
50+
self.console.log(e);
5051
if (e instanceof Error) {
5152
jsOutput.push({
5253
type: "error",
@@ -105,7 +106,8 @@ async function restoreState(id) {
105106
executedCommands.push(command);
106107
} catch (e) {
107108
// If restoration fails, we still continue with other commands
108-
console.error("Failed to restore command:", command, e);
109+
// Use self.console to avoid recursion with our console proxy
110+
self.console.error("Failed to restore command:", command, e);
109111
}
110112
}
111113

0 commit comments

Comments
 (0)