GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Double linked list implementation for asyncio tasks · Issue #107803 · python/cpython

Double linked list implementation for asyncio tasks #107803

New issue
Copy link
New issue
Copy link
Closed
Closed
Double linked list implementation for asyncio tasks#107803
Copy link
Assignees
kumaraditya303
Labels
3.14bugs and security fixesbugs and security fixesperformancePerformance or resource usagePerformance or resource usagetopic-asyncio

Description

@kumaraditya303
kumaraditya303
opened on Aug 9, 2023
Issue body actions

Currently asyncio tasks are stored in a Weakset, this is inefficient and in some cases causes bugs because of thread safety (#80788). In terms of memory usage it requires maintaining a full set and their corresponding weakref callback to cleanup objects when deallocated and finalized by the gc. In applications where tasks are created at fast pace this becomes a bottle neck, to mitigate this now asyncio tasks will now be stored in a global double linked of tasks for cases where Task is a subclass of _asyncio.Task in other cases we still rely on the weakset. This reduces the work done by the gc speedups the execution and reduces memory usage. In some of my own benchmarks I have seen 15- 20% improvement and pyperformance benchmarks reflect roughly the same.

https://github.com/faster-cpython/benchmarking-public/blob/main/results/bm-20230805-3.13.0a0-1d32835/bm-20230805-linux-x86_64-kumaraditya303-linked_list-3.13.0a0-1d32835-vs-base.md

Updated: https://github.com/faster-cpython/benchmarking-public/tree/main/results/bm-20240622-3.14.0a0-4717aaa#vs-base

Linked PRs

  • GH-107803: double linked list implementation for asyncio tasks #107804
  • gh-107803: add whatsnew for asyncio double linked list implementation #120995
  • gh-107803: fix thread safety issue in double linked list implementation  #121007
  • GH-107803: use circular double linked list #126577
Reacted by Gregory P. Smith, Ruslan, siddish reddy, fruitoiz, Bakytzhan, Abraão Zaidan and J. Nick Koston
Reacted by Itamar Oren, Charlie Zhao, Jacob Bower, Kyriakos Psarakis, Gregory P. Smith, Maxim Martynov, Okeyo, ., benedikt-bartscher, fruitoiz and 4 more

Activity

  1. kumaraditya303
    added
    performancePerformance or resource usagePerformance or resource usage
    topic-asyncio
    3.13only security fixesonly security fixes
    on Aug 9, 2023
  2. kumaraditya303
    self-assigned this
    on Aug 9, 2023
  3. github-project-automation
    added this to asyncioon Aug 9, 2023
  4. github-project-automation
    moved this to Todo in asyncioon Aug 9, 2023
  5. willingc
    added a commit that references this issue on Jun 22, 2024

    GH-107803: double linked list implementation for asyncio tasks (GH-10…

    4717aaa
  6. kumaraditya303
    added
    3.14bugs and security fixesbugs and security fixes
    and removed
    3.13only security fixesonly security fixes
    on Jun 23, 2024
  7. colesbury commented on Jun 24, 2024

    @colesbury
    colesbury
    on Jun 24, 2024
    Contributor
    More actions

    I think there are some thread-safety issues with the linked list implementation:

    • It's not going to be thread-safe in the free-threaded build. We can probably add a lock or a critical section.
    • Even with the GIL, the linked list of tasks may be modified while it's being looped over because the GIL can be released in add_one_task.
    Reacted by Carol Willing
  8. willingc commented on Jun 24, 2024

    @willingc
    willingc
    on Jun 24, 2024
    Contributor
    More actions

    Thanks @colesbury for flagging this.

  9. kumaraditya303 commented on Jun 25, 2024

    @kumaraditya303
    kumaraditya303
    on Jun 25, 2024
    ContributorAuthor
    More actions

    I am closing this, will track free threaded issues in #120974

    Reacted by Carol Willing
  10. kumaraditya303
    closed this as completedon Jun 25, 2024
  11. github-project-automation
    moved this from Todo to Done in asyncioon Jun 25, 2024
  12. colesbury commented on Jun 25, 2024

    @colesbury
    colesbury
    on Jun 25, 2024
    Contributor
    More actions

    @kumaraditya303, can you fix the thread-safety/reentrancy issue that affects the default build (with the GIL)? head can be deallocated while it's being used. It needs to be protected by a Py_INCREF(). This is not specific to the free-threading build.

    cpython/Modules/_asynciomodule.c

    Lines 3711 to 3721 in 2d3187b

    TaskObj *tail = &state->asyncio_tasks.tail;
    while (head != tail)
    {
    if (add_one_task(state, tasks, (PyObject *)head, loop) < 0) {
    Py_DECREF(tasks);
    Py_DECREF(loop);
    return NULL;
    }
    head = head->next;
    assert(head != NULL);
    }

    Reacted by Carol Willing
  13. kumaraditya303
    added 2 commits that reference this issue on Jun 26, 2024

    gh-107803: fix thread safety issue in double linked list implementati…

    8223544

    gh-107803: add whatsnew for asyncio double linked list implementation (…

    58a3580
  14. mrahtz
    added 3 commits that reference this issue on Jun 30, 2024

    pythonGH-107803: double linked list implementation for asyncio tasks (p…

    797d82f

    pythongh-107803: fix thread safety issue in double linked list implem…

    57e50d9

    pythongh-107803: add whatsnew for asyncio double linked list implemen…

    29e69e4
  15. facebook-github-bot
    added a commit that references this issue on Jul 3, 2024

    Backport gh-107803: Double linked list implementation for asyncio tasks

    6f4de81
  16. estyxx
    added 3 commits that reference this issue on Jul 17, 2024

    pythonGH-107803: double linked list implementation for asyncio tasks (p…

    c5a0305

    pythongh-107803: fix thread safety issue in double linked list implem…

    6970af9

    pythongh-107803: add whatsnew for asyncio double linked list implemen…

    b4ff32a
  17. kumaraditya303
    added a commit that references this issue on Nov 8, 2024

    GH-107803: use circular double linked list for tasks in `_asyncio` (#…

    a3e8e7b
  18. picnixz
    added a commit that references this issue on Dec 8, 2024

    pythonGH-107803: use circular double linked list for tasks in `_async…

    9eaa951
  19. ebonnal
    added a commit that references this issue on Jan 12, 2025

    pythonGH-107803: use circular double linked list for tasks in `_async…

    1cd71e3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @kumaraditya303
    kumaraditya303

Labels

3.14bugs and security fixesbugs and security fixesperformancePerformance or resource usagePerformance or resource usagetopic-asyncio

Projects

  • asyncio
    Status
    Done
    Show more project fields

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.