Harden on-demand JS segment load path against missing cache file - #57855
Closed
javache wants to merge 1 commit into
Closed
Harden on-demand JS segment load path against missing cache file#57855javache wants to merge 1 commit into
javache wants to merge 1 commit into
Conversation
Summary:
Harden the on-demand (lazily code-split) JS segment load path against a missing cache file.
When RN lazily loads an on-demand JS segment, the file lives in an OS-purgeable / LRU-evictable on-demand cache and can already be gone by the time the segment is registered. On a missing file `JSBigFileString::fromPath` throws `std::runtime_error("... Could not open file: ...")`, and `ReactInstance::registerSegment` had no failure handling, so the throw surfaced as an unhandled JS exception and crashed the app.
Fix (V4, addresses pieterdb's latest review feedback):
- `JSBigFileString::fromPath`'s throwing contract is now documented in the header: it throws `std::runtime_error` when the file cannot be opened or read, and callers loading files that may legitimately be gone (an OS-purgeable / LRU-evictable cache) must catch and degrade gracefully. No behavior change to `fromPath` and no new factory.
- `ReactInstance::registerSegment` builds the segment buffer on the calling thread (outside the `scheduleWork` callback) inside a try/catch. On the thrown exception it logs an ERROR and returns early, so a missing segment never schedules evaluation and the throw no longer escapes. The buffer is carried into the callback as a `std::shared_ptr<const JSBigFileString>` (`RuntimeScheduler` callbacks are copyable `std::function`s; `evaluateJavaScript` takes `const shared_ptr<const Buffer>&`). This keeps file I/O off the JS thread.
- Dropped the inline `access(R_OK)` pre-check: the `open()` failure inside `fromPath` is the single source of truth, removing the redundant check and its TOCTOU window.
Changelog:
[iOS][Fixed] - Avoid an unhandled exception in bridgeless `ReactInstance::registerSegment` when an on-demand JS segment file is missing from the cache at lazy-load time
Reviewed By: zeyap, javache
Differential Revision: D114761643
|
@javache has exported this pull request. If you are a Meta employee, you can view the originating Diff in D114761643. |
|
This pull request has been merged in fff4994. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Harden the on-demand (lazily code-split) JS segment load path against a missing cache file.
When RN lazily loads an on-demand JS segment, the file lives in an OS-purgeable / LRU-evictable on-demand cache and can already be gone by the time the segment is registered. On a missing file
JSBigFileString::fromPaththrowsstd::runtime_error("... Could not open file: ..."), andReactInstance::registerSegmenthad no failure handling, so the throw surfaced as an unhandled JS exception and crashed the app.Fix (V4, addresses pieterdb's latest review feedback):
JSBigFileString::fromPath's throwing contract is now documented in the header: it throwsstd::runtime_errorwhen the file cannot be opened or read, and callers loading files that may legitimately be gone (an OS-purgeable / LRU-evictable cache) must catch and degrade gracefully. No behavior change tofromPathand no new factory.ReactInstance::registerSegmentbuilds the segment buffer on the calling thread (outside thescheduleWorkcallback) inside a try/catch. On the thrown exception it logs an ERROR and returns early, so a missing segment never schedules evaluation and the throw no longer escapes. The buffer is carried into the callback as astd::shared_ptr<const JSBigFileString>(RuntimeSchedulercallbacks are copyablestd::functions;evaluateJavaScripttakesconst shared_ptr<const Buffer>&). This keeps file I/O off the JS thread.access(R_OK)pre-check: theopen()failure insidefromPathis the single source of truth, removing the redundant check and its TOCTOU window.Changelog:
[iOS][Fixed] - Avoid an unhandled exception in bridgeless
ReactInstance::registerSegmentwhen an on-demand JS segment file is missing from the cache at lazy-load timeReviewed By: zeyap, javache
Differential Revision: D114761643