Skip to content

os.close(1) crashes REPL #6146

@omar-abdelgawad

Description

@omar-abdelgawad

Summary

I was trying to work on #5521 and I noticed that in the repl if you try to os.close(sys.stdout.fileno()) or more concisely os.close(1) it crashes with a Readline error: Errno(EBADF) like this:

omarabdelgawad@omarabdelgawad:~/my_workspace/RustPython$ cargo run --release 
    Finished `release` profile [optimized] target(s) in 0.11s
     Running `target/release/rustpython`
Welcome to the magnificent Rust Python 0.4.0 interpreter 😱 🖖
RustPython 3.13.0
Type "help", "copyright", "credits" or "license" for more information.
>>>>> import os
>>>>> os.close(1)
Readline error: Errno(EBADF)
omarabdelgawad@omarabdelgawad:~/my_workspace/RustPython$

Details

where with CPython it doesnt crash:

omarabdelgawad@omarabdelgawad:~$ python
Python 3.12.3 (main, Aug 14 2025, 17:47:21) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.close(1)
>>> x = 2
>>> print(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 9] Bad file descriptor
>>> ^C
KeyboardInterrupt
>>> 
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
OSError: [Errno 9] Bad file descriptor
omarabdelgawad@omarabdelgawad:~$ 

I also noticed that even with -c "script" it kind of silently crashes:

omarabdelgawad@omarabdelgawad:~/my_workspace/RustPython$ cargo run --release -q -- -c "import os; os.close(1);print('hello')"
omarabdelgawad@omarabdelgawad:~/my_workspace/RustPython$ 

while CPython has the same behaviour:

omarabdelgawad@omarabdelgawad:~/my_workspace/projects/github_repos/RustPython$ python -c "import os; os.close(1);print('hello')"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
OSError: [Errno 9] Bad file descriptor
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
OSError: [Errno 9] Bad file descriptor

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions