Skip to content

gh-162217: Shield shared stdin close future to avoid InvalidStateError - #162284

Closed
mathisbusanabussei359-web wants to merge 2 commits into
python:mainfrom
mathisbusanabussei359-web:gh-162217-fix-stdin-wait-closed
Closed

mathisbusanabussei359-web wants to merge 2 commits into
python:mainfrom
mathisbusanabussei359-web:gh-162217-fix-stdin-wait-closed

Conversation

@mathisbusanabussei359-web

@mathisbusanabussei359-web mathisbusanabussei359-web commented Nov 19, 2026 •

Copy link
Copy Markdown

Fixes #162217

Cancelling a task that is awaiting proc.stdin.wait_closed() was
cancelling the shared _stdin_closed future. When the real close
happened later, pipe_connection_lost tried to set the result on an
already-cancelled future → InvalidStateError.

Solution: return tasks.shield(self._stdin_closed) so that cancelling
one waiter does not affect the shared future.

Added a regression test that creates two waiters, cancels one, then
closes stdin and checks that the remaining waiter succeeds and no
exception handler is triggered.

…reamProtocol

Cancelling one waiter on stdin.wait_closed() must not cancel the shared future used by pipe_connection_lost()
@python-cla-bot

python-cla-bot Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app

bedevere-app Bot commented Nov 19, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a test class for subprocesses - SubprocessMixin
I think you should move the test there. After that android / ios CI should pass

sys.executable, "-c", "import sys; sys.stdin.buffer.read()",
stdin=asyncio.subprocess.PIPE,
)
stdin = proc.stdin

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's gonna be good to write test in the same order of operations as repro from issue do

After await asyncio.create_subprocess_exec(...), stdin.close() should be called before starting the wait_closed() tasks

@deadlovelll

Copy link
Copy Markdown
Contributor

You need to create a news entry. Can be added with blurb add

@bedevere-app

bedevere-app Bot commented Nov 20, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

asyncio: cancelling a task awaiting subprocess stdin.wait_closed() cancels the shared close future, then pipe_connection_lost() raises InvalidStateError

3 participants