stablekernel / postgresql-dart

Dart PostgreSQL driver: supports extended query format, binary protocol and statement reuse.
https://www.dartdocs.org/documentation/postgres/latest
BSD 3-Clause "New" or "Revised" License
129 stars 32 forks source link

Possible bug: wrong timeout time #161

Closed bubnenkoff closed 3 years ago

bubnenkoff commented 3 years ago

I can't find bug for a week. So possible it could be a bug in driver.

void main() async {
  db = Database();

  connection = PostgreSQLConnection('localhost', 5432, 'db', username: 'postgres', password: '123456', queryTimeoutInSeconds: 75);
  await connection.open();
   app.post('/sql-insert', (req, res) async {
   // see code below
   }

  }
  app.post('/sql-insert', (req, res) async {
    try {
        var insertResult = await db.sqlInsert(req.body).timeout(Duration(seconds: 140));
        switch (insertResult) {
          case dbEnums.success:
            return res.json({'status': 'success'});
          case dbEnums.insertFailed:
            return res.json({'status': 'insertFailed'});   
          default:
            return res.json({'status': 'unkownStatus'});
        }
    } catch (e) {

      print("sql-insert exception: $e");
    }

  }); 

method:

 Future < dynamic > sqlInsert(Map body) async {
     bool isDataWasInserted = false;

     try {
         await connection.transaction((ctx) async {
             for (var s in jsonDecode(body['sql'])) {
                 await ctx.query(s);
             }
             isDataWasInserted = true;
         }).timeout(Duration(seconds: 96));

     }
     on PostgreSQLException
     catch (e) {
         await connection.cancelTransaction();

         try {
             await connection.transaction((ctx) async {
                 for (var s in jsonDecode(body['sql-remove'])) {
                     await ctx.query(s);
                 }
             });

             try {
                 await connection.transaction((ctx) async {
                     for (var s in jsonDecode(body['sql'])) {
                         await ctx.query(s);
                     }
                     isDataWasInserted = true;
                 });

             }
             on PostgreSQLException
             catch (e) {
                 print("SECOND INSERT WAS FAILED ${e.message}");
                 await connection.cancelTransaction();
                 writeLog("SECOND INSERT WAS FAILED ", e.message);
             }

         }
         on PostgreSQLException
         catch (e) {
             print("Removing duplicates was FAILED: ${e.message}");
         }

     }

     if (isDataWasInserted) {
         return dbEnums.success;
     } else {
         writeLog("Can't insert data. Something wrong", "");
         return dbEnums.insertFailed;
     }

 }

Periodically I am getting exception:

sql-insert exception: TimeoutException after 0:00:30.000000: Future not completed
sql-insert exception: TimeoutException after 0:00:30.000000: Future not completed  

But I can't understand why the code and exception have different timeouts.

bubnenkoff commented 3 years ago

I need to do more tests.