Skip to content

fix(harmony): safeguard binary version sync against background-thread resource loading failures - #592

Merged
sunnylqm merged 1 commit into
masterfrom
fix/harmony-background-thread-safe-final
Jun 30, 2026
Merged

fix(harmony): safeguard binary version sync against background-thread resource loading failures#592
sunnylqm merged 1 commit into
masterfrom
fix/harmony-background-thread-safe-final

Conversation

@sunnylqm

@sunnylqm sunnylqm commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

This PR implements a safety guard and caching layer for binary version synchronization to prevent update state wipes during soft reloads:

Background & Issue

During a soft reload, the React Native container initializes its TurboModules (including Pushy) on a background JS thread. The UpdateContext constructor calls syncStateWithBinaryVersion() which tries to read meta.json synchronously via resourceManager.getRawFileContentSync('meta.json').
In HarmonyOS, synchronous resource manager APIs are not thread-safe and throw exceptions if called from non-UI threads. The exception is silently caught, returning an empty string "". Because this empty string doesn't match the previously stored build time, the system assumes the binary has been updated and clears the entire update Preferences.

Solution

  1. Safety Guard: Added a check at the top of syncStateWithBinaryVersion():
    if (!packageVersion || !buildTime) {
      return;
    }
    If the current version/build time cannot be read (e.g. background thread loading failures), it immediately returns early instead of triggering a false-positive mismatch and wiping preferences.
  2. Static Memory Cache: Retained the caching of packageVersion and buildTime inside static class fields during the first resolution (which always runs on the Main/UI thread in the bundle provider).

Unit tests pass successfully.

Summary by CodeRabbit

  • Bug Fixes
    • Improved version and build-time handling so the app now reuses cached values when available.
    • Prevented state sync from running when required version information is missing.
    • Added clearer error logging for version and build-time lookup issues, helping diagnose failures more easily.

@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0446179e-504e-4a86-885e-0c966be594b8

📥 Commits

Reviewing files that changed from the base of the PR and between 9d116aa and a201037.

📒 Files selected for processing (1)
  • harmony/pushy/src/main/ets/UpdateContext.ts

📝 Walkthrough

Walkthrough

UpdateContext adds static cache fields for package version and build time. getPackageVersion and getBuildTime return cached values on hit and populate the cache on miss with error logging. syncStateWithBinaryVersion now short-circuits when either value is falsy.

Changes

UpdateContext Caching and Sync Guard

Layer / File(s) Summary
Static cache fields and cached getters
harmony/pushy/src/main/ets/UpdateContext.ts
Adds two static fields to cache resolved package version and build time. getPackageVersion and getBuildTime return cached values on hit; on miss they fetch/parse, store the result, and log errors on failure.
syncStateWithBinaryVersion early-exit guard
harmony/pushy/src/main/ets/UpdateContext.ts
syncStateWithBinaryVersion now returns early when packageVersion or buildTime is falsy, skipping native sync with incomplete values.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

Poem

🐇 A cache for the version, a cache for the time,
No more repeated fetches — how sublime!
If build time is empty, we skip with a hop,
Log the error politely and come to a stop.
The rabbit approves this tidy little change! 🌿

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/harmony-background-thread-safe-final

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

harmony/pushy/src/main/ets/UpdateContext.ts

Oops! Something went wrong! :(

ESLint: 8.57.1

Error: .eslintrc.js » @react-native/eslint-config#overrides[4]:
Environment key "jest/globals" is unknown

at /node_modules/.pnpm/@eslint+eslintrc@2.1.4/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2079:23
at Array.forEach (<anonymous>)
at ConfigValidator.validateEnvironment (/node_modules/.pnpm/@eslint+eslintrc@2.1.4/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2073:34)
at ConfigValidator.validateConfigArray (/node_modules/.pnpm/@eslint+eslintrc@2.1.4/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2223:18)
at CascadingConfigArrayFactory._finalizeConfigArray (/node_modules/.pnpm/@eslint+eslintrc@2.1.4/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3985:23)
at CascadingConfigArrayFactory.getConfigArrayForFile (/node_modules/.pnpm/@eslint+eslintrc@2.1.4/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3791:21)
at FileEnumerator._iterateFilesWithFile (/node_modules/.pnpm/eslint@8.57.1/node_modules/eslint/lib/cli-engine/file-enumerator.js:368:43)
at FileEnumerator._iterateFiles (/node_modules/.pnpm/eslint@8.57.1/node_modules/eslint/lib/cli-engine/file-enumerator.js:349:25)
at FileEnumerator.iterateFiles (/node_modules/.pnpm/eslint@8.57.1/node_modules/eslint/lib/cli-engine/file-enumerator.js:299:59)
at iterateFiles.next (<anonymous>)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sunnylqm
sunnylqm merged commit 8168ae5 into master Jun 30, 2026
3 of 4 checks passed
liusanhong pushed a commit to liusanhong/react-native-update that referenced this pull request Jun 30, 2026
* 'master' of github.com:reactnativecn/react-native-update:
  fix(harmony): safeguard binary version sync against thread-safety errors and cache buildTime (reactnativecn#592)
  update expo 54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant