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

Windows build - Lost connection with device #687

Closed
nelchael730 opened this issue Sep 19, 2022 · 20 comments
Closed

Windows build - Lost connection with device #687

nelchael730 opened this issue Sep 19, 2022 · 20 comments

Comments

@nelchael730
Copy link

It appears as though storing a string over 7500 characters long (an encoded json string in this case) silently crashes Windows builds. The only message logged is "Lost connection with device".

@simc
Copy link
Member

simc commented Sep 19, 2022

What version of Isar are you using?

What version of Flutter are you using?

Do you also see this issue for other platforms?

Is it a specific string or does it happen for any?

Is the string indexed?

@nelchael730
Copy link
Author

Sorry for the lack of detail.

I was previously using 3.0.0-dev.8, issues occurred after upgrade to 3.0.0. I have switched back to dev-8 to fix the issue for now.

  • What version of Flutter are you using?
    Flutter 3.4
    (Initially I was on Flutter stable channel, switched to master after reading other issues related to "Lost connection with device". Also updated VS to 2022 to update to the latest C++. These updates did not fix the issue.)

  • Do you also see this issue for other platforms?
    Issue not seen on other platforms I'm building for - android emulator versions work fine.

  • Is it a specific string or does it happen for any?
    Any as far as I can tell.

  • Is the string indexed?
    It is not...

I'm sorry for posting this with a lack of detail. I'll test a bit more later to try and figure out what's happening, I'm up against it right now so reverting to previous version and continuing my work for the day.

I'll try it on another laptop as well.

@simc
Copy link
Member

simc commented Sep 19, 2022

Thank a lot! Really appreciate your help!

@i-dentify
Copy link

I can confirm this error also within an iOS Simulator AND Android Emulator. For me the error also happens with dev.8. I am using Android Studio on MacOS. The error occurs, when awaiting Isar.open().

class DbContext {
  static DbContext? _instance;
  late Future<Isar> database;

  DbContext._() {
    database = _openDatabase();
  }

  Future<Isar> _openDatabase() async {
    if (Isar.instanceNames.isEmpty) {
      final dir = await getApplicationSupportDirectory();
      return await Isar.open(
          [
            DeliverySchema,
            DeliveryAreaSchema
          ],
          directory: dir.path,
          inspector: kDebugMode
      );
    }

    return Future.value(Isar.getInstance());
  }

  factory DbContext() => _instance ??= DbContext._();
}

And in main():

    final dbContext = DbContext();
    final database = await dbContext.database; // <--- error happens here

Furthermore: I am using runZoneGuarded with a specified onError callback. The app stops without even hitting the first line of that callback.

@simc
Copy link
Member

simc commented Sep 20, 2022

@i-dentify but is it somehow related to the 7500 character issue or a new error? What error message do you see in iOS or Android logs?

Your error does not sound like an Isar issue to me 🤔

@i-dentify
Copy link

@leisim Oh damn - I see, that there was this more detailed info in the initial post... I just replied as I have the same final error behaviour then. I do not see ANY error except the message Lost connection to device. And this definitely always happens, when awaiting Isar.open() - I put that specific part at several places within my main() method to ensure, that it's not because of some delayed error of another piece of code.

@simc
Copy link
Member

simc commented Sep 20, 2022

Android locgat shows more information since it does not stop on error.

The iOS simulator would show a crash report if Isar was killing the app

@nelchael730
Copy link
Author

nelchael730 commented Sep 20, 2022

I've done a bit more testing though I'm a bit rushed so haven't had time to make an example app - I've been on this current app about 2 months so its a bit of a code swamp...

I suspected that the json strings I was storing might of contained an unusual character or some other encoding issue, but my quick testing has confirmed that the issue exists with any string - my additional test cases were simple strings (AaaaBbbb etc up to 7500) and a base64 encoded image, both crashed the app.

With no errors being output (only the "lost connection" warning), it's hard to trace back. It seems like String columns have a character limit, but since previous versions of Isar did not have any limitations I would hazard a guess and say it feels like a memory issue.

I'll put together a small test app this weekend to test further since my current code examples are not useful for replicating the issue.

@i-dentify
Copy link

OK - I tried multiple things as well and I'd finally go along which @nelchael730 guess on a memory issue. The error occurs on my side, since I moved from IsarLink to @embedded. So - what previously has been an entity with a link to hundreds of other entities is now an entity with an embedded list of hundreds of entities. Weird thing though: the error occurs even when I did not run any query at all - the error happened directly on Isar.open(). I now switched back to yesterday's state - where I used IsarLink - and no error at all comes up.

(btw: the reason why I switched to @embedded: I simply did not get the links working - I somehow messed things up there.)

@simc
Copy link
Member

