fastapi-events icon indicating copy to clipboard operation
fastapi-events copied to clipboard

Events With Multiple Local Handlers Can Be Interfered By Each Other

Open melvinkcx opened this issue 3 years ago • 3 comments

Today, an event with multiple local handlers registered can interfere with each other as all handlers are given the same shared copy of events.

For instance,

# anywhere in code

dispatch(Events.USER_CREATED, {"key_in_payload": 123})
# in handlers.py

@local_handler.register(event_name=Events.USER_CREATED)
async def handle_user_created(event: Event):
    _, payload = event
    payload.pop("key_in_payload")


@local_handler.register(event_name=Events.USER_CREATED)
async def handle_user_created_2(event: Event)
    _, payload = event
    payload["key_in_payload"]  # KeyError

Proposal

A copy of the payload should be passed to the handlers.

melvinkcx avatar Dec 06 '21 09:12 melvinkcx

The proposal will solve this issue on a small level, but when the payload size is big and there are multiple consumers who are consuming the dispatched event at the same time this will become a big issue it will block all the memory. Especially for long-running tasks or coroutines. A quick fix that comes into my mind is writing a wrapper inside dispatch to make the payload a frozen dict. multiple handlers could accept it, frozen dict won't change. if anyone wants to change any property of that dict they could create a separate copy of that dict or get items from that dict.

danielhasan1 avatar Aug 17 '22 13:08 danielhasan1

@danielhasan1 you're right. I haven't tried to address this issue because the payload can technically be of any type, not just dict. Either approach risks breaking the user's code if the payload contains values (custom objects, etc) that cannot be cloned or frozen.

I'm also not aware of any "standard" approach to making a dict immutable as PEP 416 was rejected and PEP 603 is still under drafting stage.

I think it's best we keep this unfixed for now.

melvinkcx avatar Aug 21 '22 13:08 melvinkcx