Skip to content

Refactor hot reload logic into separate module#1365

Open
richard-to wants to merge 2 commits intomainfrom
claude/prioritize-issues-uS4Qq
Open

Refactor hot reload logic into separate module#1365
richard-to wants to merge 2 commits intomainfrom
claude/prioritize-issues-uS4Qq

Conversation

@richard-to
Copy link
Collaborator

Summary

Extracted hot reload functionality from mesop/bin/bin.py into a new dedicated module mesop/server/hot_reload.py to improve code organization and enable hot reload support in WSGI server environments.

Key Changes

  • New module: Created mesop/server/hot_reload.py containing:

    • execute_main_module() - executes the main app module with error handling
    • start_file_watcher() - starts a background daemon thread for file watching
    • fs_watcher() - filesystem watcher implementation using watchdog
    • ReloadEventHandler - handles file system events (modified, created, deleted)
    • Module tracking functions (clear_app_modules, add_app_module, remove_app_module)
  • Updated mesop/bin/bin.py:

    • Removed hot reload implementation details (now imported from mesop.server.hot_reload)
    • Simplified main function to call start_file_watcher() instead of managing threads directly
    • Removed unused imports (threading, time, watchdog, etc.)
    • Updated execute_main_module() call to pass prod_mode parameter
  • Enhanced mesop/server/wsgi_app.py:

    • Added watch_path parameter to create_wsgi_app() function
    • Enables hot reload support when running Mesop in WSGI servers (e.g., gunicorn, FastAPI)
    • Starts file watcher immediately on app creation if debug_mode=True and watch_path is provided
    • Added comprehensive docstring with usage example
  • Build configuration updates:

    • Added THIRD_PARTY_PY_WATCHDOG dependency definition in build_defs/defaults.bzl
    • Updated mesop/server/BUILD to include watchdog dependency and necessary imports
    • Updated mesop/bin/BUILD to depend on //mesop/server instead of individual modules

Implementation Details

  • The prod_mode parameter is now threaded through the hot reload pipeline to control error reporting behavior
  • Changed app_modules.remove() to app_modules.discard() for safer module removal
  • File watcher runs as a daemon thread, allowing graceful shutdown
  • Hot reload functionality is now reusable across different entry points (CLI and WSGI servers)

https://claude.ai/code/session_01U3KhAZacaA9FpVT1jzGeYn

claude added 2 commits March 15, 2026 23:40
…ervers

Extract file-watcher logic from bin.py into a new mesop/server/hot_reload.py
module so it can be shared by both the mesop CLI and the WSGI integration.

Add a `watch_path` parameter to `create_wsgi_app()`: when provided alongside
`debug_mode=True`, a background watchdog thread is started immediately so that
hot reloading works when Mesop is mounted inside FastAPI, Starlette, Gunicorn,
or any other WSGI-compatible server.

Usage:
    mesop_app = create_wsgi_app(debug_mode=True, watch_path=__file__)

Also add THIRD_PARTY_PY_WATCHDOG to build_defs/defaults.bzl and update the
server/BUILD and bin/BUILD targets accordingly.

https://claude.ai/code/session_01U3KhAZacaA9FpVT1jzGeYn
Dict keys that are Python tuples (e.g. (1, 2)) were not handled correctly
in two places:

1. diff_state: DeepDiff incorrectly expanded the tuple into multiple path
   segments, producing ["val1", 1, 2] instead of the correct single element
   ["val1", "(1, 2)"]. The fix normalises both state objects with a new
   _normalize_state_for_diff helper before passing them to DeepDiff. The
   helper converts any non-JSON-scalar dict key (tuple, frozenset, etc.) to
   its str() representation so DeepDiff emits an unambiguous, single path
   element like "(1, 2)".

2. serialize_dataclass: json.dumps raised TypeError for dicts with tuple
   keys because JSON only supports string keys. The same normalisation is
   now applied to the asdict() output before encoding.

The TypeScript applyStateDiff already handles arbitrary string path
elements correctly, so no frontend changes are required beyond a new test
that exercises the "(1, 2)" string-key path.

https://claude.ai/code/session_01U3KhAZacaA9FpVT1jzGeYn
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.

2 participants