mongo-dart / mongo_dart

Mongo_dart: MongoDB driver for Dart programming language
https://pub.dev/packages/mongo_dart
MIT License
446 stars 98 forks source link

MongoDart Error: No master connection #298

Closed 61soldiers closed 1 year ago

61soldiers commented 2 years ago

Stack trace

MongoDart Error: No master connection 
#0      _ConnectionManager.masterConnectionVerified (package:mongo_dart/src/network/connection_manager.dart:19)
#1      Db._masterConnectionVerifiedAnyState (package:mongo_dart/src/database/db.dart:198)
#2      Db._masterConnectionVerified (package:mongo_dart/src/database/db.dart:191)
#3      DbCollection.findOne (package:mongo_dart/src/database/dbcollection.dart:134)

This error comes when running with a compiled (AOT) dart app. Works fine on JIT. UPDATE: I can confirm that this error comes up on both AOT and JIT

Here's the the initialization

final db = await Db.create(confidential.dbUri);
await db.open();
giorgiofran commented 1 year ago

It is really difficoult to say. The mesasge normally means that in the list of the servers provided there is not the primary (for a replica set). The fact that it doesn't work only when compiled is really strange. I'm sorry, but I do not really know how to help you. Maybe you can do some more test .... Sometimes the primary changes between the servers and mongo_dart, at present, is not able to detect this.

61soldiers commented 1 year ago

Found an old issue (duplicate?) in which you have commented #269 I called findOne in a loop (with a try catch in relation to what you commented in #269) before my app started and it now seems to work

I specified in the loop that if the findOne call failed, close db and retry again. Don't know if this really is a temporary or a permanent fix but for now it seems good.

61soldiers commented 1 year ago

Seemed to work the first time i tried it, but the method above doesn't work. This is really frustrating, i'm kinda out of options now.

giorgiofran commented 1 year ago

Which is the error? Where and when it happens? I'd need more info ...

61soldiers commented 1 year ago

It only happens when running the app AOT compiled and calling findOne. My app is a discord bot btw. Heres how i create the DB instance

final db = await Db.create(confidential.dbUri);
await db.open();

Theres really nothing much for context, just that this error comes up when doing any call (i assume) to DB.

Future<PointsDoc?> getPoints(String guildId, String discordId) async {
  return PointsDoc.fromJson(await Globals.pointsCol
      .findOne(where.eq('guildId', guildId).eq('discordId', discordId)));
}
giorgiofran commented 1 year ago

Try to put a db.isConnected before returning to see which is the value.

61soldiers commented 1 year ago

Will try that, but in the meantime here's an update

I can confirm that this error comes even in JIT compiled

61soldiers commented 1 year ago

Update again: you are not going to believe this, but i just created a new db user in mongodb cloud and copied the db URI from the connect button on the homepage just to be sure, and its working. Though its very unpredictable, because it worked randomly before.

61soldiers commented 1 year ago

After a few hours of running the bot, it just stopped working again. Gives the same master connection error.

61soldiers commented 1 year ago

Sometimes the primary changes between the servers and mongo_dart, at present, is not able to detect this.

Is this planned or on your roadmap by any chance ?, because, as it is, I don't think mongo_dart is really usable like that

giorgiofran commented 1 year ago

As I wrote in the issue you mentioned what you can do is to create a wapper. In case the db is not connected you have to close and open again. You can do it in two ways, checking if the method throws that error, or verifying the db.isConnected flag before calling the method and, in case it is not, reconnecting to the database.

This problem is something that I have in my long term plans. Unfortunately I have not so much time to do new developments, so I cannot really tell you when a solution will be available.

61soldiers commented 1 year ago

I guess I'll close this now, since its been in a way resolved Still though it feels like its something that should be handled on the package's side ?, comparing to other packages in other langs.