Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure errors in sync periodic callbacks are logged. #7665

Merged
merged 1 commit into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions panel/io/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,9 @@ def _exec_callback(self, post=False):
if self.count is not None and self.counter > self.count:
self.stop()
cb = self.callback() if self.running else None
except Exception:
cb = None
if post:
self._post_callback()
finally:
if post:
self._post_callback()
return cb

def _post_callback(self):
Expand Down Expand Up @@ -133,9 +132,6 @@ async def _periodic_callback(self):
await cb
else:
await cb
except Exception:
log.exception('Periodic callback failed.')
raise
finally:
self._post_callback()

Expand Down
44 changes: 44 additions & 0 deletions panel/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,50 @@ def app():
assert state.cache['count'] == count


async def _async_erroring_cb():
raise ValueError("An erroring callback")

def _sync_erroring_cb():
raise ValueError("An erroring callback")

@pytest.mark.parametrize(
'threadpool, cb', [
(2, _async_erroring_cb),
(None, _async_erroring_cb),
(2, _sync_erroring_cb),
(None, _sync_erroring_cb)
])
def test_server_periodic_callback_error_logged(caplog, server_implementation, threadpool, cb):
"""Ensure errors in periodic callbacks appear in the logs"""
loggers_to_check = [logging.getLogger(x) for x in ('panel','bokeh')]
orig_level_propagate = [(l.level,l.propagate) for l in loggers_to_check]
orig_threads = config.nthreads
repeats = 3

try:
config.nthreads = threadpool
for l in loggers_to_check:
l.propagate = True
l.setLevel(logging.WARNING)

def app():
state.add_periodic_callback(cb, 100, repeats)
def loaded():
state._schedule_on_load(state.curdoc, None)
state.execute(loaded, schedule=True)
return Row()

serve_and_request(app)
time.sleep(1)
num_errors_logged = caplog.text.count('ValueError: An erroring callback')
assert num_errors_logged == repeats
finally:
for l,level_prop in zip(loggers_to_check,orig_level_propagate):
l.setLevel(level_prop[0])
l.propagate = level_prop[1]
config.nthreads = orig_threads


def test_server_schedule_repeat(server_implementation):
state.cache['count'] = 0
def periodic_cb():
Expand Down
Loading