The usual support costs will apply to additional support questions and issues that do not qualify for this specific hotfix. Note If additional issues occur or if any troubleshooting is required, you might have to create a separate service request. If this section does not appear, contact Microsoft Customer Service and Support to obtain the hotfix.
If the hotfix is available for download, there is a "Hotfix download available" section at the top of this Knowledge Base article.
Therefore, if you are not severely affected by this problem, we recommend that you wait for the next software update that contains this hotfix. This hotfix might receive additional testing. Apply this hotfix only to systems that are experiencing the problem described in this article. However, this hotfix is intended to correct only the problem that is described in this article. Hotfix informationĪ supported hotfix is available from Microsoft. This hotfix is also available at Microsoft Update Catalog.
+ Python 3.8, Python 3.9, Python 3.10, - Python 3.6, Python 3.This issue occurs because Windows in Windows 64-bit (WOW64) processes cannot duplicate non-pseudo handles from Process Environment Block (PEB32) processes. I'd rather have no change in earlier versions than a regression. I'm pretty sure this won't have any (negative) visible impact, but just in case, let's let a 3.10 release go out with it before merging the backports. Looks like backports will have to be manual, and I'm not going to get to them today myself, but if someone else wants to do it then feel free.
New changeset 5e437fb872279960992c9a07f1a4c051b4948c53 by Segev Finer in branch 'master':īpo-30555: Fix WindowsConsoleIO fails in the presence of fd redirection ( GH-1927) It also allows duping a new open of CON, CONIN$, or CONOUT$, or even a new screen buffer from CreateConsoleScreenBuffer(), to the existing fileno(), and the _WindowsConsoleIO() instance will march along none the wiser. By using _get_osfhandle() instead of caching the handle value, it guarantees that access to the original console file can be saved and restored via `fd_save = dup(fd)` and `dup2(fd_save, fd)`. If that was fixed, I think many of these problems (at least at the interactive prompt) would simply go away. Probably the main issue here is readline not properly handling sys.std*. I bet there's an optimization here, though it's not obvious. This might be the best approach, ultimately.
But so does _WindowsConsoleIO in general since it will try to continue writing to the console despite the redirection, meaning that Python code doing redirection has to handle sys.std* anyhow. > We can also switch to calling _get_osfhandle always instead of caching the handle, it will break when the fd is redirected to a non-console. Solving this in CPython will remove the need for hacks like the PR I referenced. And their yet might be other ways to solve this. Though I'm not sure about the performance of constantly calling _get_osfhandle. We can also switch to calling _get_osfhandle always instead of caching the handle, it will break when the fd is redirected to a non-console. One way I can think working around this is that Python will DuplicateHandle the console handles so that even if dup2 closes the original ones it will keep on working. It's quite possible that this doesn't happen in older CRT versions either since colorama doesn't seem to break on Python 2.7 for example. Those file descriptors are an emulation implemented by the Universal CRT and you can see their implementation in your copy of the Windows SDK. Though that code is likely going to have to deal with this by itself. This issue also impacts other code that uses console handles itself like colorama. And see for an WIP attempt to workaround this. This manifests itself for example in Pytest which does fd redirection to capture test output. OSError: The parameter is incorrect # "OSError: The handle is invalid" after > fd = os.open('stdout.txt', os.O_CREAT | os.O_WRONLY) The problem is that this is also going to close the handle that Python itself is going to use to read/write to the console.
The problem is that some Python code wants to do file descriptor level redirection by overwriting the standard file descriptors using dup2.
_WindowsConsoleIO works by taking the handle out of the file descriptors of the stdio handles (get_osfhandle) and reading/writing to it directly using ReadConsoleW/WriteConsoleW. Segev Finer, eryksun, paul.moore, steve.dower, tim.golden, zach.wareĬreated on 16:56 by Segev Finer, last changed 22:04 by steve.dower. _io._WindowsConsoleIO breaks in the face of fd redirection