Skip to content

App crashes with EXC_BAD_ACCESS when a closed store is accessed in an isolate #379

@vinicius0026

Description

@vinicius0026

ObjectBox crashes the app if the store is accessed in an Isolate (using the store reference to get access to the store) after the store is closed in the main Isolate, with a EXC_BAD_ACCESS error.

Basic info:

  • ObjectBox version: 1.3.0
  • Flutter SDK: 2.10.0
  • Dart SDK: 2.16.0
  • Null-safety enabled: yes
  • Reproducibility: always
  • OS: macOS Monterey
  • Device/Emulator: Simulator (but it's also reproducible in a physical device)

Steps to reproduce

  1. Open the store
  2. Spawn an isolate that accesses the store, passing the store.reference and building the store in the Isolate with Store.fromReference.
  3. Close the store in the main isolate
  4. In the spawned isolate, try to write some data to the store after it has been closed in the main isolate - use Future.delayed for that.

Expected behavior

An exception should be thrown, but the app shouldn't crash.

Code

This is reproducible with this minimal app, just click the Run button.

import 'dart:isolate';

import 'package:exec_bad_access/objectbox.g.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: ObjectBoxTest(),
    );
  }
}

@Entity()
class User {
  int id = 0;
  String name = '';
}

class ObjectBoxTest extends StatelessWidget {
  const ObjectBoxTest({ Key? key }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.max,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        ElevatedButton(
          onPressed: () async {
            print('open store');
            final store = await openStore();
            print('spawn isolate');
            await Isolate.spawn(backgroundTask, store.reference);
            print('wait for the isolate to start running before closing the store');
            await Future.delayed(const Duration(seconds: 2));
            print('close the store');
            store.close();
          },
          child: const Text('Run')
        ),
      ]
    );
  }
}

void backgroundTask(ByteData storeRef) async {
  print('rebuild the store from the reference');
  Store store = Store.fromReference(getObjectBoxModel(), storeRef);
  print('get the box');
  final box = store.box<User>();
  print('wait 10 seconds - this will run after the store is closed in the main isolate');
  await Future.delayed(const Duration(seconds: 10));
  print('create user');
  final user = User();
  user.name = 'Test';
  box.put(user);
}

This is the pubspec.yaml:

name: exec_bad_access
description: A new Flutter project.

publish_to: 'none'
version: 1.0.0+1
environment:
  sdk: ">=2.16.0 <3.0.0"
dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.2
  objectbox: ^1.3.0
  objectbox_flutter_libs: ^1.3.0
dev_dependencies:
  flutter_test:
    sdk: flutter
  build_runner: ^2.1.4
  objectbox_generator: any
  flutter_lints: ^1.0.0
flutter:
  uses-material-design: true

Logs, stack traces

This error is visible only by running the app in xcode:

DartWorker (29): EXC_BAD_ACCESS (code=1, address=0x6d69547472617723)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions