esper icon indicating copy to clipboard operation
esper copied to clipboard

`remove_handler` does not remove a registered handler

Open bd-jczarnowski opened this issue 1 year ago • 4 comments

Describe the bug Calling remove_handler does not remove a previously registered handler.

To Reproduce Steps to reproduce the behavior, or sample code that shows the bug.

def test_remove_handler():
    import esper

    handler_called = False

    def handler(_):
        nonlocal handler_called
        handler_called = True

    esper.set_handler("foo", handler)
    esper.remove_handler("foo", handler)

    esper.dispatch_event("foo", None)  # Dispatch the event to trigger the handler
    esper.process()

    assert not handler_called, "Handler was not removed correctly."

Expected behavior Handler is removed

Development environment:

  • Python 3.10
  • Esper 3.3

Additional context Add any other context about the problem here.

I think the comparison of the function passed to remove_handler with the weakref to the function stored in the registry fails.

bd-jczarnowski avatar Mar 26 '25 15:03 bd-jczarnowski

Thanks for opening the ticket. I think your analysis is correct. I'll make a fix for this soon.

benmoran56 avatar Mar 27 '25 01:03 benmoran56

remove_handler has a guard check that compares the passed func to its weakref, however that comparison would always fail. I could make a PR to make a weakref from the passed func, and use that in the guard check. If you agree with the change

maxlebedev avatar Apr 01 '25 13:04 maxlebedev

HI @maxlebedev, this is what I was planning to do, but havn't had a chance to knock it out. If you are up to it, please go ahead.

benmoran56 avatar Apr 02 '25 02:04 benmoran56

Just made a PR https://github.com/benmoran56/esper/pull/107

maxlebedev avatar Apr 02 '25 14:04 maxlebedev

Just realized we still had this open, even though it's merged and released. Thanks again!

benmoran56 avatar Sep 07 '25 06:09 benmoran56