Skip to content

Commit

Permalink
fix(server): patch Thread._bootstrap instead of Thread.run
Browse files Browse the repository at this point in the history
Not every thread will use the run method (it may inherit and override).
Instead, _bootstrap will always be used.
  • Loading branch information
maartenbreddels committed Mar 7, 2024
1 parent 81c681a commit a330118
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions solara/server/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def wrapper(abs_path):


Thread__init__ = threading.Thread.__init__
Thread__run = threading.Thread.run
Thread__bootstrap = threading.Thread._bootstrap # type: ignore


def WidgetContextAwareThread__init__(self, *args, **kwargs):
Expand All @@ -254,16 +254,16 @@ def WidgetContextAwareThread__init__(self, *args, **kwargs):
logger.debug(f"No context for thread {self}")


def Thread_debug_run(self):
def WidgetContextAwareThread__bootstrap(self):
if not hasattr(self, "current_context"):
return Thread__run(self)
return Thread__bootstrap(self)
if self.current_context:
kernel_context.set_context_for_thread(self.current_context, self)
shell = self.current_context.kernel.shell
shell.display_pub.register_hook(shell.display_in_reacton_hook)
try:
with pdb_guard():
Thread__run(self)
Thread__bootstrap(self)
finally:
if self.current_context:
shell.display_pub.unregister_hook(shell.display_in_reacton_hook)
Expand Down Expand Up @@ -358,7 +358,7 @@ def patch():
else:
raise RuntimeError("Could not find _instances on ipywidgets version %r" % ipywidgets.__version__)
threading.Thread.__init__ = WidgetContextAwareThread__init__ # type: ignore
threading.Thread.run = Thread_debug_run # type: ignore
threading.Thread._bootstrap = WidgetContextAwareThread__bootstrap # type: ignore
# on CI we get a mypy error:
# solara/server/patch.py:210: error: Cannot assign to a method
# solara/server/patch.py:210: error: Incompatible types in assignment (expression has type "classmethod[Any]",\
Expand Down

0 comments on commit a330118

Please sign in to comment.