• Type: Bug
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: 8.0.9
    • Component/s: None
    • Catalog and Routing
    • ALL
    • πŸŸ₯ DDL
    • None
    • None
    • None
    • None
    • None
    • None

      I found a logical error in ShardingCatalogManager::createDatabase() in MongoDB 8.0.9:

      https://github.com/mongodb/mongo/blob/r8.0.9/src/mongo/db/s/config/sharding_catalog_manager_database_operations.cpp#L207-L242

      In this method, the retries variable is initialized to 10 inside the while (true) loop:

      {{while (true) {
      // ...
      auto retries = 10;
      try

      { return _commitCreateDatabase(opCtx, dbName, candidatePrimaryShardId); } catch (const ExceptionFor<ErrorCodes::ShardNotFound>& ex) {
      // ...
      if (optResolvedPrimaryShard) { throw; } else {
      retries--;
      if (retries > 0) { continue; } else {
      LOGV2_WARNING(8917901,
      "Exhausted retries trying to commit create database",
      "dbName"_attr = dbName);
      throw;
      }
      }
      }
      }}}

      Since retries is declared inside the loop, it is reinitialized to 10 on every iteration.

      When _commitCreateDatabase() throws ErrorCodes::ShardNotFound and no primary shard was explicitly specified, retries is decremented from 10 to 9. The condition retries > 0 is therefore always true, and the continue statement starts another iteration with retries reset to 10.

      As a result, the retry counter never reaches zero, and the branch responsible for reporting exhausted retry attempts is unreachable.

      If ShardNotFound continues to occur, the function may keep retrying indefinitely instead of stopping after the intended number of attempts. This may lead to excessive repeated operations and failure to terminate the database creation attempt as intended.

      A possible fix is to move the initialization of retries outside the loop:

      {{auto retries = 10;
      while (true) {
      // ...
      try { return _commitCreateDatabase(opCtx, dbName, candidatePrimaryShardId); }

      catch (const ExceptionFor<ErrorCodes::ShardNotFound>& ex) {
      // ...
      if (optResolvedPrimaryShard)

      { throw; }

      else {
      retries--;
      if (retries > 0)

      { continue; }

      else {
      LOGV2_WARNING(8917901,
      "Exhausted retries trying to commit create database",
      "dbName"_attr = dbName);
      throw;
      }
      }
      }
      }}}

      This ensures that the retry counter is preserved across iterations and the retry limit is enforced.

      Found by Linux Verification Center (linuxtesting.org) with SVACE.

      Reporter: Grebennikov Aleksandr (grebennikov@bmstu.ru)

            Assignee:
            Unassigned
            Reporter:
            АлСксандр Π“Ρ€Π΅Π±Π΅Π½Π½ΠΈΠΊΠΎΠ² (EXT)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: