Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-2778

Implicit sessions are sometimes leaked

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Critical - P2 Critical - P2
    • 3.6.3
    • Affects Version/s: 3.6.0, 3.6.1, 3.6.2
    • Component/s: Query Operations
    • None

      ISSUE DESCRIPTION
      The 3.6 driver added support for implicit sessions as well as a session pool.

      This issue tracks several bugs with implicit session management that cause sessions to be leaked on some query operations.

      IMPACT
      Applications that are exposed to this leak may first notice it by observing SERVER-33158. Messages like the ones below will appear in the server logs:

      2018-02-06T13:18:44.855+0000 E -        [thread70181] Assertion: 10334:BSONObj size: 19492699 (0x1296F5B) is invalid. Size must be between 0 and 16793600(16MB) First element: 0: { q: { _id: { id: UUID("5447911f-6c78-4238-aeae-c249bec5df73"), uid: BinData(0, F5748B71272004FF160A969FEE42869CBD48FD155B00AFD0EF604F431FFB2F4F) } }, u: { $currentDate: { lastUse: true }, $setOnInsert: { user: { name: "vocab@admin" } } }, upsert: true } src/mongo/bson/bsonobj.cpp 101
      2018-02-06T13:18:44.925+0000 I CONTROL  [thread70181] Failed to refresh session cache: Location10334: BSONObj size: 19492699 (0x1296F5B) is invalid. Size must be between 0 and 16793600(16MB) First element: 0: { q: { _id: { id: UUID("5447911f-6c78-4238-aeae-c249bec5df73"), uid: BinData(0, F5748B71272004FF160A969FEE42869CBD48FD155B00AFD0EF604F431FFB2F4F) } }, u: { $currentDate: { lastUse: true }, $setOnInsert: { user: { name: "vocab@admin" } } }, upsert: true }
      

      EXAMPLES
      A couple of examples which demonstrate the leaks can be found below.

      A MongoCursor that is not closed will leaks an implicit session
              MongoCursor<Document> iterator = collection.find().iterator();
              while (iterator.hasNext()) {  // cursor is not closed
                  iterator.next();
              }
      

      Code which uses a cursor and properly closes it are not affected by the leak:

              try (MongoCursor<Document> iterator = collection.find().iterator()) {
                  while (iterator.hasNext()) {
                      iterator.next();
                  }
              }
              // or
              collection.find().forEach((Block<Document>) document -> {
                  // do something ...
              });
              // or
              ArrayList<Document> documents = collection.find().into(new ArrayList<>());
      
      Any use of the first() method (or findOne() in the DBCollection API):
              collection.find().first();    // session is not properly released to the pool
      

      The only workaround for this manifestation of the leak is to replace with explicit use of a MongoCursor, as above.

      Original Description

      We recently upgraded our production replica set from MongoDB 3.4.11 to 3.6.2. The following error occurs every couple minutes and is output in the Mongo log on the primary. We have also found system.sessions is empty using $listSessions on the config db and an authorized user. On the other hand $listLocalSessions returns many in-memory sessions.

      2018-02-06T13:18:44.855+0000 E -        [thread70181] Assertion: 10334:BSONObj size: 19492699 (0x1296F5B) is invalid. Size must be between 0 and 16793600(16MB) First element: 0: { q: { _id: { id: UUID("5447911f-6c78-4238-aeae-c249bec5df73"), uid: BinData(0, F5748B71272004FF160A969FEE42869CBD48FD155B00AFD0EF604F431FFB2F4F) } }, u: { $currentDate: { lastUse: true }, $setOnInsert: { user: { name: "vocab@admin" } } }, upsert: true } src/mongo/bson/bsonobj.cpp 101
      2018-02-06T13:18:44.925+0000 I CONTROL  [thread70181] Failed to refresh session cache: Location10334: BSONObj size: 19492699 (0x1296F5B) is invalid. Size must be between 0 and 16793600(16MB) First element: 0: { q: { _id: { id: UUID("5447911f-6c78-4238-aeae-c249bec5df73"), uid: BinData(0, F5748B71272004FF160A969FEE42869CBD48FD155B00AFD0EF604F431FFB2F4F) } }, u: { $currentDate: { lastUse: true }, $setOnInsert: { user: { name: "vocab@admin" } } }, upsert: true }
      

        1. cpu.png
          cpu.png
          13 kB
        2. serverStatus.png
          serverStatus.png
          76 kB

            Assignee:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Reporter:
            smachnowski@vocabulary.com Stephen Machnowski
            Votes:
            0 Vote for this issue
            Watchers:
            16 Start watching this issue

              Created:
              Updated:
              Resolved: