PyO3 / PyO3/pyo3

`sys.stdout` not flushed on exit

Open
#1,490 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

documentation needs-implementer
Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

🌍 Environment
  • Your operating system and version: MacOS 10.15.7
  • Your python version: Python 3.9.1
  • How did you install python (e.g. apt or pyenv)? Did you use a virtualenv?: Homebrew; python3 -m virtualenv .venv
  • Your Rust version (rustc --version): rustc 1.50.0 (cb75ad5db 2021-02-10)
  • Your PyO3 version: 0.13.2
  • Have you tried using latest PyO3 master (replace version = "0.x.y" with git = "https://github.com/PyO3/pyo3")?: Yes, reproduces after fixing compilation errors.
💥 Reproducing

Clone https://github.com/arxanas/pyo3-bug and run PYTHONPATH=. cargo run in the root directory.

main.rs:

use pyo3::prelude::*;

fn main() -> PyResult<()> {
    Python::with_gil(|py| {
        let testing = PyModule::import(py, "testing")?;
        testing.call0("foo")?;
        Ok(())
    })
}

testing.py:

import sys

def foo():
    sys.stdout.write("Hello, world!")
    # Uncomment to fix issue:
    # sys.stdout.flush()

Note that the issue doesn't reproduce if the output ends with a newline character. Presumably that automatically flushes stdout.

This is probably due to #1355. I understand that running finalizers might break some C code, but perhaps a special case can be made for flushing stdout/stderr? It's an idempotent operation, and it might take a long time for someone to debug the issue otherwise.

If you don't want to change this behavior, then a note in the documentation somewhere would be helpful.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the behavior by running PYTHONPATH=. cargo run in the linked project, using main.rs and testing.py as the minimal example. Read the shutdown behavior related to #1355 and determine whether stdout/stderr should be flushed or whether the behavior should be documented. Done means the chosen outcome is implemented and verified for output without a trailing newline.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.