python / python/cpython

`python -m asyncio ps` fails if any task name is longer than 255 characters

Open
#157,788 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-asyncio type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

One task name longer than 255 characters makes python -m asyncio ps fail for the whole process. The length limit is treated as a validity check instead of a cap.

Repro:

  import asyncio
  import os
  from _remote_debugging import RemoteUnwinder

  async def main():
      asyncio.create_task(asyncio.sleep(10), name='x' * 256)
      await asyncio.sleep(0)
      print(RemoteUnwinder(os.getpid()).get_all_awaited_by())

  asyncio.run(main())

Actually:

  Traceback (most recent call last):
    File "/Users/timofeiivankov/cpython/repro.py", line 10, in <module>
      asyncio.run(main())
      ~~~~~~~~~~~^^^^^^^^
    File "/Users/timofeiivankov/cpython/Lib/asyncio/runners.py", line 205, in run
      return runner.run(main)
             ~~~~~~~~~~^^^^^^
    File "/Users/timofeiivankov/cpython/Lib/asyncio/runners.py", line 128, in run
      return self._loop.run_until_complete(task)
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
    File "/Users/timofeiivankov/cpython/Lib/asyncio/base_events.py", line 725, in run_until_complete
      return future.result()
             ~~~~~~~~~~~~~^^
    File "/Users/timofeiivankov/cpython/repro.py", line 8, in main
      print(RemoteUnwinder(os.getpid()).get_all_awaited_by())
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  RuntimeError: Invalid string length (256) at 0x101493800

Expected: the call returns and the long name comes back truncated:

[AwaitedInfo(thread_id=21170325, awaited_by=[
  TaskInfo(task_id=4312485712, task_name='Task-1', ...),
  TaskInfo(task_id=4312485232, task_name='xxx…' (truncated to 255), ...)])]

Proposed fix: Truncate to the limit instead of raising in object_reading.c

I have a fix ready for that

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs
  • gh-157790

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

Start in object_reading.c and reproduce the failure with the provided asyncio task-name example, using python -m asyncio ps or the shown RemoteUnwinder call. Done means the call succeeds when a task name exceeds 255 characters and the returned name is truncated to 255 characters; check linked PR gh-157790 for work already in progress.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.