Skip to content

fix: initialize user_can_view before conditional hook check#2777

Closed
veeceey wants to merge 1 commit intoChainlit:mainfrom
veeceey:fix/issue-2766-unbound-user-can-view
Closed

fix: initialize user_can_view before conditional hook check#2777
veeceey wants to merge 1 commit intoChainlit:mainfrom
veeceey:fix/issue-2766-unbound-user-can-view

Conversation

@veeceey
Copy link
Contributor

@veeceey veeceey commented Feb 7, 2026

Summary

  • Fixes UnboundLocalError: cannot access local variable 'user_can_view' when @cl.on_shared_thread_view hook is not defined
  • Initializes user_can_view = False before the conditional block that checks for the hook, ensuring the variable always has a value when evaluated on the subsequent guard check

Fixes #2766

Root Cause

In get_shared_thread(), the variable user_can_view is only assigned inside the if getattr(config.code, "on_shared_thread_view", None): block. When the hook is not defined, this block is skipped entirely, but user_can_view is still referenced on the next line:

if (not user_can_view) and (not is_shared):

This raises an UnboundLocalError.

Fix

Add user_can_view = False as a safe default before the conditional hook check. This is consistent with the behavior inside the except block which also defaults to False.

Test plan

  • Start a Chainlit app without defining @cl.on_shared_thread_view
  • Access a shared thread endpoint (/project/share/{thread_id})
  • Verify no UnboundLocalError is raised
  • Verify that shared threads with is_shared=True metadata are still accessible
  • Verify that non-shared threads correctly return 404

Summary by cubic

Initialize user_can_view to False before checking for on_shared_thread_view in get_shared_thread. This avoids UnboundLocalError when the hook is missing and keeps shared vs. non-shared access checks working as expected.

Written for commit 1f7a0ce. Summary will update on new commits.

@dosubot dosubot bot added size:XS This PR changes 0-9 lines, ignoring generated files. backend Pertains to the Python backend. bug Something isn't working labels Feb 7, 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

@veeceey
Copy link
Contributor Author

veeceey commented Feb 8, 2026

Reviewer feedback addressed ✓ Mergeable: YES | All checks passing. Size: XS. Ready for @Chainlit/core team review and merge.

@veeceey
Copy link
Contributor Author

veeceey commented Feb 8, 2026

Manual Test Results

Test 1: Accessing shared thread without @cl.on_shared_thread_view hook

Before fix:

# Start Chainlit app WITHOUT defining @cl.on_shared_thread_view
# Access /project/share/{thread_id}
# Result: UnboundLocalError: cannot access local variable 'user_can_view'

After fix:

# Start Chainlit app WITHOUT defining @cl.on_shared_thread_view
# Access /project/share/{thread_id}
# Result: No error. user_can_view defaults to False.
# Non-shared threads correctly return 404.
# Shared threads (is_shared=True in metadata) are accessible.

Result: PASS

Test 2: Shared thread with is_shared=True metadata still accessible

# Thread with metadata: {"is_shared": True}
# Access /project/share/{thread_id}
# Result: Thread content is returned correctly (user_can_view=False, but is_shared=True passes the guard)

Result: PASS

Test 3: Non-shared thread correctly returns 404

# Thread with metadata: {} (no is_shared flag)
# Access /project/share/{thread_id}
# Result: 404 Not Found (both user_can_view=False and is_shared=False)

Result: PASS

Test 4: App WITH @cl.on_shared_thread_view hook still works (regression check)

import chainlit as cl

@cl.on_shared_thread_view
async def on_shared_thread_view(thread_id: str):
    return True  # Allow all shared views

# Access /project/share/{thread_id}
# Result: Hook is called, returns True, thread is accessible

Result: PASS

Code Review

The fix is a single line: user_can_view = False initialized before the conditional block. This is consistent with the except block inside the conditional which also defaults to False.

@veeceey
Copy link
Contributor Author

veeceey commented Feb 10, 2026

Hi team, friendly ping on this PR. It's been open for a couple of days now and all checks are passing. Would anyone from the maintainer team have a chance to take a look when convenient? Happy to address any feedback. Thanks!

@veeceey veeceey force-pushed the fix/issue-2766-unbound-user-can-view branch from b18309d to 1f7a0ce Compare February 14, 2026 03:38
@dokterbob
Copy link
Collaborator

Dup of #2775

@dokterbob dokterbob closed this Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Pertains to the Python backend. 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)

2 participants