Skip to content

fix: initialize user_can_view before conditional check#2775

Open
thecaptain789 wants to merge 1 commit intoChainlit:mainfrom
thecaptain789:fix/user-can-view-unbound-error
Open

fix: initialize user_can_view before conditional check#2775
thecaptain789 wants to merge 1 commit intoChainlit:mainfrom
thecaptain789:fix/user-can-view-unbound-error

Conversation

@thecaptain789
Copy link

@thecaptain789 thecaptain789 commented Feb 5, 2026

Summary

Fixes UnboundLocalError when the @cl.on_shared_thread_view hook is not defined.

Problem

In backend/chainlit/server.py, 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 at line 1019:

if (not user_can_view) and (not is_shared):

When the hook is not defined/registered, this caused:

UnboundLocalError: cannot access local variable 'user_can_view' where it is not associated with a value

Solution

Initialize user_can_view = False before 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

  1. Create a minimal Chainlit app without defining @cl.on_shared_thread_view
  2. Start the server
  3. Access a shared thread URL
  4. Verify no UnboundLocalError is raised

Related 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.

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
@dosubot dosubot bot added size:XS This PR changes 0-9 lines, ignoring generated files. bug Something isn't working labels Feb 5, 2026
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UnboundLocalError: user_can_view referenced before assignment when on_shared_thread_view hook is not set (2.9.6)

1 participant