sqlite_async.dart icon indicating copy to clipboard operation
sqlite_async.dart copied to clipboard

Memory corruption issues on isolates

Open xogno opened this issue 8 months ago • 11 comments

Edit from team: These issues are likely the same underlying problem:

  • https://github.com/powersync-ja/sqlite_async.dart/issues/129
  • https://github.com/powersync-ja/powersync.dart/issues/393
  • (maybe) https://github.com/simolus3/drift/issues/3771
  • https://discord.com/channels/1138230179878154300/1194710422960472175/1468475869147304149

Summary

We reproduced a matched iOS profile-mode crash on a physical iPhone while using:

  • powersync 1.18.0
  • powersync_core 1.8.0
  • sqlite_async 0.13.1
  • sqlite3 2.9.4
  • iPhone 16 Pro, iOS 26.2 (23C55)

The crash is not in the native PowerSync/sqlite libs. It is caught by LLDB on a DartWorker thread inside App.framework, and symbolication points into the Dart-side sqlite connection-isolate path:

  • sqliteConnectionIsolateInner.handle
  • sqliteConnectionIsolateInner.runStatement
  • PortServer._init.<anonymous closure>
  • mapParameters

Repro shape

This was reproduced from a clean install on-device using:

  1. flutter build ios --profile --no-tree-shake-icons
  2. xcrun devicectl device install app ... build/ios/iphoneos/Runner.app
  3. LLDB waiting attach:
    • xcrun lldb -o "device select <CoreDevice id>" -o "device process attach -w -n Runner -c"
  4. Launch:
    • xcrun devicectl device process launch --device <CoreDevice id> <bundle-id>

Matched repro 1

Fresh matched crash:

  • Runner-2026-04-04-172738.ips
  • matching App.framework / dSYM UUID:
    • BF2DD795-731F-3166-95C8-F2A21751E099

LLDB caught:

  • EXC_BAD_ACCESS
  • SIGSEGV
  • faulting thread: DartWorker
  • faulting address: 0x83f69a47
  • App load address: 0x110ce8000

Top App frames:

frame #0: App`___lldb_unnamed_symbol_2e3c84 + 216
frame #1: App`___lldb_unnamed_symbol_2e3c84 + 192
frame #2: App`___lldb_unnamed_symbol_2e3ba4 + 88
frame #3: App`___lldb_unnamed_symbol_2e3a44 + 236
frame #4: App`___lldb_unnamed_symbol_2e3908 + 216
frame #5: App`___lldb_unnamed_symbol_2d2400 + 268
...
frame #18: Flutter`dart::DartEntry::InvokeFunction(...)
frame #19: Flutter`dart::IsolateMessageHandler::HandleMessage(...)

Symbolicated App frames:

profilerNameAndArgs (profiler.dart)
TimeSync.timeSync (profiler.dart)
sqliteConnectionIsolateInner.handle (native_sqlite_connection_impl.dart:374)
sqliteConnectionIsolateInner.<anonymous closure> (#2) (native_sqlite_connection_impl.dart:398)
PortServer._init.<anonymous closure> (port_channel_native.dart:252)
RawReceivePort._handleMessage (isolate_patch.dart:194)

Follow-up mitigations tested

We tested two more fresh profile builds:

  1. Force profileQueries: false for the PowerSync sqlite open factory.
  2. Force maxReaders: 1.

Both still crashed.

Matched repro 2: profileQueries: false

  • fresh crash: Runner-2026-04-04-174331.ips
  • matching UUID:
    • FE3FCBDA-A0D7-3785-99DB-EF86CC72A0B8

Still crashed on DartWorker with:

  • EXC_BAD_ACCESS
  • SIGSEGV
  • faulting address 0x74623

Symbolicated top frames:

FollowerLayer.findAnnotations (layer.dart:2688)
mapParameters (shared_utils.dart)
sqliteConnectionIsolateInner.runStatement
sqliteConnectionIsolateInner.handle.<anonymous closure>
TimeSync.timeSync
sqliteConnectionIsolateInner.handle
sqliteConnectionIsolateInner.<anonymous closure> (#2)
PortServer._init.<anonymous closure>

Matched repro 3: maxReaders: 1

  • fresh crash: Runner-2026-04-04-174733.ips
  • matching UUID:
    • D6659CCA-F367-38CC-963D-ACC59C81E244

Still crashed on DartWorker with:

  • EXC_BAD_ACCESS
  • SIGSEGV
  • faulting address 0x7

LLDB top frame:

frame #0: App`___lldb_unnamed_symbol_7db774 + 84

This means the crash is not explained by:

  • query timeline profiling alone
  • the default multi-reader pool width alone

Why I think this belongs here

The matched symbolication consistently lands in sqlite_async’s Dart-side isolate / port-channel / parameter-mapping codepath, not in app-specific feature code and not in the native sqlite / PowerSync extension binaries.

The strongest recurring frames are:

  • sqliteConnectionIsolateInner.runStatement
  • sqliteConnectionIsolateInner.handle
  • PortServer._init.<anonymous closure>
  • mapParameters

Question

Does this look like a known iOS profile-mode issue in the sqlite isolate path on sqlite_async 0.13.x?

If not, what would you recommend as the next best narrowing step?

  • instrument runStatement / mapParameters for the exact SQL + parameter types?
  • try an older sqlite_async line against the same app code?
  • avoid the isolate-backed connection implementation for PowerSync on iOS profile?

xogno avatar Apr 04 '26 15:04 xogno

We ran a second iOS profile repro with our app startup heavily minimized behind a gate:

ORAKEMU_IOS_PROFILE_MINIMAL_POWERSYNC_STARTUP=true

Under that gate we skipped/delayed our app-owned startup work, including:

  • CalendarDotsGlobalWatch.attach()
  • LiveSurfaceGlobalWatch.attach()
  • startup rcconnectToRC()
  • the startup repair query
  • initpowersync() until after the first frame

Result: the app still crashes in the same place.

Observed on the gated repro:

  • LLDB caught EXC_BAD_ACCESS
  • crashing thread: DartWorker
  • faulting address: 0x83f69a47
  • fresh App.framework/App UUID: C93F2127-8949-3272-992F-33E2343AA6E6
  • top symbolicated frames are still:
    • profilerNameAndArgs
    • TimeSync.timeSync
    • sqliteConnectionIsolateInner.handle
    • sqliteConnectionIsolateInner.<anonymous closure> (#2)
    • PortServer._init.<anonymous closure>

This seems to rule out our app’s heavier startup sequencing as the trigger, or at least makes it much less likely. The crash remains reproducible even with most of our integration-side startup work removed.

If helpful, I can also provide the exact LLDB transcript for this gated repro. For now, the key takeaway is that a minimal iOS/profile startup path still crashes in the same sqlite_async isolate/port path.

xogno avatar Apr 04 '26 17:04 xogno

We've seen one other report of this, but so far we haven't been able to track this down. My best guess at this point is that something, probably in the sqlite3 package, corrupts the heap which later causes a crash in another place (I really don't see how mapParameters could call findAnnotations in some Flutter rendering library).

One thing potentially worth trying could be to enable asan when compiling your app. I've not tried that with a Flutter app before though, I'm not sure if something in Dart might be incompatible with it. But if this comes from the ffi layer with SQLite, that might catch it.

simolus3 avatar Apr 04 '26 19:04 simolus3

It's not just on iOS devices, but also on Android devices. The application closes immediately when synchronization starts. If you are on the login screens, the application works normally, but as soon as I get the token and start the synchronization process, the application is killed and exits, and it doesn't work at all.

SadiqCyber avatar Apr 05 '26 13:04 SadiqCyber

I reported the same error more than half a month ago, but I have not received a response to address this issue. The application is nearing its delivery deadline, and this is a major problem.

https://github.com/powersync-ja/sqlite_async.dart/issues/129#issue-4076680628

SadiqCyber avatar Apr 05 '26 13:04 SadiqCyber

https://github.com/powersync-ja/powersync.dart/issues/393#issuecomment-4189404058

check my issue.. it might help. we checked commit by commit until we identified the issue

sparrow58 avatar Apr 05 '26 19:04 sparrow58

To give an update on this, I've started testing the sqlite3 package with AOT builds (previously, tests were only running through JIT). This is a bit annoying to set up, but we can likely do that for sqlite_async and the PowerSync SDK as well. I've also set up tests with sanitizers (asan, msan and tsan). However, none of these found any issues in the sqlite3 package itself, which is the only package here doing actual FFI. We also run a few tests for the PowerSync SQLite core extension with sanitizers, but since that's written in Rust I think the likelihood of memory issues originating from there is unlikely.

We are currently working on a major refactor of the sqlite_async package and the PowerSync Dart SDK, we should hopefully have a beta release of that ready in a few days. Those pending changes also affect how we manage isolates and concurrency, so it's possible those might fix this.

However, the fact that this only happens on release builds is really odd. There's no difference between debug and release builds for native libraries, so this must be coming from Dart. And Dart doesn't have the undefined behavior footguns we might have with native libraries, so we should expect debug mode builds to behave identically. Finally, it's worrying that this can be worked around with Dart code changes (see the reports from @sparrow58 and also the Discord thread linked in the issue where changing some widget setup related to riverpod fixes this). So at this point it seems possible that this is a Flutter/Dart VM or compiler bug.

It would be very helpful if those still running into this could report the following:

  1. The Flutter version in use.
  2. How consistently you run into this (on every release mode app start, a few times in crashlogs in the wild, ...).

If it turns out to be a Dart bug, we'll likely have to come up with a small app reproducing this that we could share with the Dart/Flutter team. So far I haven't been able to reproduce this, so if any of you run into this with source code you might be willing to share, that would be very helpful.

simolus3 avatar Apr 07 '26 07:04 simolus3

@simolus3 what's the best way to reach out to you and share source code? I spend many hours trying to debug this and burning tokens on this (kinda burned out on the issue 😅) so I'd gladly share source code.

xogno avatar Apr 08 '26 10:04 xogno

Does your app crash every time when you start it? If you have a repository on GitHub you could add me to that, otherwise you could also upload a zip to google drive and share it with [email protected]. If it needs additional setup instructions you could also email those to me, I can share them with the team to reproduce this.

simolus3 avatar Apr 08 '26 11:04 simolus3

Yes it crashes each time in release and profile mode (Flutter 3.41.4) I invited you to the private repo and sent instructions and extra details by email. Thank you for looking into this.

xogno avatar Apr 08 '26 15:04 xogno

A quick update: So far we haven't been able to diagnose a specific root cause. But in the app that @orakemu shared with me, I was able to consistently reproduce the issue with the latest 1.x release of the powersync package and the app starts with the 2.0.0-wip.0 pre-release.

It would be helpful to know if upgrading to powersync: ^2.0.0-wip.0 resolves the issue for you or if you're still running into problems. Please note that we're still working on that version though. We expect to have a stable release for that next week, but trying the pre-release version to see if it fixes the issue for you would be helpful.

simolus3 avatar Apr 10 '26 08:04 simolus3

iPhone 15 Pro + iOS 26.5 + 2.0.0-wip.0 fixed the crash for me

ttcat avatar Apr 12 '26 00:04 ttcat

Hi @simolus3,

I've put together a small standalone demo repo that reproduces a release-only SQLite transaction issue related to what we're seeing here:

https://github.com/sparrow58/sqlite_tx_dynamic_bug_demo

Branches main — contains the bug. tx is typed as dynamic in write transaction paths. Debug works; release fails (e.g. RangeError when correcting a voucher). fixed — contains the fix. dynamic tx is replaced with SqliteWriteContext across all write paths inside transactions, exported from lib/core/database/sqlite_write_context.dart. With compile-time typing, release behaves the same as debug. What this demonstrates This repo is intentionally minimal — a local POS-style Flutter app with vouchers, customers, invoices, etc. — so the problem is easy to see and test:

Bug (main)

git checkout main flutter run --release

Fix (fixed)

git checkout fixed flutter run --release On main, the app crashes in release because dynamic dispatch can route to the wrong implementation at runtime. On fixed, voucher correction (and other write transactions) succeed in release.

Caveat This demo covers one concrete scenario we hit in production-like code. There may be other cases where a dynamic transaction handle causes release-only failures — we don't have a full list. The underlying lesson is the same: keep tx strongly typed (e.g. SqliteWriteContext) in all write transaction paths so Dart resolves calls at compile time.

This is meant as an example of the class of problem we're investigating, not a claim that it explains every crash signature in this issue (SIGSEGV/SIGABRT during initialize() may have additional causes).

Happy to share more context from the real project if useful.

sparrow58 avatar Jun 10 '26 19:06 sparrow58

Closing since this appears to be fixed with version 2.0.0 of the PowerSync SDK and 0.14.0 of sqlite_async.

simolus3 avatar Jun 11 '26 09:06 simolus3