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

Callback executing twice? #127

Closed
timwis opened this issue Jul 22, 2016 · 6 comments
Closed

Callback executing twice? #127

timwis opened this issue Jul 22, 2016 · 6 comments

Comments

@timwis
Copy link

timwis commented Jul 22, 2016

Originally reported in choojs/choo#193:

I have an effect that calls multiple effects using run-waterfall. The "sub" effects use the choo/http module to make an http request. When the requests encounter an error, they return done('Bad request'). This error message bubbles up to the parent effect, which calls done(err) itself, bubbling it up to the application event handler, which logs an uncaught error.

The problem is the uncaught error is being logged twice. Here's a test case to demonstrate. If you switch out the http call with the setTimeout call, it only logs once. Also if you comment out the done(err) in the parent effect, it only logs once.

Am I crazy or is this unexpected behaviour?

@yoshuawuyts investigated and believes xhr is calling the callback twice. Would it be helpful to alter the test case to demonstrate it without choo? Or is this enough to diagnose?

Thanks for your help

@naugtur
Copy link
Owner

naugtur commented Jul 22, 2016

I'll try to diagnose based on what you already have and lat you know in an hour.

@yoshuawuyts
Copy link

Quick note: this only happens when used in choo, but not without it. There's no obvious interaction between the two libs tho so I've got no idea why xhr is calling back twice

@naugtur
Copy link
Owner

naugtur commented Jul 22, 2016

I couldn't reproduce with just xhr either. In debugger now.
Sourcemaps in the testcase would help... ;)

@naugtur
Copy link
Owner

naugtur commented Jul 22, 2016

Observation:

first call goes through this in callstack:

callback(...)
callback=noop

second call does

callback(...)

and is not invoking a noop function.

Possible explanations:

  • callback is somehow calling xhr again or changing state of the XMLHttpRequest object being handled before it returns
  • I'm going insane

[edit]

http('http://google.com', (err, response) => {
      debugger;
      console.log('called')
        //if (err) return done('Bad request') // throws twice
        //else done()
      })

logs "called" only once

@naugtur
Copy link
Owner

naugtur commented Jul 22, 2016

Ok, found it.

Root cause is: if you throw inside xhr callback, the code that guarantees it runs only once does not execute.

I'll have a fix for that in minutes

@yoshuawuyts
Copy link

Yay, nice! 😁

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

3 participants