TL DR
It really was a bug in Motor 1.2.0, which was quickly fixed by A. Jesse Jiryu Davis and is available in version 1.2.1 or later of the driver.
Original question
I wrote a program to track changes to the MongoDB collection using its new Change Stream function in Python 3. Here is MCVE:
from asyncio import get_event_loop, CancelledError
from contextlib import suppress
from motor.motor_asyncio import AsyncIOMotorClient
async def watch(collection):
async with collection.watch([]) as stream:
async for change in stream:
print(change)
async def cleanup():
task.cancel()
with suppress(CancelledError):
await task
if __name__ == '__main__':
conn = AsyncIOMotorClient()
loop = get_event_loop()
task = loop.create_task(watch(conn.database.collection))
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
loop.run_until_complete(cleanup())
loop.shutdown_asyncgens()
loop.close()
When I kill a program using CTRL + C, there are three different exceptions.
^Cexception calling callback for <Future at 0x102efea58 state=finished raised InvalidStateError>
Traceback (most recent call last):
File "/Users/viotti/motor/lib/python3.6/site-packages/motor/core.py", line 1259, in _next
change = self.delegate.next()
File "/Users/viotti/motor/lib/python3.6/site-packages/pymongo/change_stream.py", line 79, in next
change = self._cursor.next()
File "/Users/viotti/motor/lib/python3.6/site-packages/pymongo/command_cursor.py", line 292, in next
raise StopIteration
StopIteration
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/concurrent/futures/thread.py", line 56, in run
result = self.fn(*self.args, **self.kwargs)
File "/Users/viotti/motor/lib/python3.6/site-packages/motor/core.py", line 1264, in _next
future.set_exception(StopAsyncIteration())
asyncio.base_futures.InvalidStateError: invalid state
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/concurrent/futures/_base.py", line 324, in _invoke_callbacks
callback(self)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/futures.py", line 414, in _call_set_state
dest_loop.call_soon_threadsafe(_set_state, destination, source)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py", line 620, in call_soon_threadsafe
self._check_closed()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py", line 357, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Is there a way to make this program shut silently?
I am testing with Python 3.6.4, Motor 1.2 and pymongo 3.6.0 on macOS Sierra.