Skip to content

fix: dispatch HealthKit callback continuations to main thread#327

Merged
robertherber merged 2 commits intokingstinct:masterfrom
debitan:fix/main-thread-continuation-resume
Mar 11, 2026
Merged

fix: dispatch HealthKit callback continuations to main thread#327
robertherber merged 2 commits intokingstinct:masterfrom
debitan:fix/main-thread-continuation-resume

Conversation

@debitan
Copy link
Contributor

@debitan debitan commented Mar 9, 2026

Fixes #326

Summary

HealthKit completion handlers fire on arbitrary background dispatch queues. When using Nitro Modules, resuming a Swift continuation from a background thread causes the JSI bridge to access the Hermes JS runtime from a non-main thread, resulting in a SIGSEGV.

This PR wraps all continuation.resume(...) calls inside HealthKit callbacks with DispatchQueue.main.async, consistent with the existing pattern in subscribeToObserverQuery and queryStatisticsForQuantityInternal.

Changes

7 files, all in packages/react-native-healthkit/ios/:

  • CoreModule.swift - 8 callbacks fixed (getRequestStatusForAuthorization, requestAuthorization, querySources, enableBackgroundDelivery, disableBackgroundDelivery, disableAllBackgroundDelivery, deleteObjects, getPreferredUnitsInternal)
  • Helpers.swift - 3 callbacks fixed (sampleAnchoredQueryAsync, sampleQueryAsync, saveAsync)
  • ElectrocardiogramModule.swift - 1 callback fixed (getECGVoltages)
  • HeartbeatSeriesModule.swift - 1 callback fixed (getHeartbeatSeriesHeartbeats)
  • QuantityTypeModule.swift - 1 callback fixed (queryStatisticsCollectionForQuantityInternal)
  • WorkoutProxy.swift - 1 callback fixed (getRouteLocations)
  • WorkoutsModule.swift - 2 callbacks fixed (startWatchAppWithWorkoutConfiguration, saveWorkoutSample including nested store.add callback)

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 9, 2026

Open in StackBlitz

npm i https://pkg.pr.new/kingstinct/react-native-healthkit/@kingstinct/react-native-healthkit@327

commit: 2cdec47

@changeset-bot
Copy link

changeset-bot bot commented Mar 9, 2026

🦋 Changeset detected

Latest commit: 2cdec47

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@kingstinct/react-native-healthkit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@debitan debitan force-pushed the fix/main-thread-continuation-resume branch from 97842e1 to 1076e61 Compare March 9, 2026 16:31
debitan added 2 commits March 9, 2026 17:31
HealthKit completion handlers fire on arbitrary background dispatch
queues. When using Nitro Modules, resuming a Swift continuation from
a background thread causes the JSI bridge to access the Hermes JS
runtime from a non-main thread. Since Hermes is single-threaded and
not thread-safe, this results in a SIGSEGV crash.

Wrap all `continuation.resume(...)` calls inside HealthKit callbacks
with `DispatchQueue.main.async` to ensure they execute on the main
thread, consistent with the existing pattern in
`subscribeToObserverQuery`.

Affected files:
- CoreModule.swift
- Helpers.swift
- ElectrocardiogramModule.swift
- HeartbeatSeriesModule.swift
- QuantityTypeModule.swift
- WorkoutProxy.swift
- WorkoutsModule.swift
@debitan debitan force-pushed the fix/main-thread-continuation-resume branch from 1076e61 to 2cdec47 Compare March 9, 2026 16:32
@robertherber robertherber merged commit d55a540 into kingstinct:master Mar 11, 2026
7 checks passed
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.

SIGSEGV crash: HealthKit callbacks resume continuations on background thread

2 participants