Skip to content

Failure during the print of select.select output with unblocking sys.stdin #126468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
yurivict opened this issue Nov 6, 2024 · 6 comments
Open
Labels
extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error

Comments

@yurivict
Copy link

yurivict commented Nov 6, 2024

Bug report

Bug description:

This code terminates with error messages after several loop iterations:

import sys
import os
import select

os.set_blocking(sys.stdin.fileno(), False)

while True:
    print('>')
    ready = select.select([sys.stdin, ], [], [], 0.0)[0]
    print(f'< ready={ready}')
$ python3.11 stdin-nonblocking-bug-report.py
<... skip many iterations ...>
< ready=[]
>
< ready=[]
>
< ready=[]
Traceback (most recent call last):
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
Exception ignored in sys.unraisablehook: <built-in function unraisablehook>

$ echo $?
120

The failure occurs in print(f'< ready={ready}').

Python-3.11
FreeBSD 14.1

CPython versions tested on:

3.11

Operating systems tested on:

Other

@yurivict yurivict added the type-bug An unexpected behavior, bug, or error label Nov 6, 2024
@picnixz picnixz added the extension-modules C modules in the Modules dir label Nov 6, 2024
@Zheaoli
Copy link
Contributor

Zheaoli commented Nov 6, 2024

Can not reproduce it on main and 3.13 branch. Would you mind providing more detail?

@yurivict
Copy link
Author

yurivict commented Nov 6, 2024

@Zheaoli
Did you run on FreeBSD ?
What other details can I provide?

@Zheaoli
Copy link
Contributor

Zheaoli commented Nov 6, 2024

Did you run on FreeBSD ?

Yes,

root@freebase-test:~ # fastfetch
```                        `        root@freebase-test
  ` `.....---.......--.```   -/     ------------------
  +o   .--`         /y:`      +.    Host: Standard PC (i440FX + PIIX, 1996) (pc-i440fx-9.0)
   yo`:.            :o      `+-     Kernel: FreeBSD 14.1-RELEASE
    y/               -/`   -o/      Uptime: 19 mins
   .-                  ::/sy+:.     Shell: sh
   /                     `--  /     Terminal: /dev/pts/0
  `:                          :`    CPU: QEMU Virtual version 2.5+ (8) @ 2.50 GHz
  `:                          :`    GPU: Unknown Device 1111 (VGA compatible)
   /                          /     Memory: 4.14 GiB / 47.96 GiB (9%)
   .-                        -.     Swap: 0 B / 1.00 GiB (0%)
    --                      -.      Disk (/): 4.31 GiB / 101.24 GiB (4%) - zfs
     `:`                  `:`       Local IP (eth0): 192.168.12.41/16
       .--             `--.         Locale: en_US.UTF-8
          .---.....----.

@yurivict
Copy link
Author

yurivict commented Nov 6, 2024

I encountered this problem with the python311 package, and you said that you tried 3.13

Can you try with python311?
Maybe it is already fixed in 3.13

@ericvsmith
Copy link
Member

3.11 is no longer receiving bug fixes. Can you try 3.12 or 3.13? See https://devguide.python.org/versions/

@duaneg
Copy link
Contributor

duaneg commented Jun 9, 2025

Running this on Linux on current HEAD (v3.1-alpha) I get this:

...
< ready=([], [], [])
>
< ready=([], [], [])Traceback (most recent call last):
  File "/home/duaneg/src/cpython/a.py", line 11, in <module>
    print(f'< ready={ready}')
    ~~~~~^^^^^^^^^^^^^^^^^^^^
BlockingIOError: [Errno 11] write could not complete without blocking

Which I think is actually to be expected: note this is failing in the print function, not select. It is stdout that is reporting this error. Changing stdin to be non-blocking also changes stdout likewise, since those file descriptors are reading and writing from/to the same pipe when run from a POSIX shell. Changing the print string to a much longer string (e.g. ~32k characters) makes it fail quickly and reliably.

It looks like the OP's original bug is the same, note the file reported in the error is stdout:

Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>.

In the bug report the exception is being swallowed and reported by sys.unraisablehook: this sometimes happens on HEAD too, again, I think this is to be expected when flushing stdout on shutdown fails due to it being non-blocking.

In other words, I think everything is working correctly now, and actually probably was before also, given POSIX standard in/out file descriptor semantics. It is losing error output, but that possibility is unavoidable given a non-blocking stdout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

5 participants