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

Android: "FATAL EXCEPTION: mqt_js" / Error calling function: RCTDeviceEventEmitter:emit / - malformed JSON parsing in RN's android native code #10756

Closed
GeoffreyPlitt opened this issue Nov 4, 2016 · 49 comments
Labels
Bug Platform: Android Android applications. Ran Commands One of our bots successfully processed a command. Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@GeoffreyPlitt
Copy link

GeoffreyPlitt commented Nov 4, 2016

Description

Calling fetch() and expecting JSON results (i.e. using response.json()) will crash if server sends malformed JSON. Error/logging system prints "Failed to create String from JSON:" and then does NOT print the JSON (the problem this ticket IS about).

#9117 promised to fix this, but did not.

Reproduction

Call fetch() and then response.json() with a server that returns bad JSON.

Additional Information

  • React Native version: 0.36
  • Platform: Android
  • Operating System: MacOS

Here's an example log:

11-04 13:39:46.611  3509  3509 D EPDG -- [EPDGService]: onDataActivity: direction=3
11-04 13:39:46.611  7541  7836 D NetworkController.MobileSignalController(0/2): onDataActivity: direction=3
11-04 13:39:46.781 17917 17917 V MediaPlayer-JNI: getCurrentPosition: 617 (msec)
11-04 13:39:47.031 17917 17917 V MediaPlayer-JNI: getCurrentPosition: 617 (msec)
11-04 13:39:47.281 17917 17917 V MediaPlayer-JNI: getCurrentPosition: 617 (msec)
11-04 13:39:47.541 17917 17917 V MediaPlayer-JNI: getCurrentPosition: 617 (msec)
11-04 13:39:47.641  3509  3509 D EPDG -- [EPDGService]: onDataActivity: direction=1
11-04 13:39:47.641  7541  7836 D NetworkController.MobileSignalController(0/2): onDataActivity: direction=1
11-04 13:39:47.791 17917 17917 V MediaPlayer-JNI: getCurrentPosition: 617 (msec)
11-04 13:39:47.821 17917 17976 I System.out: (HTTPLog)-Static: isSBSettingEnabled false
11-04 13:39:47.821 17917 17976 I System.out: (HTTPLog)-Static: isSBSettingEnabled false
11-04 13:39:47.821  3079  3883 D EnterpriseController: netId is 0
11-04 13:39:47.821  3079  3883 D Netd    : getNetworkForDns: using netid 507 for uid 10726
11-04 13:39:47.821 17917 17968 E AndroidRuntime: FATAL EXCEPTION: mqt_js
11-04 13:39:47.821 17917 17968 E AndroidRuntime: Process: com.stardust, PID: 17917
11-04 13:39:47.821 17917 17968 E AndroidRuntime: java.lang.RuntimeException: Error calling function: RCTDeviceEventEmitter:emit
11-04 13:39:47.821 17917 17968 E AndroidRuntime: 	at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
11-04 13:39:47.821 17917 17968 E AndroidRuntime: 	at android.os.Handler.handleCallback(Handler.java:739)
11-04 13:39:47.821 17917 17968 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:95)
11-04 13:39:47.821 17917 17968 E AndroidRuntime: 	at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
11-04 13:39:47.821 17917 17968 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:158)
11-04 13:39:47.821 17917 17968 E AndroidRuntime: 	at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:196)
11-04 13:39:47.821 17917 17968 E AndroidRuntime: 	at java.lang.Thread.run(Thread.java:818)
11-04 13:39:47.821 17917 17968 E AndroidRuntime: Caused by: java.lang.RuntimeException: Failed to create String from JSON: 
11-04 13:39:47.821 17917 17968 E AndroidRuntime: 	... 7 more
11-04 13:39:47.841  3509  4040 W ActivityManager:   Force finishing activity com.stardust/.MainActivity
11-04 13:39:47.851  3509  4040 D FocusedStackFrame: Set to : 0
11-04 13:39:47.851  3509  4040 D InputDispatcher: Focused application set to: xxxx
11-04 13:39:47.861  3509  4040 D InputDispatcher: Focus left window: 17917
11-04 13:39:47.861  3509  3638 D PointerIcon: setMouseIconStyle1 pointerType: 1001 iconType:101 flag:0 pid:3509 uid:1000
11-04 13:39:47.861  3509  3638 D PointerIcon: setMouseCustomIcon IconType is same.101
11-04 13:39:47.931  3509 18371 E android.os.Debug: ro.product_ship = true
11-04 13:39:47.931  3509 18371 E android.os.Debug: ro.debug_level = 0x4f4c
11-04 13:39:47.931  3509 18371 E android.os.Debug: sys.mobilecare.preload = false
11-04 13:39:47.971  3509  3610 D ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{5822614 V.E...... R.....I. 0,0-0,0}
11-04 13:39:47.981  3509  3610 D ISSUE_DEBUG: InputChannelName : 1cdbcb2 Application Error: com.stardust
11-04 13:39:47.981  3509  3610 V MARsPolicyManager: handelAlertToastWindowStarted pkgName = android
11-04 13:39:47.981  3509  3610 D InputDispatcher: Focus entered window: 3509
11-04 13:39:47.981  3509  3638 D PointerIcon: setMouseIconStyle1 pointerType: 1001 iconType:101 flag:0 pid:3509 uid:1000
11-04 13:39:47.981  3509  3638 D PointerIcon: setMouseCustomIcon IconType is same.101
11-04 13:39:47.991  3067  3067 I SurfaceFlinger: id=2946 createSurf (193x193),1 flag=4, ttardust
11-04 13:39:48.001 18372 18372 E Zygote  : v2
11-04 13:39:48.001 18372 18372 I libpersona: KNOX_SDCARD checking this for 1000
11-04 13:39:48.001  3509  3609 I ActivityManager: Start proc 18372:com.samsung.android.sm/1000 for broadcast-3 com.samsung.android.sm/.common.SmartManagerReceiver
11-04 13:39:48.001 18372 18372 I libpersona: KNOX_SDCARD not a persona

