tortoise / tortoise/tortoise-orm

Always encountering the error `'NoneType' object has no attribute 'write'`.

Open
#1,453 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

I used apscheduler to start a scheduled task that runs every 5 seconds. However, the statement related to updating the database always throws an error after running for a while.

await Session.filter(id__in=[...])update(state=StateEnum.communication)

Traceback (most recent call last):

  File "D:\Dev\MyProject\WorkProjects\src\faster\routers\control\tasks.py", line 138, in __rcst_update_to_communication
    await asyncio.shield(
          │       └ <function shield at 0x000001F8C3F59EE0>
          └ <module 'asyncio' from 'D:\\Dev\\Miniconda3\\envs\\lib\\asyncio\\__init__.py'>
  File "D:\Dev\Miniconda3\envs\lib\asyncio\tasks.py", line 688, in _wrap_awaitable
    return (yield from awaitable.__await__())
                       │         └ <function UpdateQuery.__await__ at 0x000001F8C7CF8A60>
                       └ <tortoise.queryset.UpdateQuery object at 0x000001F8C93FF280>
  File "D:\Dev\Miniconda3\envs\lib\site-packages\tortoise\queryset.py", line 1109, in _execute
    return (await self._db.execute_query(str(self.query), self.values))[0]
                  │    │                     │    │       │    └ <member 'values' of 'UpdateQuery' objects>
                  │    │                     │    │       └ <tortoise.queryset.UpdateQuery object at 0x000001F8C93FF280>
                  │    │                     │    └ <member 'query' of 'AwaitableQuery' objects>
                  │    │                     └ <tortoise.queryset.UpdateQuery object at 0x000001F8C93FF280>
                  │    └ <member '_db' of 'AwaitableQuery' objects>
                  └ <tortoise.queryset.UpdateQuery object at 0x000001F8C93FF280>
  File "D:\Dev\Miniconda3\env\lib\site-packages\tortoise\backends\mysql\client.py", line 44, in translate_exceptions_
    return await func(self, *args)
                 │    │      └ ("UPDATE `control_session` SET `state`=%s WHERE `id` IN ('00-0a-35-00-01-59')", ['COMMUNICATION'])
                 │    └ <tortoise.backends.mysql.client.TransactionWrapper object at 0x000001F8C8C06580>
                 └ <function MySQLClient.execute_query at 0x000001F8C915C8B0>
  File "D:\Dev\Miniconda3\envs\lib\site-packages\tortoise\backends\mysql\client.py", line 199, in execute_query
    await cursor.execute(query, values)
          │      │       │      └ ['COMMUNICATION']
          │      │       └ "UPDATE `control_rcst_session` SET `state`=%s WHERE `rcst_hid` IN ('00-0a-35-00-01-59')"
          │      └ <method 'execute' of 'asyncmy.cursors.Cursor' objects>
          └ <asyncmy.cursors.Cursor object at 0x000001F8C9319AC0>
  File "asyncmy\cursors.pyx", line 179, in execute
    result = await self._query(query)
  File "asyncmy\cursors.pyx", line 364, in _query
    await conn.query(q)
  File "asyncmy\connection.pyx", line 449, in query
    await self._execute_command(COM_QUERY, sql)
                                └ 3
  File "asyncmy\connection.pyx", line 685, in _execute_command
    self._write_bytes(prelude + sql[: packet_size - 1])
  File "asyncmy\connection.pyx", line 624, in asyncmy.connection.Connection._write_bytes
    self._writer.write(data)
AttributeError: 'NoneType' object has no attribute 'write'

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the scheduled update against MySQL and follow the traceback through tortoise/backends/mysql/client.py to asyncmy/connection.py, especially Connection._write_bytes. Start by checking why the connection’s writer is None after the task has run for a while. Done means the repeated database update no longer raises the reported AttributeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, python
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.