react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[BUG] setTimeout fires incorrectly when using chrome debug

Open Thomas101 opened this issue 11 years ago • 47 comments

Hey,

I've found an issue that means when you're running on an iOS device and debugging in chrome setTimeouts can fire in the wrong order. If you place the following code block into a fresh app and run it you should get the error...

  componentDidMount:function() {
    this.runTest()
  },

  test:function(cb) {
    var aFired = false
    var bFired = false
    var aExpected = new Date().getTime()+130
    var bExpected = new Date().getTime()+500
    console.log('Schedule A to fire @' + aExpected)
    console.log('Schedule B to fire @' + bExpected)

    var a = setTimeout(() => {
      var now = new Date().getTime()
      console.log('A fired', now, now - aExpected)
      aFired = true
      if (bFired) { console.log('ERROR: b fired early'); return;
      }
      if (aFired && bFired) { cb() }
    }, 130)
    var b = setTimeout(() => {
      var now = new Date().getTime()
      console.log('B fired', now, now - bExpected)
      bFired = true
      if (!aFired) { console.log('ERROR: A not fired'); return;
      }
      if (aFired && bFired) { cb() }
    }, 500)
  },
  runTest:function() {
    this.test(() => {
      setTimeout(() => {
        this.runTest()
      }, 500)
    })
  },

On the console you'll get something like...

index.ios.js:25 Schedule A to fire @1448977966848
index.ios.js:26 Schedule B to fire @1448977967218
index.ios.js:30 A fired 1448977966754 -94
index.ios.js:38 B fired 1448977966755 -463
index.ios.js:25 Schedule A to fire @1448977966902
index.ios.js:26 Schedule B to fire @1448977967272
index.ios.js:30 A fired 1448977966806 -96
index.ios.js:38 B fired 1448977966806 -466
index.ios.js:25 Schedule A to fire @1448977966951
index.ios.js:26 Schedule B to fire @1448977967321
index.ios.js:30 A fired 1448977966835 -116
index.ios.js:38 B fired 1448977966835 -486


index.ios.js:25 Schedule A to fire @1448977966983
index.ios.js:26 Schedule B to fire @1448977967353
index.ios.js:38 B fired 1448977966871 -482
index.ios.js:40 ERROR: A not fired
index.ios.js:30 A fired 1448977966871 -112
index.ios.js:32 ERROR: b fired early

... with the first 3 firing correctly and the last one incorrectly. I believe this bug is also what is causing issue #1693 as one of the setTimeout functions sometimes fires early in touchableHandleResponderGrant

Thomas101 avatar Dec 01 '15 13:12 Thomas101

Hey Thomas101, thanks for reporting this issue!

React Native, as you've probably heard, is getting really popular and truth is we're getting a bit overwhelmed by the activity surrounding it. There are just too many issues for us to manage properly.

  • If this is a feature request or a bug that you would like to be fixed by the team, please report it on Product Pains. It has a ranking feature that lets us focus on the most important issues the community is experiencing.
  • If you don't know how to do something or not sure whether some behavior is expected or a bug, please ask on StackOverflow with the tag react-native or for more real time interactions, ask on Discord in the #react-native channel.
  • We welcome clear issues and PRs that are ready for in-depth discussion; thank you for your contributions!

facebook-github-bot avatar Dec 01 '15 14:12 facebook-github-bot

@Thomas101 Try taking a look at https://facebook.github.io/react-native/docs/timers.html#timermixin at the last paragraph: We strongly discourage using the global setTimeout(...) and recommend instead that you use this.setTimeout(...)

christopherdro avatar Dec 01 '15 15:12 christopherdro

Thanks @christopherdro, we've been using that wherever possible but some of the built in react functionality uses plain old settimeout which is where the bug raised its head initially

Thomas101 avatar Dec 01 '15 17:12 Thomas101

@Thomas101 Thanks for your detailed investigation and write-up. RN should implement timers correctly. In fact if I'm reading your code and logs correctly, it looks like the timeout arg is ignored as events A and B run at almost the same time. cc @sahrens

