Skip to content

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

@alexniculescu23

Description

@alexniculescu23

Describe the bug
In Chainlit 2.9.6, the shared thread endpoint can crash with an UnboundLocalError because user_can_view is only assigned inside the optional @cl.on_shared_thread_view hook. When the hook is not defined / not executed, the code still evaluates (not user_can_view) and raises:
UnboundLocalError: cannot access local variable 'user_can_view' where it is not associated with a value.

This reproduces on a fresh install using the installed wheel at .venv/lib/python3.12/site-packages/chainlit/server.py (around line 1019).

To Reproduce
Steps to reproduce the behavior:

  1. Install Chainlit 2.9.6 (fresh venv).
  2. Create a minimal Chainlit app without defining @cl.on_shared_thread_view.
  3. Start the server (e.g., chainlit run server.py).
  4. Call the shared thread endpoint (example):

Open the share URL in the browser

  1. See error.

Expected behavior
When @cl.on_shared_thread_view is not implemented, Chainlit should not crash. The request should be handled gracefully (e.g., return a 403/404 or a safe default behavior), and user_can_view should have a default value (e.g. False) before being checked.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: wsl Ubuntu 22.04
  • Browser chrome
  • Version latest

Suggested fix/workaround: set a safe default before the optional hook runs, e.g. user_can_view = False prior to evaluating (not user_can_view).

Stack trace:
ERROR: Exception in ASGI application
Traceback (most recent call last):
File ".../site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
result = await app(...)
File ".../site-packages/fastapi/applications.py", line 1054, in call
await super().call(scope, receive, send)
File ".../site-packages/starlette/routing.py", line 75, in app
response = await f(request)
File ".../site-packages/fastapi/routing.py", line 302, in app
raw_response = await run_endpoint_function(
File ".../site-packages/fastapi/routing.py", line 213, in run_endpoint_function
return await dependant.call(**values)
File ".../site-packages/chainlit/server.py", line 1019, in get_shared_thread
if (not user_can_view) and (not is_shared):
UnboundLocalError: cannot access local variable 'user_can_view' where it is not associated with a value

  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendPertains to the Python backend.bugSomething isn't workingneeds-triagestaleIssue has not had recent activity or appears to be solved. Stale issues will be automatically closed

    Type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions