fix: initialize user_can_view before conditional check#2775
Open
thecaptain789 wants to merge 1 commit intoChainlit:mainfrom
Open
fix: initialize user_can_view before conditional check#2775thecaptain789 wants to merge 1 commit intoChainlit:mainfrom
thecaptain789 wants to merge 1 commit intoChainlit:mainfrom
Conversation
Fix UnboundLocalError when on_shared_thread_view hook is not defined. The user_can_view variable was only assigned inside the optional on_shared_thread_view hook block, but was used unconditionally in the subsequent check. When the hook is not defined, this caused: UnboundLocalError: cannot access local variable 'user_can_view' where it is not associated with a value This fix initializes user_can_view to False before the conditional block, ensuring the variable is always defined. Fixes Chainlit#2766
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.
Summary
Fixes
UnboundLocalErrorwhen the@cl.on_shared_thread_viewhook is not defined.Problem
In
backend/chainlit/server.py, theuser_can_viewvariable was only assigned inside the optionalon_shared_thread_viewhook block, but was used unconditionally in the subsequent check at line 1019:When the hook is not defined/registered, this caused:
Solution
Initialize
user_can_view = Falsebefore the conditional block, ensuring the variable is always defined regardless of whether the hook exists.Changes
+ user_can_view = False if getattr(config.code, "on_shared_thread_view", None):Testing
@cl.on_shared_thread_viewUnboundLocalErroris raisedRelated Issue
Fixes #2766
Summary by cubic
Initialize user_can_view to False before the shared-thread access check to prevent UnboundLocalError when @cl.on_shared_thread_view is not defined. This ensures apps without the hook handle shared thread views without crashing.
Written for commit 5e5e00c. Summary will update on new commits.