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

[Debugger] Asynchronous callbacks aren't called until the screen is touched

Open aleclarson opened this issue 10 years ago • 14 comments

Have you guys experienced this before? Or should we get a repro going?

This is only when debugging in Chrome.

aleclarson avatar Feb 02 '16 21:02 aleclarson

I think this behavior is coming from requestAnimationFrame. Not 100% sure though.

aleclarson avatar Mar 12 '16 00:03 aleclarson

Is this when you type requestAnimationFrame into the Chrome REPL (expected that this doesn't work -- it runs on a different event loop), or when you have requestAnimationFrame in your app's code?

ide avatar Mar 12 '16 00:03 ide

@ide In my experience, it's when I call a function (from the Chrome REPL) that sets an AnimatedValue. And since that API uses requestAnimationFrame, that must be where it's getting hung up.

aleclarson avatar Mar 12 '16 00:03 aleclarson

@ide It's also happening with onLayout callbacks. Touching the screen seems to flush the Chrome event loop?

aleclarson avatar Mar 12 '16 03:03 aleclarson

Touching the screen will run the React Native event loop. What's happening, roughly, is that when you execute code from the REPL, you're not inside of the React Native event loop. The native side of the bridge isn't in a call stack that receives commands from JS and processes them. So the commands just sit in the message queue until the React Native event loop runs in the future and ends up receiving those old commands when the JS finishes executing during that run of the event loop.

Last I looked into this about a year ago, there's no easy way to hook into the Chrome REPL (maybe an extension?) so that before each REPL command we could prepare the JS environment for the next iteration of the event loop in JS and then flush the command queue when it's done. So to be safe, don't use the Chrome REPL -- even if tapping the screen ends up flushing elements in the command queue, the JS environment hasn't been prepared for the next run of the event loop when you execute code from the REPL.

ide avatar Mar 12 '16 04:03 ide

when you execute code from the REPL, you're not inside of the React Native event loop. The native side of the bridge isn't in a call stack that receives commands from JS and processes them.

Does that mean native is "responsible" for any UI jitter while Debug in Chrome is enabled? Or are there side effects to the JS thread too?

Touching the screen will run the React Native event loop

Could we prevent the event loop from "sleeping" when Debug in Chrome is enabled?

hook into the Chrome REPL (maybe an extension?) so that before each REPL command we could prepare the JS environment for the next iteration of the event loop in JS and then flush the command queue when it's done.

By preparing the JS environment, do you mean "replaying" the actions performed by the REPL on the device JS thread?

aleclarson avatar Mar 12 '16 04:03 aleclarson

Does that mean native is "responsible" for any UI jitter while Debug in Chrome is enabled? Or are there side effects to the JS thread too?

It depends on the type of jitter. For something like Animated, which computes each frame in real time on the JS thread and sends it from JS to native (over the WebSocket connection in the Chrome debugger's case), it's probably due to the JS thread not being able to send frames consistently at 60fps to the native side. The cause of that could be slow JS (ex: a heavy JSON.parse in the middle of an animation) or high latency between JS and native (ex: WebSockets w/Chrome debugger).

Could we "prevent" the event loop from sleeping when Debug in Chrome is enabled?

Technically, yes. We could have the native event loop always ask JS if its command queue has commands in it. With the Chrome debugger, there's a lot of overhead because native needs to communicate over a WebSocket to talk to JS.

The bigger problem, though, is that even if the native side continuously polls the JS side, executing JS through the REPL still happens outside of the JS part of React Native's event loop. (See next answer.)

By preparing the JS environment, do you mean "replaying" the actions performed by the REPL on the device JS thread?

Not quite. Look at this code here: https://github.com/facebook/react-native/blob/060664fd3d9331f062696e68179bac9cd4544a06/Libraries/WebSocket/RCTWebSocketExecutor.m#L173

  [self sendMessage:message waitForReply:^(NSError *socketError, NSDictionary<NSString *, id> *reply) {
    if (socketError) {
      onComplete(nil, socketError);
      return;
    }

    NSString *result = reply[@"result"];  // this is the command queue
    id objcValue = RCTJSONParse(result, NULL);
    onComplete(objcValue, nil);  // process the command queue
  }];

Aside from the app's initialization, the only time we initiate JS execution is when there's a native event (ex: a touch, network response, geolocation event, push notification, etc.) -- the point being that all JS execution is initiated from the native side over the course of an application's lifetime.

The code snippet above is how native initiates that execution via WebSockets -- it sends a message via a WebSocket to Chrome, telling Chrome to run a piece of JS. This marks the beginning of the JS side of React Native's event loop. At this point in time, the RN core JS may prepare itself to capture setTimeout calls, start profiling markers, and other things you'd want to do at the start of executing a JS command.

After the environment is prepared, the next thing that happens is that your JS is run. It might do setImmediate calls, which the JS environment is prepared to capture.

After your JS finishes running, the JS call stack is back at the RN core JS. This is where it runs enqueued setImmediate calls and cleanup work you'd want to do at the end of the event loop. One other thing it does is take all the native module calls that you may have made from your JS (this is the command queue) and sends them back over the WebSocket.

Finally we're back in native, and process the commands in the queue.

The problem with the REPL is that you're executing JS outside of the RN core JS that normally prepares the environment and later cleans it up. What we need is for the REPL to simulate the WebSocket listener on the JS side: prepare the environment, run your code, clean up the environment. But what's happening now is:

  1. the REPL runs your code
  2. you make some bridge calls that get put in the command queue... which happens to be set up... and isn't processed at this time
  3. later, you touch the screen which tells native to send an event to JS
  4. we prepare the JS environment
  5. run the touch event listeners
  6. clean up the JS environment and send the commands back to native. Since you're lucky, the command queue code was written in a way that it happens to contain your commands that were made from when your REPL code ran

ide avatar Mar 12 '16 05:03 ide

...or high latency between JS and native (ex: WebSockets w/Chrome debugger).

Would it be possible to reduce latency by going through direct connection (as opposed to wireless)?

We could have the native event loop always ask JS if its command queue has commands in it.

Should we get a PR going on this? Or is hooking into the Chrome REPL a choke point?

As for the overhead with Chrome, avoiding wi-fi could make this workable. Or just raising the polling interval.

aleclarson avatar Mar 12 '16 05:03 aleclarson

Would it be possible to reduce latency by going through direct connection (as opposed to wireless)?

I'm not sure how much of the latency comes from where. You might be able to test this (relatively) easily by setting up tethering over USB.

Should we get a PR going on this?

I don't think this is a good direction - it doesn't fix the root issue of the REPL executing code out of band, adds complexity to the bridge code, and makes the development environment less like production.

ide avatar Mar 12 '16 06:03 ide

@ide this makes for an interesting read, but I'm not quite clear what steps (if any) should be taken to close this issue. Can you clarify, or should we consider this "question answered"?

geirman avatar Apr 20 '16 00:04 geirman

Let's leave this open but I'm not sure how to fix this off the top of my head. Probably need to make a change on Chrome's side, either by submitting a patch to Blink or writing a Chrome extension if that's possible.

ide avatar Apr 20 '16 02:04 ide

Hi guys, I am having the same problem in here when trying some async call and then return the response. It does not show to the view until I press the screen. I need to show data before the component mounts I am running the emulator on the genymotion. Any idea to this one. Thanks

ylligashi avatar Jun 28 '16 13:06 ylligashi

related to https://github.com/facebook/react-native/issues/6679

bestander avatar Jun 30 '16 16:06 bestander

Hi there! This issue is being closed because it has been inactive for a while.

But don't worry, it will live on with ProductPains! Check out its new home: https://productpains.com/post/react-native/debugger-asynchronous-callbacks-arent-called-until-the-screen-is-touched

Product Pains has been very useful in highlighting the top bugs and feature requests: https://productpains.com/product/react-native?tab=top

Also, if this issue is a bug, please consider sending a pull request with a fix.

mkonicek avatar Oct 27 '16 22:10 mkonicek