Skip to content

Switch gen.with_timeout to asyncio.wait_for #3367

Description

@jrbourbeau

There are several instances where gen.with_timeout is used in the codebase. As part of our switch from existing tornado code to asyncio, we should move to using asyncio.wait_for.

However, one important distinction between these functions is that gen.with_timeout does not cancel a task when it's timeout expires, while asyncio.wait_fordoes cancel the task. So we need to be careful and determine on a case by case basis when task cancellation on timeout should happen. From @jcrist's comment (#3365 (comment)):

replacing it isn't always straightforward. Each case needs to be examined to determine what the intent was and if the lack of cancellation was accidental or necessary.

To avoid task cancellation when using asyncio.wait_for, the task should first be wrapped in asyncio.shield

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions