Skip to content

Commit cf4f5c1

Browse files
fix: re-run app on hot reload before running on_kernel_start callbacks
This caused the tasks to keep a reference to the old function
1 parent cafe757 commit cf4f5c1

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

solara/server/app.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
import traceback
99
import warnings
1010
import weakref
11+
import html
1112
from enum import Enum
1213
from pathlib import Path
1314
from typing import Any, Dict, List, Optional, cast
1415

1516
import ipywidgets as widgets
1617
import reacton
1718
from reacton.core import Element, render
19+
from reacton import ipywidgets
1820

1921
import solara
2022
import solara.lifecycle
@@ -26,7 +28,7 @@
2628

2729
WebSocket = Any
2830
apps: Dict[str, "AppScript"] = {}
29-
thread_lock = threading.Lock()
31+
thread_lock = threading.RLock()
3032

3133
logger = logging.getLogger("solara.server.app")
3234

@@ -232,12 +234,22 @@ def check(self):
232234

233235
def run(self):
234236
self.check()
235-
if reload.reloader.requires_reload or self._first_execute_app is None:
237+
if self._first_execute_app is None:
236238
with thread_lock:
237-
if reload.reloader.requires_reload or self._first_execute_app is None:
238-
self._first_execute_app = None
239-
self._first_execute_app = self._execute()
240-
print("Re-executed app", self.name) # noqa
239+
if self._first_execute_app is None:
240+
try:
241+
self._first_execute_app = self._execute()
242+
print("Re-executed app", self.name) # noqa
243+
except Exception as e:
244+
error = ""
245+
error = "".join(traceback.format_exception(None, e, e.__traceback__))
246+
print(error, file=sys.stdout, flush=True) # noqa
247+
248+
error = html.escape(error)
249+
self._first_execute_app = ipywidgets.HTML(value=f"<pre>{error}</pre>", layout=ipywidgets.Layout(overflow="auto"))
250+
# We now ran the app again, might contain new imports
251+
252+
print("Failed to execute app, fix the error and save the file to reload") # noqa
241253
# We now ran the app again, might contain new imports
242254
patch.patch_heavy_imports()
243255

@@ -319,6 +331,9 @@ def reload(self):
319331
except Exception as e:
320332
logger.exception("Could not close render context: %s", e)
321333

334+
self._first_execute_app = None
335+
self.run()
336+
322337
# ask all contexts/users to reload
323338
for context in context_values:
324339
with context:

tests/unit/toestand_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,8 @@ def test_computed_reload(no_kernel_context):
13031303
module = route.module
13041304
assert text.widget.v_model == "3.0"
13051305
solara.server.reload.reloader.requires_reload = True
1306+
assert solara.server.reload.reloader.on_change is not None
1307+
solara.server.reload.reloader.on_change("notimportant.py")
13061308
kernel_context.restart()
13071309
solara.toestand.KernelStore._type_counter.clear()
13081310
c = app.run()

0 commit comments

Comments
 (0)