Uh oh!
There was an error while loading. Please reload this page.
[Blob] Release underlying resources when JS instance is GC'ed on Android - #24767
[Blob] Release underlying resources when JS instance is GC'ed on Android#24767janicduplessis wants to merge 1 commit into
Conversation
459d608 to
9fc56dfCompare7b689fb to
ccaf85fComparejanicduplessis
commented
May 8, 2019
I added the buck config but I can't confirm it works, RNTester currently crashes on master with |
ccaf85f to
1ee0449Compare
facebook-github-bot
left a comment
There was a problem hiding this comment.
@hramos has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
facebook-github-bot
left a comment
There was a problem hiding this comment.
@cpojer has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
react-native-bot
commented
May 31, 2019
This pull request was successfully merged by @janicduplessis in a4f7e17. When will my fix make it into a release? | Upcoming Releases |
cpojer
commented
Jun 5, 2019
Heads up: this is causing an insta-crash in one of our apps. I'll try to get more details but we have to unland this change for now. In case you have a hunch of what could cause it, feel free to send the same PR again with a fix. |
janicduplessis
commented
Jun 7, 2019
@cpojer Any change you can get me the crash log? I get a feeling it's related to fb internal setup. |
cpojer
commented
Jun 10, 2019
Unfortunately I don't have anything actionable that I can share :( |
janicduplessis
commented
Jun 10, 2019
@cpojer The crash happens in one specific fb app, not all apps right? I can't tell for sure but most crashes I experienced when working on the feature was with JNI refs or failing to load the .so file, but those should lead to a clear stacktrace so might not be that. https://github.com/facebook/react-native/pull/24767/files#diff-8d838f0df357a8f8620e924ba05c9815R16 maybe this could be null and cause an obscure crash later in c++. What would be the best way test changes and try to move this forward? |
cpojer
commented
Jun 10, 2019
@janicduplessis I'll put it on my queue and ask somebody at FB to help out re-land this. |
@janicduplessis I found the reason of the crash in one of the FB apps, Proguard renamed the
I landed this diff by adding the The crash occurs in the debug build, so this time it's not related to Proguard apparently. It has something to do with the Java class loader probably. Do you have any ideas? |
janicduplessis
commented
Jun 28, 2019
@makovkastar Hmm this is strange. Does this happen at launch, at a specific point or randomly later? We inject the Do you know if there is something at facebook that allows native modules to be unloaded during runtime? In that case it is possible that the class gets unloaded if all instances are gone while the c++ code is still injected in the js runtime. |
@janicduplessis Sorry, it took me some time to start working on this issue again. The crash happens always at the same time, shortly after the app is started. At first I thought it was because the destructor is not called from a Java thread, so JNI doesn't have access to the Java class loader (https://developer.android.com/training/articles/perf-jni#faq_FindClass). But by putting a breakpoint into the destructor of BlobCollector, I verified that it was actually called from a Java thread. We can use I don't think unloading classes is the case, since |
janicduplessis
commented
Jul 4, 2019
@makovkastar Nice find! I don't have much jni experience so I didn't know about this. Seems like a good fix to me. I remember reading there was no guarantee on what thread jni objects destructors are called on (hence adding synchronization to BlobManager) but didn't about the difference between c++ and java threads. |
tlupo
commented
Jul 16, 2019
@makovkastar@janicduplessis Thank you very much for your work on this issue ! I work on a RN mobile application which has to make recurrent API calls and I had some memory issues after few hours of app usage (this app has to be active 24/7). I have tested the fix for iOS on the new RN version (v0.60 : 9ef5107) and it works ! I would like to fix the Android version of the app (which seems to leak with new version). If I understand, it seems that the addition of « ThreadScope::WithClassLoader » in the destructor fixes the last side effect of this proposal ? Do you think it’s ready to be used in production (I am looking for the best way to add this patch on my RN version, maybe via a fork, it’s gonna be hard to wait for the new RN version because this memory issue is critical for our app) ? Again thank you very much for your help ! |
janicduplessis
commented
Jul 16, 2019
@tlupo Yes, I've been using this in prod for a few months without issues. 88e18b6 landed on master a few days ago which is the final version of this PR. I'll post in the 0.60 release thread to see if we can get this cherry-picked in 0.60. You can also use this with a fork (note that you have to build android from source in this case https://github.com/facebook/react-native/wiki/Building-from-source). |
tlupo
commented
Jul 17, 2019
@janicduplessis Great ! I will try this PR ! Should be great to get it in RN 0.60. Thank you very much ! |
… 2 (#26155) Summary: Reland #24767 The commit had to be reverted because it caused a crash when using remote debugging in chrome. This is normal since jsi is not available in that environment. The crash was caused by `jsContext.get()` being 0, then being dereferenced later in c++. We can simply skip initializing the blob collector in this case. This also includes the fix from #25720 to fix a crash when using hermes. ## Changelog [Android] [Fixed] - Release underlying resources when JS instance is GC'ed on Android Pull Request resolved: #26155 Test Plan: Test using RN tester with jsc and hermes Test remote debugging Reviewed By: mdvacca, fred2028 Differential Revision: D17072644 Pulled By: makovkastar fbshipit-source-id: 079d1d43501e854297fbbe586ba229920c892584
… 2 (react#26155) Summary: Reland react#24767 The commit had to be reverted because it caused a crash when using remote debugging in chrome. This is normal since jsi is not available in that environment. The crash was caused by `jsContext.get()` being 0, then being dereferenced later in c++. We can simply skip initializing the blob collector in this case. This also includes the fix from react#25720 to fix a crash when using hermes. [Android] [Fixed] - Release underlying resources when JS instance is GC'ed on Android Pull Request resolved: react#26155 Test Plan: Test using RN tester with jsc and hermes Test remote debugging Reviewed By: mdvacca, fred2028 Differential Revision: D17072644 Pulled By: makovkastar fbshipit-source-id: 079d1d43501e854297fbbe586ba229920c892584
… 2 (#26155) Summary: Reland #24767 The commit had to be reverted because it caused a crash when using remote debugging in chrome. This is normal since jsi is not available in that environment. The crash was caused by `jsContext.get()` being 0, then being dereferenced later in c++. We can simply skip initializing the blob collector in this case. This also includes the fix from #25720 to fix a crash when using hermes. ## Changelog [Android] [Fixed] - Release underlying resources when JS instance is GC'ed on Android Pull Request resolved: #26155 Test Plan: Test using RN tester with jsc and hermes Test remote debugging Reviewed By: mdvacca, fred2028 Differential Revision: D17072644 Pulled By: makovkastar fbshipit-source-id: 079d1d43501e854297fbbe586ba229920c892584
… 2 (#26155) Summary: Reland react/react-native#24767 The commit had to be reverted because it caused a crash when using remote debugging in chrome. This is normal since jsi is not available in that environment. The crash was caused by `jsContext.get()` being 0, then being dereferenced later in c++. We can simply skip initializing the blob collector in this case. This also includes the fix from react/react-native#25720 to fix a crash when using hermes. ## Changelog [Android] [Fixed] - Release underlying resources when JS instance is GC'ed on Android Pull Request resolved: react/react-native#26155 Test Plan: Test using RN tester with jsc and hermes Test remote debugging Reviewed By: mdvacca, fred2028 Differential Revision: D17072644 Pulled By: makovkastar fbshipit-source-id: 079d1d43501e854297fbbe586ba229920c892584
…act#24767) Summary: Android followup for react#24745. This adds a jsi object that removes blobs when it is gc'ed. We don't have many modules with native code on Android so I've added the native code directly in the blob package as a separate .so. I used a similar structure as the turbomodule package. ## Changelog [Android] [Fixed] - [Blob] Release underlying resources when JS instance is GC'ed on Android Pull Request resolved: react#24767 Differential Revision: D15279651 Pulled By: cpojer fbshipit-source-id: 2bbdc4bbcbeae8945588ac5e3e895c49e6ac9e1a
Summary
Android followup for #24745. This adds a jsi object that removes blobs when it is gc'ed. We don't have many modules with native code on Android so I've added the native code directly in the blob package as a separate .so. I used a similar structure as the turbomodule package.
Changelog
[Android] [Fixed] - [Blob] Release underlying resources when JS instance is GC'ed on Android
Test Plan
Tested in RNTester by adding a breakpoint in the method that removes blobs and made sure it is triggered when making an http request with fetch.