Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-929

cursor.alive is always True after find() even if the connection dies / next() fails

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:
      MongoDB 2.4.6 ; pymongo==3.0.2-1-dce3f36

      I observed some code infinite loop when using a tailable cursor on a capped collection.

      The issue seems to be that after a find(...), cursor.alive is True even if the MongoDB isn't there, and continues being true after cursor.next() fails.

      Also coll.find() continues to work even with no MongoDB.

      This occurred in part of a data pipeline where we had a thread polling the tailable cursor and forwarding the data on to downstream consumers.

      Repro looks like this (MongoTestServer is a fixtured MongoDB):

      import gzip
      import bson
      from bson import json_util
      from bson import Binary
      import pymongo
      from pymongo import MongoClient
      import time
      import timeit
      
      from ahl.testing.mongo_server import MongoTestServer
      
      server = MongoTestServer()
      server.start()
      client = server.api
      
      coll = client.some_db.create_collection('some_coll', options={'capped': True})
      coll.insert([{}, {}])
      
      # while True:
      cursor = coll.find()
      assert cursor.next()
      while cursor.alive:
          print "cursor.alive"
          server.kill()  # MongoDB will die at this point
          try:
              do_something_with_data(cursor.next())
          except StopIteration:
              time.sleep(1)
          except Exception:
              # Catch any client exceptions...
              pass
      

      This prints "cursor.alive" forever AFAICS.

      Also this works without any MongoDB:

      server.kill()
      while True:
          cursor = coll.find()
          assert cursor.alive
      

            Assignee:
            bernie@mongodb.com Bernie Hackett
            Reporter:
            jblackburn James Blackburn
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: