Skip to content

Fix: infinite loop crashes and autosave loss #3891#3896

Open
Nixxx19 wants to merge 13 commits intoprocessing:developfrom
Nixxx19:nityam/fix-infinite-loop-deletes-autosave-3891-clean
Open

Fix: infinite loop crashes and autosave loss #3891#3896
Nixxx19 wants to merge 13 commits intoprocessing:developfrom
Nixxx19:nityam/fix-infinite-loop-deletes-autosave-3891-clean

Conversation

@Nixxx19
Copy link
Contributor

@Nixxx19 Nixxx19 commented Feb 16, 2026

Fixes #3891

Summary

Fixes browser crashes and data loss from infinite loops. Adds localStorage crash recovery, improves loop detection (including multiple loops on same line), and provides clearer error messages.

Before: Many infinite loops caused browser hang/freeze as all loops executed simultaneously.

After: Execution stops immediately when multiple loops are detected (hitCount > 1), preventing hang even with 66 infinite loops. See screenshot below.

Screenshot 2026-02-16 at 4 02 32 PM

Changes

1. localStorage Backup System (Crash Recovery)

Saves code to localStorage on every change, restores on project load if backup is newer than server save. Prevents data loss when browser crashes from infinite loops.

  • Backup on every editor change (debounced)
  • Restore on load if local backup > server updatedAt
  • Auto-trigger save after restore
  • Clear backup after successful server save

2. Always Apply loopProtect

Previously skipped when JSHINT found errors, leaving users vulnerable while typing incomplete code. Now always applies loopProtect even if parsing fails.

  • Try loopProtect after decomment
  • Fallback to loopProtect on original code if needed
  • Still respects // noprotect opt-out

3. Detect Multiple Loops on Same Line

Consecutive loops like for(){}for(){} bypass protection. Regex detects and adds semicolons between them so each loop is wrapped individually.

  • Pattern: ((?:for|while)\s*\([^)]*\)\s*\{[^}]*\})((?:for|while)\s*\([^)]*\)\s*\{[^}]*\})
  • Insert semicolon: for(){}for(){}for(){}; for(){}
  • Handles empty bodies, while loops, 3+ consecutive loops

4. Improved Loop Detection & Messages

Simplified messages, immediate stop for multiple loops.

Logic:

  • Track hitCount and firstLine (reset after 1s)
  • Single loop: Wait 30ms, show "Infinite loop detected at line X. Stopping execution."
  • Multiple loops: Stop immediately (no delay) on second detection, show "Multiple infinite loops detected. Stopping execution."

Why 66 loops don't hang:

  • Stop on second loop detection (hitCount > 1), not after detecting all loops
  • Immediate throw error (no timeout, no waiting)
  • Prevents browser freeze from many simultaneous loops

5. Auto-refresh Fix

Removed isPlaying check so auto-refresh works regardless of play state, preventing stale saves.

I have verified that this pull request:

  • has no linting errors (npm run lint)
  • has no test errors (npm run test)
  • has no typecheck errors (npm run typecheck)
  • is from a uniquely-named feature branch and is up to date with the develop branch.
  • is descriptively named and links to an issue number, i.e. Fixes #3891
  • meets the standards outlined in the accessibility guidelines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Infinite loop deletes autosave

1 participant