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

Queue'd Timer Callbacks still execute even after timer is cleared #135

Closed
kbuffington opened this issue Mar 10, 2021 · 4 comments
Closed
Labels
bug Something isn't working

Comments

@kbuffington
Copy link
Contributor

kbuffington commented Mar 10, 2021

I found some weird behavior that is an issue with FSM, but maybe on the actual SM side of the things? I encountered an issue where a queued setInterval callback (i.e. the interval had fired, but code had not been executed) had it's spawning timer cleared, and then afterwards the callback was still executed.

What's supposed to happen is that before the callback executes the timerId is checked to see if it's still valid, and if not it's discarded. See this SO answer. Here's the code snippet:

var counter = 0;
var timer = setInterval(function() {
  ++counter;
  console.log("timer fired:", counter);
  if (counter == 1) {
    setTimeout(busyWaitAndClear, 0);
  }
}, 500); // Every 500ms
function busyWaitAndClear() { // Obviously never really do this
  var done = Date.now() + 1000;
  while (done > Date.now()) {
    // wait
  }
  clearInterval(timer);
  console.log("timer cleared");
}

You can run that code snippet in any browser and the interval will only execute once. However if you paste that into a FSM panel, the interval will execute 3 times (the initial time and then two more queued callbacks while the busyWait function is spinning for a second). This completely threw me for a loop and I spent a couple hours debugging before I realized the queue'd callbacks were still executing after the clear. Not sure if this is fixable, but my workaround is super ugly so I figured I should let you know :)

image

@TheQwertiest
Copy link
Owner

Thanks, I'll take a look.

@TheQwertiest TheQwertiest added this to To do: current release in Spider Monkey Panel via automation Apr 9, 2021
@TheQwertiest TheQwertiest added the bug Something isn't working label Apr 9, 2021
@TheQwertiest TheQwertiest moved this from To do: current release to Done in Spider Monkey Panel Jun 24, 2021
@TheQwertiest
Copy link
Owner

Fixed

@TheQwertiest
Copy link
Owner

TheQwertiest commented Jul 11, 2021

@kbuffington , I think I'll have to revert the fix for this issue, because I don't think I can fix all the problems that this fix brings.
I'm working on the total rewrite of the event handling system which should fix this issue, but it'll take quite a bit of time and it will probably will bring more bugs =) (being a huge rewrite and all)

@TheQwertiest TheQwertiest reopened this Jul 11, 2021
@kbuffington
Copy link
Contributor Author

That's fine. I'll revert my revert of my fixes then :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development

No branches or pull requests

2 participants