Skip to content

prompt with async_ doesn't clean up if cancelled #787

@bmerry

Description

@bmerry

I'm having some trouble with porting a REPL app (ntsh to prompt-toolkit 2.0. Mostly the new API is making things much simpler (thanks!), and instead of using an Application I'm now trying to use just PromptSession plus asyncio. Sometimes I want to terminate the app in response to a network event rather than user input. However, it seems that if the prompt future is cancelled, it doesn't restore normal terminal settings, and after exit characters aren't echoed.

Here's a minimal example case (tested with Ubuntu 18.04, Python 3.6, prompt-toolkit 2.0.7; also requires async_timeout). Just run it and wait 5 seconds for it to exit on its own, then observe that terminal echo is off.

#!/usr/bin/env python

import asyncio

import async_timeout
from prompt_toolkit.eventloop import use_asyncio_event_loop
from prompt_toolkit.shortcuts import prompt

async def main():
    try:
        with async_timeout.timeout(5):
            text = await prompt('> ', async_=True)
        print('You entered:', text)
    except asyncio.TimeoutError:
        print('Timed out')

use_asyncio_event_loop()
asyncio.get_event_loop().run_until_complete(main())

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