Skip to content

ContextVar cannot be updated in the handle #20

Open

Description

@scorpiord

When I use browser-use and bubus in the fastAPI application, ContextVar cannot be updated in the handle

test code:

import asyncio
from contextvars import ContextVar

from pydantic import BaseModel


class User(BaseModel):
    userid: int
    name: str
    user_code: str


user_ctx: ContextVar[User] = ContextVar('user_ctx', default=None)
user_ctx1: ContextVar[User] = ContextVar('user_ctx1', default=None)

class CtxEvent(BaseEvent):
    ctx: Any


class MyShow:

    def __init__(self):
        self.user_event_bus = EventBus(name=f"UserAgent")
        self.user_event_bus.on(CtxEvent, self.test_show)

    async def test_show(self, event: CtxEvent):
        print(f"test_show:{id(asyncio.get_running_loop())}")
        print(event.event_id)
        user = user_ctx.get()
        print(user)
        user1 = user_ctx1.get()
        print(user1)
        print(event.ctx.get(user_ctx))
        print(event.ctx.get(user_ctx1))

    async def test_show_1(self):
        print(f"test_show_1:{id(asyncio.get_running_loop())}")
        id(asyncio.get_running_loop())
        user = user_ctx.get()
        print(user)
        user1 = user_ctx1.get()
        print(user1)

my_show_instance = MyShow()


@system_router.get("/test")
async def test(user_code,user_code1):
    user_ctx.set(User(user_code=user_code,userid=1,name="user"))
    await asyncio.create_task(test1(user_code,user_code1))

async def test1(user_code,user_code1):

    user_ctx1.set(User(user_code=user_code1,userid=1,name="user"))

    my_show_instance.user_event_bus.dispatch(CtxEvent(ctx=contextvars.copy_context()))
    # await my_show_instance.test_show_1()

Can we support switching the execution context to the dispatch event’s context when handle runs?

For example, have base_event support accepting a context, and then use the provided context when executing handle.

ctx = getattr(event, "ctx", None)

if inspect.iscoroutinefunction(handler):
    if ctx:
        handler_task = ctx.run(asyncio.create_task(handler(event)))
    else:
        handler_task = asyncio.create_task(handler(event))

    result_value: Any = await asyncio.wait_for(handler_task, timeout=event_result.timeout)

elif inspect.isfunction(handler) or inspect.ismethod(handler):
    if ctx:
        result_value: Any = ctx.run(handler, event)
    else:
        result_value: Any = handler(event)

Activity

  1. pirate commented on Nov 26, 2025

    @pirate
    Contributor

    My intuition is that the contexts would need to be nested or merged somehow, not just overridden because the whole point of our internal use of ContextVars is that events emitted inside handlers need to know their parent. Do you know if ctx.run() auto-stacks/nests/merges contexts?


    P.S. FYI I am no longer working at browser-use, but I forked bubus and am maintaining it + adding new features over here now:

    https://github.com/pirate/bubus

  2. scorpiord commented on Nov 28, 2025

    @scorpiord
    Author

    My intuition is that the contexts would need to be nested or merged somehow, not just overridden because the whole point of our internal use of ContextVars is that events emitted inside handlers need to know their parent. Do you know if ctx.run() auto-stacks/nests/merges contexts?

    P.S. FYI I am no longer working at browser-use, but I forked bubus and am maintaining it + adding new features over here now:

    https://github.com/pirate/bubus

    Thank you for your reply.

    I believe ctx.run() is meant to execute the specified method within the given contexts (ctx). When a user includes a context in an event, it indicates that they expect the corresponding handler to run within that specific context.

    P.S.:
    Will the bubus used by browser-use be the version that you continue to maintain (https://github.com/pirate/bubus)?

  3. pirate commented on Dec 8, 2025

    @pirate
    Contributor

    I've just fixed this issue in my fork in v1.7.1 (see commits 2db5914 and 1ca9709). ContextVars are now copied and stored at dispatch time and propagate to the handler correctly. To avoid memory leaks/stale db sessions lingering the context copy is deleted after all the event handlers finish executing.

    As for whether browser-use will update to use my fork, unfortunately I don't have an answer at the moment. I'm still trying to figure out the best approach going forward with them so I can keep pushing fixes as an outside contributor.

    For now if you need my fixes maybe you can try:

    uv pip uninstall bubus
    uv pip install git+https://github.com/pirate/bubus@1.7.1

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions