Skip to content

Fix thread safety and race condition in Flask mode#3

Merged
b0x42 merged 1 commit intomainfrom
fix/critical-thread-safety
Feb 12, 2026
Merged

Fix thread safety and race condition in Flask mode#3
b0x42 merged 1 commit intomainfrom
fix/critical-thread-safety

Conversation

@b0x42
Copy link
Copy Markdown
Owner

@b0x42 b0x42 commented Feb 12, 2026

Summary

  • Initialize image_bytes before starting the Flask server to prevent AttributeError on early requests (race condition where browser hits /screen.png before the update loop runs)
  • Add threading.Lock to protect shared mutable state (image, draw, image_bytes) from concurrent access by the main thread, update loop thread, and Flask request handler thread
  • Remove overly broad try/except Exception in the /screen.png route now that the root cause is fixed

Test plan

  • All 99 existing tests pass
  • Manual: start the emulator in Flask mode and verify the first page load renders correctly (no 500 error)
  • Manual: rapidly call drawing methods while the Flask UI is open to verify no image corruption

🤖 Generated with Claude Code

Initialize image_bytes before starting the Flask server to prevent
AttributeError on early requests. Add threading.Lock to protect shared
mutable state (image, draw, image_bytes) from concurrent access by
the main thread, update loop, and Flask request handler.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@b0x42 b0x42 merged commit 5b30415 into main Feb 12, 2026
7 checks passed
@b0x42 b0x42 deleted the fix/critical-thread-safety branch February 12, 2026 14:20
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.

1 participant