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

response.text() and response.json() get "stuck" sometimes #14391

Closed
mikelambert opened this issue Jun 8, 2017 · 8 comments
Closed

response.text() and response.json() get "stuck" sometimes #14391

mikelambert opened this issue Jun 8, 2017 · 8 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@mikelambert
Copy link
Contributor

mikelambert commented Jun 8, 2017

My code more or less does the following, on React Native 0.45:

async function request() {
    const result = await fetch(url);
    const data = await result.text();
    console.log(data);
    return data;
}

When running this in Chrome Debugger on the iOS Simulator, the result will get "stuck" sometimes. By that, I mean the first time I call it, it works fine. But the second time I run a request, the data will not be printed out until something else happens to jiggle the event loop and cause it to return.

If I disable the chrome debugger, things work.

I assume this is related to what people have been complaining about on the prematurely-closed bug #6679 .

=================

What follows is my attempt at debugging. I won't promise anything, as I'm not a promise expert, but maybe it helps narrow things down?

The http response data is shuffled into the response.blob. When text() or json() is called, it calls readBlobAsText to read the blob, and I do see the string successfully loaded and passed around within the promise/setimmediate/core.js code. Unfortunately, this string doesn't always make it back to the caller for some reason.

(This might be a red herring, but I see it call handleResolved() and the equivalent of setImmediate((self, deferred) => tryCallOne(deferred.onFulfilled, self._65 /* body payload */)), but the anonymous function isn't actually called immediately...)

And interesting to note, if I edit fetch.js to disable support.blob, it also works.

This makes me suspect it has something to do with the text() function. In most cases, it returns Promise.reject or Promise.resolve, but in the case of using blobs, it returns a newly-generated Promise (from the fileReaderReady function). My guess is that this probably triggers another level of promise-indirection, that somehow causes it to stall before the event-loop notices it and triggers it to complete.

I'm still quite a newb as to the JSTimers* code and guts of the promise code, so this is where the trail goes cold for me. :(

@wschurman
Copy link
Contributor

Seeing this as well. Commenting to bump.

@congqiao
Copy link

Same here. It seems this only happens in React Native 0.45. For me the probability is around 50%.

Quick workaround: Tap the screen to continue.

@kyo504
Copy link
Contributor

kyo504 commented Jun 14, 2017

I also experienced this behavior after upgrading to 0.45.

@edwhu
Copy link

edwhu commented Jun 15, 2017

Just upgraded and ran into this bug. Using @sebandres' quick hack to force it to complete.

fetch(url).then(response => {
  setTimeout(() => null, 0);  // workaround for issue-6679
  return response.json();
})

@mikelambert
Copy link
Contributor Author

And the modified ES7 form of the above code that also works for me (thanks!):

    setTimeout(() => null, 0);
    const json = await result.json();

Just add the setTimeout before you call json() or text() or anything else that operates on response.blob.

As far as the underlying bug, definitely feels like the eventloop "falls asleep" when processing response.blob and readBlobAsText, instead of recognizing there are still a Promise on the eventloop to process and return data back to the callers.

@mikelambert
Copy link
Contributor Author

And I just found this comment (in a "closed" bug), which might describe the root cause of this issue: #9436 (comment)

@mikelambert
Copy link
Contributor Author

Yay, this appears to be fixed in 0.47 by 6482538 !

@arash-hacker
Copy link

thanks @EdwardHuCS

@facebook facebook locked as resolved and limited conversation to collaborators Jul 11, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

7 participants