Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

asyncio.create_subprocess_exec does not respond properly to asyncio.CancelledError #103847

Open
Labels
@DarkArc

Description

@DarkArc

Bug report

asyncio programs that callproc = await asyncio.create_subprocess_exec but do not reach the call toawait proc.communicate are not properly cancelled.

This can be observed in the following script (it may take a few runs to observe):

importasyncioimportfunctoolsimportsignalcounter=0asyncdefrun_bash_sleep():globalcountercounter+=1local_counter=countertry:print(f"Started -{local_counter}")proc=awaitasyncio.create_subprocess_exec('bash','-c','sleep .001',stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE,start_new_session=True    )print(f"Waiting -{local_counter}")stdout,stderr=awaitproc.communicate()print(f"Done -{local_counter}!")exceptasyncio.CancelledError:print(f"Canceled -{local_counter}!")asyncdefrun_loop(loop):max_jobs=8active_tasks= []whileTrue:try:# Add jobs to the list of active jobswhilelen(active_tasks)<max_jobs:active_tasks.append(loop.create_task(run_bash_sleep()))# All tasks have finished, end the loopiflen(active_tasks)==0:break# Wait for a test to finish (or a 1 second timeout)done,pending=awaitasyncio.wait(active_tasks,timeout=1,return_when=asyncio.FIRST_COMPLETED      )print(f"Running jobs:{len(active_tasks)}")# Update the active jobsactive_tasks=list(pending)exceptasyncio.CancelledError:max_jobs=0defstop_asyncio_loop(signame,loop):fortaskinasyncio.all_tasks(loop):task.cancel()defmain():loop=asyncio.new_event_loop()asyncio.set_event_loop(loop)forsignamein {'SIGINT','SIGTERM'}:loop.add_signal_handler(getattr(signal,signame),functools.partial(stop_asyncio_loop,signame,loop)    )loop.run_until_complete(loop.create_task(run_loop(loop)))main()

When the signal handler cancels the tasks, any task that hasn't made it toawait proc.communicate() will never complete.

A subsequent SIGTERM to the script can then actually terminate the task; however, I'd expect the first call tocancel() to disrupt the coroutine.

Your environment

  • CPython versions tested on: 3.11.2, 3.11.3
  • Operating system and architecture: Fedora 38, x86_64

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp