Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
vinicius0026 opened this issue Feb 8, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@vinicius0026
Copy link

vinicius0026 commented Feb 8, 2022

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)
@vinicius0026 vinicius0026 added the bug Something isn't working label Feb 8, 2022
@greenrobot
Copy link
Member

Related: #376

@greenrobot-team
Copy link
Member

Maybe some more details: #376 introduces a new attach API to Store that would keep the internal store open until all references are closed. So in above example it would be safe to close the store in one isolate while it still might be used in another.

@greenrobot-team
Copy link
Member

greenrobot-team commented May 24, 2022

Closing this issue due to inactivity. 💤 Feel free to comment with more details or submit a new issue.

Assuming the new attach API prevents this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants