google / gvisor

Application Kernel for Containers

Home Page:https://gvisor.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Printing an empty string to a PTY in Python with `-u` causes the program to hang

thecodingwizard opened this issue · comments

Description

If I make the following Python file:

print("")

and I run this Python file from within a PTY with the -u flag (ie. python -u main.py from within a PTY), the program will hang indefinitely.

I think you can run the Python file from within a pty with import pty; pty.spawn(["python", "-u", "main.py"]). I used docker run --rm -it --runtime=runsc python3.11-bullseye /bin/bash and manually created + ran the file to test.

I suspect it's because this line of code is being run:

return 0, linuxerr.ErrWouldBlock

I believe Python will make a write() syscall that writes an empty string to stdout, causing the issue.

I think this also means that print("something", file=sys.stderr, end="") won't work, which means that if an exception is raised in a Python script running within a PTY, it will hang forever.

raise Exception("hello")

^ This will never finish when run inside a PTY with the -u flag; I think it's because it makes a print() call with end="".

Steps to reproduce

No response

runsc version

No response

docker version (if using docker)

No response

uname

No response

kubectl (if using Kubernetes)

No response

repo state (if built from source)

No response

runsc debug logs (if available)

No response

Thanks for another PR. Similar to the other one, could you add a test to test/syscalls/linux/pty.cc?

I don't like to be too nitpicky about tests for very small changes, but the PTY code especially is finnicky and difficult. Behavior is complex and I just want to ensure we're doing the same thing as Linux.