[BUG] setTimeout fires incorrectly when using chrome debug
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
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-nativeor 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!
@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(...)
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 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
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?
@christopherdro Also not possible to use it when using the ES6 class syntax. Need to manually cleanup ofc.
Not sure who owns timers these days - maybe @tadeuzagallo or @nicklockwood ? Also cc @dmmiller because we want to be consistent on iOS and android.
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?
Hi @aleclarson using the TimerMixin still has the same problem as this uses setTimeout behind the scenes.
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.
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/
I don't know much about our chrome integration or why it would cause a difference in timer behavior - @frantic or @tadeuzagallo?
Would it be worth implementing a native shim for setTimeout and clearTimeout?
Any updates on this?
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
[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.
I think the release 0.25-rc solve the problem.
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.
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.
I'm using RN0.29RC0 and facing the same problem, onLongPress fires immediately while Debug JS Remotely enabled