simc commented Sep 20, 2022

@nelchael730 a few lines of test code that show the error would be super helpful. thanks so much!

@noga-dev
Copy link

Windows here. When trying to store 10869 bytes the app silently crashes.

image

Specifically this is the last line of code to run before the silent crash:

  int isar_put_all(
    ffi.Pointer<CIsarCollection> collection,
    ffi.Pointer<CIsarTxn> txn,
    ffi.Pointer<CObjectSet> objects,
  ) {
    return _isar_put_all(
      collection,
      txn,
      objects,
    );
  }

The param values are:
collection: Pointer (Pointer<CIsarCollection>: address=0x1837fe89850) (1664298293328)
txn: Pointer (Pointer<CIsarTxn>: address=0x1831521e9c0) (1662506887616)
objects: Pointer (Pointer<CObjectSet>: address=0x1831524e630) (1662507083312)

image

@simc
Copy link
Member

simc commented Sep 24, 2022

@noga-dev do you have a sample that reproduces this error? I fail to find one on my machine :(

@simc
Copy link
Member

simc commented Sep 26, 2022

Could you please remove the database and try disabling Windows Security temporarily? It kills my build sometimes

@noga-dev
Copy link

@noga-dev do you have a sample that reproduces this error? I fail to find one on my machine :(

@vothvovo

@nelchael730
Copy link
Author

nelchael730 commented Oct 11, 2022

OK, i'm back... Sorry I wasn't able to provide any example code, I was ill most of last month.

I've done more testing. From what I can determine it is either not related to string length or I've found a separate issue that is resulting in the same "Lost connection to device." issue.

How to replicate:

start app (windows)
open database inspector
click on floating + button
see database rows added
delete any/all rows from database via inspector
click on floating + button
silent crash

You can restart the app etc and it will always crash. The only way to get it working again is to deleteFromDisk and restart the app. It will work until you delete rows and then forever crashes.

main.dart

import 'dart:math';

import 'package:apptest/collections/bigdata.dart';
import 'package:flutter/material.dart';
import 'package:isar/isar.dart';
import 'package:path_provider/path_provider.dart';

// random string generator
const _chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
Random _rnd = Random();
String getRandomString(int length) => String.fromCharCodes(
      Iterable.generate(
        length,
        (_) => _chars.codeUnitAt(
          _rnd.nextInt(_chars.length),
        ),
      ),
    );

Future<void> main() async {
  // get box directory
  final dir = await getApplicationSupportDirectory();
  // open isar db
  var isar = await Isar.open(
    name: 'IsarTest',
    [BigdataSchema],
    directory: dir.path,
    inspector: true,
  );
  // isar.close(deleteFromDisk: true);

  runApp(MyApp(isar: isar));
}

class MyApp extends StatelessWidget {
  final Isar isar;
  const MyApp({
    super.key,
    required this.isar,
  });

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(isar: isar),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({
    super.key,
    required this.isar,
  });
  final Isar isar;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  // create instance and run
  void _pushData() async {
    String string1 = getRandomString(1500);

    final newData = Bigdata()
      ..data1 = string1
      ..data2 = string1
      ..data3 = string1;

    await widget.isar.writeTxn(() async {
      await widget.isar.bigdatas.put(newData); // insert & update
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: const Text('Test PAge Description'),
      floatingActionButton: FloatingActionButton(
        onPressed: _pushData,
        child: const Icon(Icons.add),
      ),
    );
  }
}

bigdata.dart

import 'package:isar/isar.dart';

part 'bigdata.g.dart';

@collection
class Bigdata {
  Id id = Isar.autoIncrement; // you can also use id = null to auto increment

  late String data1;
  late String data2;
  late String data3;
}

@nelchael730
Copy link
Author

nelchael730 commented Nov 4, 2022

Is there any progress with this?

To be honest I'm fine using the dev version but i'm now at a point where i'm struggling with not having access to the isar inspector since it stopped working when version 3 was released...

Is ther any way at all to use the inspector with the dev versions anymore?

@ductranit
Copy link

I'm having the same issue. Can't run on windows because my data is very large.
I can see someone post the sample to reproduce here #827 (comment)
Just wonder do we have any workaround now?

@simc
Copy link
Member

simc commented Nov 11, 2022

A fix is ready. I'll release a new version very soon!

@simc simc closed this as completed Nov 11, 2022
@hiennguyen1001
Copy link

A fix is ready. I'll release a new version very soon!

@leisim I've updated Isar version 3.0.3 for this example and run a Window build, it crashes the first time you open an Isar instance. Please check your new version with the example above.

@StevenGolhen
Copy link

I also confirm, that the fix did not solve the problem either with my example #827
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants