Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-21095

Using tailable cursor on capped collection memory leak

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.4.7, 3.0.2
    • Component/s: Querying
    • Labels:
      None
    • ALL
    • Hide

      Create capped collection.
      Insert one record - save id,
      Create tailable cursor with query id > saved id.
      tyr to get next record in a loop.
      Every loop ther is an amount of memory leaked in the server.

      Show
      Create capped collection. Insert one record - save id, Create tailable cursor with query id > saved id. tyr to get next record in a loop. Every loop ther is an amount of memory leaked in the server.

      Using a tailable cursor on a capped collection leads to memory leak in the server if the first cursor MoveNext does not get a record.

      BsonValue lastId = collection.FindAll().SetSortOrder(SortBy.Descending("_id")).SetLimit(1).First()["_id"];
      
      cursor = collection.Find(Query.GT("_id", lastId))
                    .SetFlags(QueryFlags.AwaitData | QueryFlags.TailableCursor | QueryFlags.NoCursorTimeout);
      
      using (var enumerator = new MongoCursorEnumerator<BsonDocument>(cursor))
      {
      	while (triggerThread.IsAlive)
      	{
      		if (enumerator.MoveNext())
      		{
      			var document = enumerator.Current;
      			if (document != null)
      			{
      				HandleNextData(document);
      			}
      		}
      		else
      		{
      			if (enumerator.IsDead)
      			{
      				break;
      			}
      			if (!enumerator.IsServerAwaitCapable)
      			{
      				Thread.Sleep(TimeSpan.FromMilliseconds(100));
      			}
      		}
      	}
      }
      

      If the cursor query is changed to "GTE"

      cursor = collection.Find(Query.GTE("_id", lastId))
      

      so as the first result the last record is returned all works fine.

        1. IncreasingOpenCursors.png
          IncreasingOpenCursors.png
          81 kB
        2. metrics.2016-04-04T07-28-52Z-00000
          39 kB
        3. mongod-21095.log
          31.39 MB
        4. screenshot.png
          screenshot.png
          19 kB
        5. TailableCursorLegacy.zip
          967 kB

            Assignee:
            kelsey.schubert@mongodb.com Kelsey Schubert
            Reporter:
            fpf_baden Frank Pfattheicher
            Votes:
            0 Vote for this issue
            Watchers:
            11 Start watching this issue

              Created:
              Updated:
              Resolved: