Skip to content

Commit

Permalink
Ensure we do not unlock document when on the wrong thread (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored Mar 6, 2024
1 parent 1484f9c commit fe8598f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 37 deletions.
9 changes: 7 additions & 2 deletions lumen/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import tqdm # type: ignore

from panel.io.document import unlocked
from panel.io.state import state as pn_state
from panel.viewable import Viewer
from panel.widgets import Widget
from typing_extensions import Literal
Expand Down Expand Up @@ -301,8 +302,12 @@ def _update_data(self, *events: param.parameterized.Event, force: bool = False):
for f in self.filters+self.transforms+self.sql_transforms:
f._sync_refs()

with unlocked():
self.data = self._compute_data()
new_data = self._compute_data()
if pn_state._unblocked(pn_state.curdoc):
with unlocked():
self.data = new_data
else:
self.data = new_data
if state.config and state.config.on_update:
pn.state.execute(partial(state.config.on_update, self))
self._stale = False
Expand Down
42 changes: 9 additions & 33 deletions lumen/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import sys
import unicodedata

from contextlib import contextmanager
from functools import partial, wraps
from logging import getLogger
from subprocess import check_output
Expand All @@ -20,8 +19,7 @@
from jinja2 import DebugUndefined, Environment, Undefined
from packaging.version import Version
from pandas.core.dtypes.dtypes import CategoricalDtype
from panel import state
from panel.io.document import unlocked
from panel.io.state import state

log = getLogger(__name__)

Expand Down Expand Up @@ -142,19 +140,19 @@ def _j_getshell(x):
def _j_getheaders(x):
if isinstance(x, Undefined):
x = x._undefined_name
return pn.state.headers.get(x, '')
return state.headers.get(x, '')

def _j_getcookies(x):
if isinstance(x, Undefined):
x = x._undefined_name
return pn.state.cookies.get(x, '')
return state.cookies.get(x, '')

def _j_getoauth(x):
if isinstance(x, Undefined):
x = x._undefined_name
if pn.state.user_info is None:
if state.user_info is None:
return ''
return pn.state.user_info.get(x, '')
return state.user_info.get(x, '')

def expand_spec(pars, context={}, getenv=True, getshell=True, getheaders=True,
getcookies=True, getoauth=True):
Expand Down Expand Up @@ -308,14 +306,14 @@ def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
from .state import state
if state.config and state.config.on_error:
pn.state.execute(partial(state.config.on_error, e))
from .state import state as session_state
if session_state.config and session_state.config.on_error:
state.execute(partial(state.config.on_error, e))
if pn.config.notifications:
log.error(
f"{func.__qualname__!r} raised {type(e).__name__}: {e}"
)
pn.state.notifications.error(message.format(e=e))
state.notifications.error(message.format(e=e))
else:
raise e
return wrapper
Expand All @@ -325,28 +323,6 @@ def wrapper(*args, **kwargs):

return decorator

@contextmanager
def immediate_dispatch(doc=None):
"""
Utility to trigger immediate dispatch of events even when Document
events are currently on hold.
"""
doc = doc or state.curdoc

# Skip if not in a server context
if not doc or not doc._session_context:
yield
return

old_events = doc.callbacks._held_events
hold = doc.callbacks._hold
doc.callbacks._held_events = []
doc.callbacks.unhold()
with unlocked():
yield
doc.callbacks._hold = hold
doc.callbacks._held_events = old_events

def slugify(value, allow_unicode=False) -> str:
"""
Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated
Expand Down
4 changes: 2 additions & 2 deletions lumen/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from bokeh.models import NumeralTickFormatter # type: ignore
from packaging.version import Version
from panel.io.document import immediate_dispatch
from panel.pane.base import PaneBase
from panel.pane.perspective import (
THEMES as _PERSPECTIVE_THEMES, Plugin as _PerspectivePlugin,
Expand All @@ -35,8 +36,7 @@
from ..transforms.base import Transform
from ..transforms.sql import SQLTransform
from ..util import (
VARIABLE_RE, catch_and_notify, immediate_dispatch, is_ref,
resolve_module_reference,
VARIABLE_RE, catch_and_notify, is_ref, resolve_module_reference,
)
from ..validation import ValidationError

Expand Down

0 comments on commit fe8598f

Please sign in to comment.