Claim the Document thread before running app code - #8693
Merged
Conversation
state._thread_id was only recorded in init_doc(), which runs after the app callable returns. During the build it was therefore None, so hold() concluded it was running off the Document's thread and, instead of unholding on exit, deferred the unhold to a next-tick callback. The Document stayed held for the rest of the build, and the deferred unhold then replayed the queued events after Bokeh had already constructed the ServerSession -- registering every session callback added after the hold a second time and raising "A callback of the same type has already been added with this ID". Recording the thread in Application.initialize_document sets the same value init_doc would, just early enough for user code to see it. Fixes holoviz#8691
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #8693 +/- ##
==========================================
- Coverage 86.15% 86.04% -0.11%
==========================================
Files 348 348
Lines 57156 57171 +15
==========================================
- Hits 49242 49195 -47
- Misses 7914 7976 +62 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
philippjfr
approved these changes
Jul 29, 2026
Contributor
Author
|
@philippjfr I am not sure why the CI run failed, could you have a look? |
philippjfr
approved these changes
Aug 1, 2026
Member
|
Thanks @SimonHeybrock, test failure was just flaky, merging. |
1 task
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
state._thread_idis only recorded ininit_doc(), which runs after the app callable returns. While the app callable is running it is thereforeNone, andhold()decides whether it was called off the Document's thread withthreaded = state._current_thread != state._thread_id— so it unconditionally concludes it is off-thread. Rather than unholding on exit it schedules_unholdas a next-tick callback and restores_hold, leaving the Document held for the rest of the build. Bokeh then constructs theServerSession, whose__init__registers every session callback already on the Document, and the deferred unhold afterwards replays the queuedSessionCallbackAddedevent — registering the same callback a second time.Every session opened by an app that registers a session callback after a
hold()block therefore logsValueError: A callback of the same type has already been added with this ID:Beyond the log noise,
DocumentCallbackManager.unhold()dispatches in a plain loop, so the exception drops every event still queued behind the offending one. In the app where I hit this that was 38 of the 39 events queued during the initial render.The Document's thread is knowable before user code runs, so
Application.initialize_documentnow claims it. That writes the same valueinit_docwrites moments later —init_docruns synchronously inside the same build — just early enough for the app callable to see it.init_dockeeps its own assignment, which still covers Panel objects embedded in a plain BokehApplication.The alternative I originally suggested on the issue, guarding the comparison with
state._thread_id is not None and ..., resolves the ambiguity by guessing that an unset thread id means "on-thread". That guess also fires where the thread id is legitimately never set — notably notebooks, whereinit_docreturns early because there is nosession_context— and would silently movehold()there onto a different path, so I went with making the thread id known instead.Fixes #8691
A question on #8619
This moves a build-time
hold()from thethreadedbranch of thefinallychain onto thenot state._connectedbranch, which drops the hold policy but leaves_held_eventspopulated for an unrelated laterunhold()to flush. Build-time holds behaved exactly that way before #8619 reordered the chain, so this restores the older behaviour rather than introducing something new — but was that reordering meant to change what happens to events queued while the app is still being built? Raised separately in #8692, since the leftover events outlive this PR either way.AI Disclosure
Tool & Model: Claude Code + Opus 5
Usage: Claude Code diagnosed the root cause against the Panel and Bokeh sources, chose where the fix should live, wrote the fix and the regression test, and ran the verification — the reproducer above before and after, the regression test confirmed failing on the parent commit, and the full non-UI test suite compared against a baseline run (no new failures; the 18 pre-existing ones are missing JS bundles and
bokeh_fastapiin my environment). I reported the original issue and reviewed the change.Checklist