Uploaded image for project: 'Motor'
  1. Motor
  2. MOTOR-488

InvalidStateError in AgnosticLatentCommandCursor when future is cancelled

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 2.2
    • Affects Version/s: 2.1
    • Component/s: aiohttp, asyncio
    • Labels:
      None
    • Environment:
      python 3.7.2, motor 2.1.0, pymongo 3.10.1, ubuntu 18.04

      Hello,

      I am using the motor library in a simple aiohttp API backend and came across the following issue. When the aggregation that should return just one document was interrupted because web request was aborted, result of get_more (first i suppose) to the cancelled future. So an InvalidStateError exception is raised.

      The code part looks like this:

      def handler(request):
        pipeline = [{'$match': {...}},
                    {'$group': {'_id': 'k', 'v': {'$avg': '$smth'}}}]
        cursor = coll.aggregate(pipeline)
        data = [doc async for doc in cursor]
        return json_response(data)
      

      Here is the traceback:

      [2020-01-15 20:12:51,508] [ERROR] [asyncio] Exception in callback AgnosticLatentCommandCursor._on_started(<Future cancelled>, <Future finis...7f30909eb160>>)
      handle: <Handle AgnosticLatentCommandCursor._on_started(<Future cancelled>, <Future finis...7f30909eb160>>)>
      Traceback (most recent call last):
        File "/home/user/.pyenv/versions/3.7.2/lib/python3.7/asyncio/events.py", line 88, in _run
          self._context.run(self._callback, *self._args)
        File "/home/user/api/.venv/lib/python3.7/site-packages/motor/core.py", line 1542, in _on_started
          len(self.delegate._CommandCursor__data))
      asyncio.base_futures.InvalidStateError: invalid state
      

            Assignee:
            shane.harvey@mongodb.com Shane Harvey
            Reporter:
            gumgummy@ya.ru Denis Denis
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: