Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Registering Nested Event Handlers, and Unexpected Firing Results #7197

Open
craigrs84 opened this issue Apr 4, 2024 · 0 comments
Open

Registering Nested Event Handlers, and Unexpected Firing Results #7197

craigrs84 opened this issue Apr 4, 2024 · 0 comments

Comments

@craigrs84
Copy link

Brief summary of issue / Description of requested feature:

When nesting a call to registerAnonymousEventHandler, a single call to raiseEvent("testEvent", 999) will fire both the parent and nested handlers simultaneously. This seems counter intuitive, and can lead to an unexpected infinite loop and stack overflow issue in more complex use cases. The example below is a trivial oversimplification only meant to demonstrate the problem.

local handlerId1
handlerId1 = registerAnonymousEventHandler("testEvent", function(event, i)
   cecho("HANDLER1: " .. tostring(i) .. "\n")
  
    local handlerId2
    handlerId2 = registerAnonymousEventHandler("testEvent", function(event, i)
        cecho("HANDLER2: " .. tostring(i) .. "\n")        
        killAnonymousEventHandler(handlerId1 or 0)    
        killAnonymousEventHandler(handlerId2 or 0)
    end)
    
end)

Steps to reproduce the issue / Reasons for adding feature:

  1. Call raiseEvent("testEvent", 999) a single time
  2. Both event handler 1 and event handler 2 are executed simultaneously for the single event that was fired.

Error output / Expected result of feature

I would expect that it only fires the outer/parent event handler for the first call to raiseEvent. A second call to raiseEvent should fire the inner event handler since it is registered after the first event is fired.

Extra information, such as the Mudlet version, operating system and ideas for how to solve / implement:

On your side, I would suggest cloning the list of registered event handlers before you iterate over them to process a raiseEvent call, so that they remain stable during this time, and so that it doesn't automatically process new event handlers added to the tail end of your list (which is what is currently occuring when the new event handler is added inside the parent event handler).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant