From 51faa1863f535a0037b280e072e823e2fb01f5f2 Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Wed, 9 Aug 2023 09:29:32 -0700 Subject: [PATCH] More robust handling of errors when trying to load JS bundle Summary: ## Changelog: [Internal] - When trying to load JS bundle, there were conditions under which it could just silently fail, not giving much to start with when troubleshooting. It was relying on Folly internals to handle the error, and depending on the context it could either throw an exception or just silently exit the process if exceptions are not enabled in Folly. This diff makes this error handling more explicit, with a clear error message in the log. Reviewed By: NickGerleman Differential Revision: D48147690 fbshipit-source-id: 1bb08ad17a880989e829c281fe25ee0d4a385d59 --- .../ReactCommon/cxxreact/JSBigString.cpp | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/packages/react-native/ReactCommon/cxxreact/JSBigString.cpp b/packages/react-native/ReactCommon/cxxreact/JSBigString.cpp index 436aeec9e3a..ca2a41ebcc2 100644 --- a/packages/react-native/ReactCommon/cxxreact/JSBigString.cpp +++ b/packages/react-native/ReactCommon/cxxreact/JSBigString.cpp @@ -10,7 +10,6 @@ #include #include -#include #include #include #include @@ -22,7 +21,14 @@ namespace facebook::react { JSBigFileString::JSBigFileString(int fd, size_t size, off_t offset /*= 0*/) : m_fd{-1}, m_data{nullptr} { - folly::checkUnixError(m_fd = dup(fd), "Could not duplicate file descriptor"); + m_fd = dup(fd); + + if (m_fd == -1) { + const char *message = + "JSBigFileString::JSBigFileString - Could not duplicate file descriptor"; + LOG(ERROR) << message; + throw std::runtime_error(message); + } // Offsets given to mmap must be page aligned. We abstract away that // restriction by sending a page aligned offset to mmap, and keeping track @@ -83,15 +89,29 @@ int JSBigFileString::fd() const { std::unique_ptr JSBigFileString::fromPath( const std::string &sourceURL) { int fd = ::open(sourceURL.c_str(), O_RDONLY); - folly::checkUnixError(fd, "Could not open file", sourceURL); - SCOPE_EXIT { - CHECK(::close(fd) == 0); - }; - struct stat fileInfo; - folly::checkUnixError(::fstat(fd, &fileInfo), "fstat on bundle failed."); + if (fd == -1) { + const std::string message = + std::string("JSBigFileString::fromPath - Could not open file: ") + + sourceURL; + LOG(ERROR) << message; + throw std::runtime_error(message.c_str()); + } - return std::make_unique(fd, fileInfo.st_size); + struct stat fileInfo {}; + int res = ::fstat(fd, &fileInfo); + + if (res == -1) { + const std::string message = + "JSBigFileString::fromPath - fstat on bundle failed: " + sourceURL; + LOG(ERROR) << message; + ::close(fd); + throw std::runtime_error(message.c_str()); + } + + auto ptr = std::make_unique(fd, fileInfo.st_size); + CHECK(::close(fd) == 0); + return ptr; } } // namespace facebook::react