-
Notifications
You must be signed in to change notification settings - Fork 24.5k
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
[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 #24767
Conversation
459d608
to
9fc56df
Compare
7b689fb
to
ccaf85f
Compare
I added the buck config but I can't confirm it works, RNTester currently crashes on master with |
ccaf85f
to
1ee0449
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hramos has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's try to land this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cpojer has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
This pull request was successfully merged by @janicduplessis in a4f7e17. When will my fix make it into a release? | Upcoming Releases |
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. |
@cpojer Any change you can get me the crash log? I get a feeling it's related to fb internal setup. |
Unfortunately I don't have anything actionable that I can share :( |
@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? |
@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? |
@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 |
@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. |
@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 ! |
@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). |
@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 (facebook#26155) Summary: Reland facebook#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 facebook#25720 to fix a crash when using hermes. [Android] [Fixed] - Release underlying resources when JS instance is GC'ed on Android Pull Request resolved: facebook#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 facebook/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 facebook/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: facebook/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
…cebook#24767) Summary: Android followup for facebook#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: facebook#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.