Skip to content

gh-158414: Fix hang when writing incomplete UTF-8 sequence to Windows console - #158435

Merged
cmaloney merged 2 commits into
python:mainfrom
aisk:winconsoleio-incomplete-utf8
Nov 17, 2026
Merged

cmaloney merged 2 commits into
python:mainfrom
aisk:winconsoleio-incomplete-utf8

Conversation

@aisk

@aisk aisk commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

After gh-124008, _find_last_utf8_boundary() may return 0 when the data is only an incomplete UTF-8 sequence, which causes flush() to hang.

In this case, use the original len instead, so the data is written as "�". I don't think there is a better way to handle it correctly without adding too much complexity.

@cmaloney cmaloney left a comment

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.

This is likely going to cause gh-110913 in some cases. I think in explicit flush always emptying the buffer fully is good; partial emoji printing is okay in that case.

I worry with this being in the write loop regardless of "is this a flush" is going to result in more broken than good. Emptying most but not all here I think would be nice (Although that may have implications for buffering layers above this).

@cmaloney cmaloney added 3.14 bugs and security fixes 3.15 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes and removed 3.14 bugs and security fixes 3.15 bugs and security fixes labels Nov 14, 2026
@aisk

aisk commented Nov 17, 2026

Copy link
Copy Markdown
Member Author

Thank you for the review!

Other paths not in flush() (for example, write() when the buffer is full) also call _bufferedwriter_flush_unlocked(), so adding a flag can’t t simply avoid the hang.

Also, the fallback in this change only triggers in cases that currently hang, so it won’t make the normal path worse. I think it’s better not to make it too complex.

@cmaloney
cmaloney merged commit aedb2c1 into python:main Nov 17, 2026
57 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @aisk for the PR, and @cmaloney for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Nov 17, 2026

Copy link
Copy Markdown

GH-162168 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Nov 17, 2026
@bedevere-app

bedevere-app Bot commented Nov 17, 2026

Copy link
Copy Markdown

GH-162169 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Nov 17, 2026
cmaloney added a commit that referenced this pull request Nov 17, 2026
…Windows console (GH-158435) (#162168)

gh-158414: Fix hang when writing incomplete UTF-8 sequence to Windows console (GH-158435)
(cherry picked from commit aedb2c1)

Co-authored-by: An Long <aisk@users.noreply.github.com>
Co-authored-by: Cody Maloney <cmaloney@theoreticalchaos.com>
cmaloney added a commit that referenced this pull request Nov 17, 2026
…Windows console (GH-158435) (#162169)

gh-158414: Fix hang when writing incomplete UTF-8 sequence to Windows console (GH-158435)
(cherry picked from commit aedb2c1)

Co-authored-by: An Long <aisk@users.noreply.github.com>
Co-authored-by: Cody Maloney <cmaloney@theoreticalchaos.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants