Title: Make LiveSync writes atomic instead of locking the runtime's reads by edusperoni · Pull Request #2022 · NativeScript/android · GitHub
Open Graph Title: Make LiveSync writes atomic instead of locking the runtime's reads by edusperoni · Pull Request #2022 · NativeScript/android
X Title: Make LiveSync writes atomic instead of locking the runtime's reads by edusperoni · Pull Request #2022 · NativeScript/android
Description: Description m_fileWriteMutex paired LiveSync's file writes against Runtime::ReadFileText, so the runtime would not compile a half-written hot-synced module. It could not deliver that, for three independent reasons: Wrong scope. It is a non-static member (Runtime.h), so LiveSync locking one runtime excludes nothing on a worker's thread — precisely the case that matters now that workers run on their own detached threads. Taken after the damage. The lock landed after prepareFile() deleted the file and the FileOutputStream constructor truncated it, so it covered only fos.write and not the window a reader actually hits. Unmatched unlock. The finally called runtime.unlock() even when lock() had never been reached (a throw from mkdirs() or the FileOutputStream constructor). Unlocking a std::mutex this thread does not own is UB, and on bionic can release a lock the JS thread holds inside ReadFileText. Rather than fix the locking, this inverts the approach: make the write atomic instead of locking the readers. Writes now go to a sibling temp file which is renamed over the target. rename(2) within a directory is atomic, so a reader gets either the whole previous file or the whole new one — on every runtime and every thread, with no shared state to scope wrongly. That also closes the delete/truncate window that no reader-side lock could have covered, and takes a mutex off every debug module read. java.nio.file's ATOMIC_MOVE is unavailable at minSdk 21 (it needs 26), so this uses File.createTempFile(prefix, suffix, parentDir) plus File.renameTo, which maps to rename(2) on every API level. The temp file must be a sibling — rename is only atomic within one filesystem. m_fileWriteMutex, Runtime::Lock/Unlock, both lock_guards, the two JNI entry points and the private native lock/unlock(int) declarations are removed. com.tns.Runtime.lock()/unlock() remain as @Deprecated no-ops, since they are public API and external tooling may call them. Current behaviour: LiveSync truncates the target in place and takes a per-runtime mutex around part of the write; readers on other runtimes are unprotected, and an early throw unlocks a mutex the thread does not hold. New behaviour: LiveSync writes atomically; readers never observe a partial file, and there is no lock on the read path. Found while removing File::Buffer in 7c9c3db — this mutex superficially looked like it guarded that shared buffer. It never did. Does your commit message include the wording below to reference a specific issue in this repo? Fixes the m_fileWriteMutex item in #2020. Related Pull Requests Follows 7c9c3db on main (process-global state shared across isolates), which surfaced this. Does your pull request have unit tests? No. The test suite does not exercise LiveSync at all — it is a debug socket service driven by the CLI over a LocalServerSocket, with no harness in the runtime test app. So the green run below proves the code builds and nothing else regressed, not that hot-sync still works. Verified: :runtime:externalNativeBuildDebug -Pabis=arm64-v8a — compiles and links clean ./gradlew runtestsAndVerifyResults — 1038/1038 passed, 0 failed Manual check still needed before merge: ns run android on a device/emulator Edit a .js/.ts file in the app while it runs Confirm the file hot-syncs and the app reloads (i.e. createOrOverrideFile → renameTo succeeds and DO_SYNC_OPERATION still triggers livesync.js) Worth also confirming a new file (not just an overwrite) and a deleted file, since prepareFile() is gone Summary by CodeRabbit Bug Fixes Improved runtime file updates by writing changes safely and replacing files atomically. Automatically creates missing parent directories during file updates. Cleans up temporary files when an update fails. Removed obsolete file-locking behavior that could interfere with runtime file access.
Open Graph Description: Description m_fileWriteMutex paired LiveSync's file writes against Runtime::ReadFileText, so the runtime would not compile a half-written hot-synced module. It could not deliver that, for three...
X Description: Description m_fileWriteMutex paired LiveSync's file writes against Runtime::ReadFileText, so the runtime would not compile a half-written hot-synced module. It could not deliver that, for t...
Opengraph URL: https://github.com/NativeScript/android/pull/2022
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:28bd1ccb-8716-7540-945b-eca41e3fdc6c |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | DFC8:1D86C4:15B7E31:1B9D2B7:6B0131E4 |
| html-safe-nonce | e89b5eed727fc21b76ce9fcf7cbd4296634b24e143c875bef7d81b51b5067de6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERkM4OjFEODZDNDoxNUI3RTMxOjFCOUQyQjc6NkIwMTMxRTQiLCJ2aXNpdG9yX2lkIjoiNjQ3NzU3MTEzMzg5MjgwMDk5NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e55b727914cd5154a06ac6681f81a9ce081780e91463d197045ee72efa6a38ac |
| hovercard-subject-tag | pull_request:4325724171 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/NativeScript/android/pull/2022/files |
| twitter:image | https://avatars.githubusercontent.com/u/4061767?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/4061767?s=400&v=4 |
| og:image:alt | Description m_fileWriteMutex paired LiveSync's file writes against Runtime::ReadFileText, so the runtime would not compile a half-written hot-synced module. It could not deliver that, for three... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/NativeScript/android git https://github.com/NativeScript/android.git |
| octolytics-dimension-user_id | 7392261 |
| octolytics-dimension-user_login | NativeScript |
| octolytics-dimension-repository_id | 29137950 |
| octolytics-dimension-repository_nwo | NativeScript/android |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 29137950 |
| octolytics-dimension-repository_network_root_nwo | NativeScript/android |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width