Skip to content

asyncio should make it easy to enable cooperative SIGINT handling #75568

Closed as not planned

Description

@ncoghlan
BPO 31387
Nosy @gpshead, @ncoghlan, @vstinner, @giampaolo, @njsmith, @1st1, @ryanhiebert, @aeros
Dependencies
  • bpo-29988: with statements are not ensuring that exit is called if enter succeeds
  • bpo-31388: Provide a way to defer SIGINT handling in the current thread
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2017-09-07.19:37:30.590>
    labels = ['3.7', 'type-bug', 'library']
    title = 'asyncio should make it easy to enable cooperative SIGINT handling'
    updated_at = <Date 2019-10-14.23:11:16.713>
    user = 'https://github.com/ncoghlan'

    bugs.python.org fields:

    activity = <Date 2019-10-14.23:11:16.713>
    actor = 'aeros'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2017-09-07.19:37:30.590>
    creator = 'ncoghlan'
    dependencies = ['29988', '31388']
    files = []
    hgrepos = []
    issue_num = 31387
    keywords = []
    message_count = 4.0
    messages = ['301619', '301631', '301661', '301669']
    nosy_count = 8.0
    nosy_names = ['gregory.p.smith', 'ncoghlan', 'vstinner', 'giampaolo.rodola', 'njs', 'yselivanov', 'ryanhiebert', 'aeros']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = 'needs patch'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue31387'
    versions = ['Python 3.7']

    Activity

    1. ncoghlan commented on Sep 7, 2017

      @ncoghlan
      ContributorAuthor

      bpo-29988 covers the fact that with the default SIGINT handler installed, a poorly timed Ctrl-C can lead to context managers failing to even start running their __(a)exit__ methods, let alone complete them.

      For the asynchronous case, the problem is even worse, as the *event loop* may be interrupted at arbitrary points if the default SIGINT handler is left in place.

      To handle this robustly, it's desirable to make it easy to switch event-driven programs over to cooperative Ctrl-C handling by installing an asyncio SIGINT handler while the event loop is running, rather than leaving the default SIGINT handler in place.

      (Note: while installing a cooperative SIGINT handler will enable more robust event-loop managed resource cleanup, it will have the downside that Ctrl-C won't be able to interrupt a coroutine that has incorrectly blocked the main thread)

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Sep 7, 2017
    3. njsmith commented on Sep 7, 2017

      @njsmith
      Contributor

      Some prior discussion on the old asyncio tracker:
      python/asyncio#305 (comment)
      python/asyncio#341

    4. ncoghlan commented on Sep 8, 2017

      @ncoghlan
      ContributorAuthor

      bpo-31388 is also potentially relevant here, as registering a signal handler for SIGINT isn't sufficient to cover all potential cases were Py_AddPendingCall gets called. In particular, the tests for bpo-29988 use Py_AddPendingCall to emulate Ctrl-C, rather than relying on SIGINT actually being raised.

    5. ncoghlan commented on Sep 8, 2017

      @ncoghlan
      ContributorAuthor

      As per Nathaniel's comments on bpo-29988 and 31388, doing this robustly relies on:

      1. the event loop being able to reliably guard itself and __aexit__ method implementations against interrupts (bpo-31388)
      2. "async with" statements ensuring that if the frame resumes after calling __aenter__, then it will also call __aexit__ (as was done for synchronous with statements in bpo-29988)
    6. transferred this issue fromon Apr 10, 2022
    7. willingc commented on Jun 21, 2024

      @willingc
      Contributor

      @ncoghlan I'm going to close this issue as stale, needing reframing for current state of asyncio, and related to an EOL version. If there is specific ongoing work that we feel would be a beneficial addition for asyncio, let's open a new issue based on the current asyncio implementation. Thanks (I'm going through the asyncio issues to make sure each one is actionable without too much cognitive load.). ☀️

    8. moved this from Todo to Done in asyncioon Jun 21, 2024
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      • Status
        Done

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions