AttributeError: 'NoneType' object has no attribute 'call_soon'

Hi! I have the function which gets file from s3 bucket

async def get_object(self, file_path: str) -> Optional[bytes]:
        async with self.session.client(self.SERVICE_NAME) as s3:
            try:
                s3_object = await s3.get_object(Bucket=self.BUCKET_NAME, Key=file_path)
            except ClientError:
                return

            async with s3_object["Body"] as stream:
                file_data = await stream.read()

        return file_data

When my endpoint returned file data and everything is ok, then I get this error

Exception in callback SanicProtocol.abort()
handle: <TimerHandle when=23043869.405625403 SanicProtocol.abort() created at /usr/local/lib/python3.8/site-packages/sanic/server/protocols/base_protocol.py:93>
source_traceback: Object created at (most recent call last):
File “/usr/local/lib/python3.8/site-packages/sanic/server/runners.py”, line 199, in serve_single
serve(**server_settings)
File “/usr/local/lib/python3.8/site-packages/sanic/server/runners.py”, line 150, in serve
loop.run_forever()
File “/usr/local/lib/python3.8/asyncio/base_events.py”, line 570, in run_forever
self._run_once()
File “/usr/local/lib/python3.8/asyncio/base_events.py”, line 1851, in _run_once
handle._run()
File “/usr/local/lib/python3.8/asyncio/events.py”, line 81, in _run
self._context.run(self._callback, *self._args)
File “connection_task”, line 35, in connection_task
# request config
File “/usr/local/lib/python3.8/site-packages/sanic/server/protocols/base_protocol.py”, line 93, in close
self.loop.call_later(timeout, self.abort)
Traceback (most recent call last):
File “/usr/local/lib/python3.8/asyncio/events.py”, line 81, in _run
self._context.run(self._callback, *self._args)
File “/usr/local/lib/python3.8/site-packages/sanic/server/protocols/base_protocol.py”, line 101, in abort
self.transport.abort()
File “/usr/local/lib/python3.8/asyncio/selector_events.py”, line 672, in abort
self._force_close(None)
File “/usr/local/lib/python3.8/asyncio/selector_events.py”, line 723, in _force_close
self._loop.call_soon(self._call_connection_lost, exc)
AttributeError: ‘NoneType’ object has no attribute ‘call_soon’

What it can be? How can I avoid this error?

What version of Sanic?What does your endpoint look like?

My sanic version is 21.9.1

My endpoint:

async def get_thumbnail_file(_, entity_id: int) -> HTTPResponse:
    try:
        entity = await services.fetch_by_id(entity_id)
    except EntityNotFound as err:
        raise exceptions.NotFound(str(err))

    file_data = await get_entity_s3_file(file_path=entity.thumbnail_path)  # <- decoded by means of b64decode
    return json({"file": file_data})

The error doesn’t happen if I use uvloop package, however aerich doesn’t work with uvloop in my case (I use aiomysql package and tortoise orm).

I’ve created command file and use it for migrate and upgrade

import uvloop

from aerich.cli import cli


uvloop.install()


def main():
    cli()


if __name__ == "__main__":
    main()

I solved my problem, however the error happens if I use asyncio loop instead of uvloop