Questions:

  • What do we have to do to actually include the JSON so we can get to the bottom of this?
  • Why does it say "... 7 more" ?
@brentvatne
Copy link
Collaborator

Hello! You're unlikely to get much help until you can provide a reproducible example :( Please keep trying and let us know when you have it.

@GeoffreyPlitt
Copy link
Author

I narrowed it down.

BUG: Using fetch() and then doing response.json() when the server sends
malformed JSON causes a crash.

Expectation: JS exception, not crash.

On Nov 6, 2016 10:04 AM, "Brent Vatne" notifications@github.com wrote:

Hello! You're unlikely to get much help until you can provide a
reproducible example :( Please keep trying and let us know when you have it.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#10756 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAXXZY1Mr-JMwYXdY0poY0JMccwNIGZ_ks5q7ha2gaJpZM4Kp-dh
.

@brentvatne
Copy link
Collaborator

@GeoffreyPlitt - that's no fun. Does that happen in release mode or debug or both?

@GeoffreyPlitt
Copy link
Author

Both

On Nov 6, 2016 10:14 AM, "Brent Vatne" notifications@github.com wrote:

@GeoffreyPlitt https://github.com/GeoffreyPlitt - that's no fun. Does
that happen in release mode or debug or both?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#10756 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAXXZcA_h-an57Pav1_aHGGQNebkhVPVks5q7hkGgaJpZM4Kp-dh
.

@brentvatne
Copy link
Collaborator

@lacker - any idea who this should be assigned to?

@lacker
Copy link
Contributor

lacker commented Nov 7, 2016

I don't have someone who is going to jump on and fix this, but if there is a pull request that fixes this I would be happy to prioritize its review.

@GeoffreyPlitt GeoffreyPlitt changed the title "Failed to create String from JSON" - STILL not showing the problematic JSON fetch() with malformed JSON from server can crash app Nov 7, 2016
@leeight
Copy link
Contributor

leeight commented Nov 9, 2016

@GeoffreyPlitt I tried to reproduce this case but failed ....

export default class Issue10756 extends Component {
  constructor(...args) {
    super(...args);
    this.state = {
      text: 'Welcome to React Native!'
    };
  }

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          {this.state.text}
        </Text>
        <Button title="fetch()" onPress={this.sendRequest.bind(this)} />
      </View>
    );
  }

  sendRequest() {
    fetch('https://www.baidu.com/')
      .then(response => response.json())
      .then(response => this.setState({text: JSON.stringify(response, null, 2)}))
      .catch(error => this.setState({text: String(error)}));
  }
}

After click the <Button>, i got the JS Exception, and the app not crash...

image

@GeoffreyPlitt
Copy link
Author

Update: Yeah I just re-tested this and you're right, it's not caused by fetch() / response.json(). I'm now convinced that this is some background android process. New crash logs from today:

1-09 12:05:20.698 16816 16850 E AndroidRuntime: FATAL EXCEPTION: mqt_js
11-09 12:05:20.698 16816 16850 E AndroidRuntime: Process: com.stardust, PID: 16816
11-09 12:05:20.698 16816 16850 E AndroidRuntime: java.lang.RuntimeException: Error calling function: RCTDeviceEventEmitter:emit
11-09 12:05:20.698 16816 16850 E AndroidRuntime:    at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
11-09 12:05:20.698 16816 16850 E AndroidRuntime:    at android.os.Handler.handleCallback(Handler.java:751)
11-09 12:05:20.698 16816 16850 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:95)
11-09 12:05:20.698 16816 16850 E AndroidRuntime:    at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
11-09 12:05:20.698 16816 16850 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:154)
11-09 12:05:20.698 16816 16850 E AndroidRuntime:    at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:196)
11-09 12:05:20.698 16816 16850 E AndroidRuntime:    at java.lang.Thread.run(Thread.java:761)
11-09 12:05:20.698 16816 16850 E AndroidRuntime: Caused by: java.lang.RuntimeException: Failed to create String from JSON: 
11-09 12:05:20.698 16816 16850 E AndroidRuntime:    ... 7 more

What is mqt_js and how can we find out why it causes Error calling function: RCTDeviceEventEmitter:emit ?

@GeoffreyPlitt GeoffreyPlitt changed the title fetch() with malformed JSON from server can crash app Android: "FATAL EXCEPTION: mqt_js" / Error calling function: RCTDeviceEventEmitter:emit / - malformed JSON parseing in native-land Nov 9, 2016
@GeoffreyPlitt GeoffreyPlitt changed the title Android: "FATAL EXCEPTION: mqt_js" / Error calling function: RCTDeviceEventEmitter:emit / - malformed JSON parseing in native-land Android: "FATAL EXCEPTION: mqt_js" / Error calling function: RCTDeviceEventEmitter:emit / - malformed JSON parsing in RN's android native code Nov 9, 2016
@GeoffreyPlitt
Copy link
Author

More: This seems to only happen in release-mode i.e. react-native run-android --variant=release or install via APK.

It does not happen in debug mode.

@lacker
Copy link
Contributor

lacker commented Nov 9, 2016

This is another case where reproducing it is going to make it easier to debug. What is the simplest app that reproduces this?

@lexor90
Copy link

lexor90 commented Dec 15, 2016

Hey there,
I just got the same error.

All working properly on iOS but it doesn't work with Android.
I'm getting this with

generateImage(id) {
  var self = this;
  this.setState({
    image: ''
  }, () => {
    var uri = 'https://source.unsplash.com/' + Dimensions.get('window').width + 'x' + Dimensions.get('window').height + '/?restaurants#ignoreCacheFragment=' + Number(Date.now());
  
    fetch(uri)  
    .then((response) => {
    
      uri = response.url;
      this.setState({
        images: self.state.images.concat([uri]),
        image: uri,
        lastId: self.state.lastId++,
      });
    }).catch(error => console.log(error));
  });
}

Ignoring the other parts which are not causing this issue what you get here is a 302 HTTP response from unsplash. You can try with a static url such as:

$ curl https://source.unsplash.com/1600x900/?restaurants -i

HTTP/1.1 302 Found
Server: Cowboy
Date: Thu, 15 Dec 2016 23:16:36 GMT
Connection: keep-alive
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Location: https://images.unsplash.com/photo-1429681601148-75510b2cef43?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1600&h=900&fit=crop&s=2c9a1a9c3bb523bef3ee6b07aba56cfa
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
X-Request-Id: 6a9fa9a4-8c56-451e-b20a-569f86f2b2d0
X-Runtime: 0.135146
Strict-Transport-Security: max-age=31536000
Vary: Origin
Transfer-Encoding: chunked
Via: 1.1 vegur

<html><body>You are being <a href="https://images.unsplash.com/photo-1429681601148-75510b2cef43?ixlib=rb-0.3.5&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=1600&amp;h=900&amp;fit=crop&amp;s=2c9a1a9c3bb523bef3ee6b07aba56cfa">redirected</a>.</body></html>

On iOS I can properly access the raw response object. On Android the error is the same as the OP, it crashes before to even resolve the promise (it's not related to any call inside the promise, the promise never runs).

I was digging into this problem but I'm unsure if with Android (5.1.1 here for tests) it finds a native implementation or it's using the whatwg-fetch (tbh, I don't quite understand where in react native could it be found natively – i'd say it's always from the library, am I wrong?).

I'm new into looking at RN codebase if you can give to me some advise I can try to get more info.

@luocf
Copy link

luocf commented Dec 20, 2016

Hey there,
I just got the same error in 0.39

@yangrui
Copy link

yangrui commented Dec 28, 2016

Crash on Android, but OK on iOS

fetch(pngURL)
  .then(response => response.text())
  .then(base64Data => {});

@anray
Copy link

anray commented Jan 13, 2017

In my case the problem was that fetch("URL") method expected JSON in response, however I was passing stub URL which was an url to an image, e.g. http://test.com/test.png .
Once I passed in fetch("URL_returning_JSON") the error has gone and I can run app on the phone in usual mode and release build is not crashing anymore.

FYI: List of open APIs https://github.com/toddmotto/public-apis

@teyou
Copy link

teyou commented Jan 24, 2017

it happens when I do

fetch('http://ideapit.net/filedata/201203/9fi32220120330230712.jpg')

and it's only crash on Android release mode.

Hence, I switched to axios and since then no more crash!

@Amurmurmur
Copy link

Same error here when fetching an image :/
Whats more interesting is that on Nexus 5 running Android 6.0.1 it crashes right before promise finishes, however on Samsung Nexus running 5.1.1 it doesnt with exactly the same fetch code

@mr-ryan-james
Copy link

mr-ryan-james commented Feb 20, 2017

This is happening to me on android 5.0.1 when attempting to fetch(URL) an image.

It seems to be happening due to lack of support for 'blob'. I feel like this should be documented in the networking page since the tutorial seems to indicate you can use the fetch api in it's entirety.

A workaround is to use the react-native-fetch-blob library.

@langjun
Copy link

langjun commented Mar 23, 2017

I got the same crash.
"react-native": "0.42.3"
when I do this on Android.
fetch('http://s1.dwstatic.com/group1/M00/F5/15/af09c9472255fc101863cf4929a4daff.gif')

I try to use axios, it's correct.

@ArcanoxDragon
Copy link

I'm getting the same crash when requesting an HTML page. Don't have an iOS device to test on, but this crash occurs on my Pixel XL with latest react-native and latest Android version.

@cbjs
Copy link

cbjs commented Apr 4, 2017

same problem here.

trying to fetch image info from http head response.
crash only in release mode, android.

curl -I http://m.newsmth.net/att/inbox/52/89252
Accept-Ranges →bytes Cache-Control →no-store, no-cache, must-revalidate Connection →keep-alive Content-Disposition →inline;filename=Íí·¹£¬Ñ©²ËÇඹ¶¹¸¯¡£170403_±±¾©¡£.jpg Content-Length →89381 Content-Type →image/jpeg Date →Tue, 04 Apr 2017 10:49:11 GMT Expires →Thu, 18 Feb 1988 01:00:00 GMT Pragma →no-cache Server →nginx/1.8.0

@yk3372
Copy link
Contributor

yk3372 commented May 27, 2017

fetch('http://s1.dwstatic.com/group1/M00/F5/15/af09c9472255fc101863cf4929a4daff.gif')
I test above request, and console log at Libraries/Network/XMLHttpRequest.js send method, I find the this._responseType is empty string.
But when I Remote Debugging, the this._responseType is blob. blob is right, why?

@jasonchou3
Copy link

jasonchou3 commented Jul 2, 2017

Same error
fetch('http://www.00ksw.org/html/1/1391/614905.html') will Crash on Android

"react-native": "0.45.1",

Device :
Nexus 5
Android 6

@flyher
Copy link

flyher commented Jul 10, 2017

same problem here in Android 6.

@langjun
The axios worked for me too.

@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Aug 29, 2018
@zolaahihi
Copy link

Hello,

Any update on this issue?

How can I fix this bug ? :(

@morenoh149
Copy link
Contributor

@zolaahihi sounds like you'd have to trace through the c++ and debug it. Let us know if you have questions.

@pothos-dev
Copy link

Just a head's up, this error occured for us when we sent Accept-Encoding=gzip with the request. Maybe this will help someone to work around the issue.

@huangd0ng
Copy link

It's a bug caused by the following code:

Value Value::fromDynamic(JSContextRef ctx, const folly::dynamic& value) {
// JavaScriptCore's iOS APIs have their own version of this direct conversion.
// In addition, using this requires exposing some of JSC's private APIs,
//  so it's limited to non-apple platforms and to builds that use the custom JSC.
// Otherwise, we use the old way of converting through JSON.
#if USE_FAST_FOLLY_DYNAMIC_CONVERSION
  // Defer GC during the creation of the JSValue, as we don't want
  //  intermediate objects to be collected.
  // We could use JSValueProtect(), but it will make the process much slower.
  JSDeferredGCRef deferGC = JSDeferGarbageCollection(ctx);
  // Set a global lock for the whole process,
  //  instead of re-acquiring the lock for each operation.
  JSLock(ctx);
  JSValueRef jsVal = Value::fromDynamicInner(ctx, value);
  JSUnlock(ctx);
  JSResumeGarbageCollection(ctx, deferGC);
  return Value(ctx, jsVal);
#else
  auto json = folly::toJson(value);
  return fromJSON(String(ctx, json.c_str()));
#endif
}

folly:toJson produces a string that cannot be decoded by String(ctx, json.c_str()) with utf8 encoding.

if we change the code to:

  folly::json::serialization_opts opts;
  opts.encode_non_ascii = true;
  opts.skip_invalid_utf8 = true;
  auto json = folly::json::serialize(value, opts);

It will not crash. But the result may be incorrect.

@stale
Copy link

stale bot commented Jan 6, 2019

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as "For Discussion" or "Good first issue" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Jan 6, 2019
@lll000111

This comment has been minimized.

@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Jan 6, 2019
@morenoh149
Copy link
Contributor

@huangd0ng what lines are changed exactly?

  auto json = folly::toJson(value);

should be

  folly::json::serialization_opts opts;
  opts.encode_non_ascii = true;
  opts.skip_invalid_utf8 = true;
  auto json = folly::json::serialize(value, opts);

? that could be a PR

@huangd0ng
Copy link

@morenoh149 It is that line. My change just prevents it from crashing. But when you fetch binary data, you may not get correct result. So I do not think we should fix it this way.

@stale
Copy link

stale bot commented Aug 4, 2019

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Aug 4, 2019
@ManishPatiyal
Copy link

Hi, I am getting this issue in my app's on of the @ReactMethod. My observations are that this issue is occuring only on 32 bit android devices and not 64 bit. I am not sure if its only on emulator or actual device too. I am not having any 32-bit device with me so checked only on emulator.

@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Aug 7, 2019
@stale
Copy link

stale bot commented Nov 5, 2019

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Nov 5, 2019
@lll000111
Copy link

Just for the bot. No activity means it's not solved.

@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Nov 5, 2019
@rodrigomf24
Copy link

I am having this issue too, has anyone been able to solve it? I am using react native version 0.56.1

@stale
Copy link

stale bot commented Mar 26, 2020

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Mar 26, 2020
@stale
Copy link

stale bot commented Apr 2, 2020

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.

@stale stale bot closed this as completed Apr 2, 2020
@facebook facebook locked as resolved and limited conversation to collaborators Apr 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Platform: Android Android applications. Ran Commands One of our bots successfully processed a command. Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

No branches or pull requests