ide avatar Dec 01 '15 17:12 ide

Those were the logs that came out, you should get similar results if you run it yourself. This is purely speculation as I haven't looked at the implementation, but as it only happenens when running the chrome debugger and using the device, did wonder if it could be a time drift issue between my Mac and ipad?

Thomas101 avatar Dec 01 '15 18:12 Thomas101

@christopherdro Also not possible to use it when using the ES6 class syntax. Need to manually cleanup ofc.

satya164 avatar Dec 03 '15 03:12 satya164

Not sure who owns timers these days - maybe @tadeuzagallo or @nicklockwood ? Also cc @dmmiller because we want to be consistent on iOS and android.

sahrens avatar Dec 05 '15 21:12 sahrens

I've experienced this while "Debug in Chrome" is active. setTimeout fires very early.

Has it been confirmed that TimerMixin avoids this issue?

I thought TimerMixin only helps with cleanup in componentWillUnmount?

aleclarson avatar Jan 06 '16 20:01 aleclarson

Hi @aleclarson using the TimerMixin still has the same problem as this uses setTimeout behind the scenes.

Thomas101 avatar Jan 07 '16 10:01 Thomas101

Who works on the debugger-proxy packager endpoint? Is there a hack related to setTimeout that involves accounting for socket latency? I can't imagine how else setTimeout could be acting so strangely.

aleclarson avatar Jan 08 '16 00:01 aleclarson

Go vote for this issue so that the developers can see that it's important: https://productpains.com/post/react-native/settimeout-fires-incorrectly-when-using-chrome-debug/

PaulBGD avatar Jan 09 '16 02:01 PaulBGD

I don't know much about our chrome integration or why it would cause a difference in timer behavior - @frantic or @tadeuzagallo?

sahrens avatar Jan 09 '16 03:01 sahrens

Would it be worth implementing a native shim for setTimeout and clearTimeout?

aleclarson avatar Jan 12 '16 03:01 aleclarson

Any updates on this?

satya164 avatar Jan 19 '16 21:01 satya164

So, I did a some small test on this,

var start = performance.now(); console.log(performance.now(), start); // 751855.6900000001 492872.2
var start = performance.now(); setTimeout(() => console.log(performance.now() - start), 1); // 345195.46

This is super weird. The value of performance.now() is vastly different than when it's assigned to a variable. SImilar results for Date.now().

No wonder setTimeout executes instantly even if you give a timeout of 10 seconds.

cc @vjeux @dmmiller @nicklockwood

satya164 avatar Jan 25 '16 13:01 satya164

[meta-discussion] At this point, there is a good repro case (thanks @Thomas101, that's really awesome) and no clear owner (as everyone is cc-ing other people). The way forward is to get someone to dig into the internals and figure out what's going on. It doesn't have to be a Facebook employee, thankfully the entire part of this stack is open source and anyone can try and figure out what's going on.

I suggest that if you are affected by this issue and have some time to spare, try to dig in and figure out what's going on.

vjeux avatar Jan 25 '16 16:01 vjeux

I think the release 0.25-rc solve the problem.

marcosinigaglia avatar Apr 19 '16 13:04 marcosinigaglia

I am using 0.28.0. It looks like logging in chrome with this.setInterval is not working still. I faced the issue for Android.

Abhay-Joshi-Git avatar Jun 29 '16 05:06 Abhay-Joshi-Git

Same as @Abhay-Joshi-Git here as well (using plain setTimeout in a function outside a React component). For a timeout of 4000 ms it fires immediately. (RN 0.28.0 running on Android device). When Chrome Debugging is disabled it works as expected.

martinarroyo avatar Jun 29 '16 14:06 martinarroyo

I'm using RN0.29RC0 and facing the same problem, onLongPress fires immediately while Debug JS Remotely enabled

nihgwu avatar Jun 29 '16 16:06 nihgwu