Skip to content
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

Bug: piping output of commands results in BrokenPipeError #2320

Closed
1 task done
guacs opened this issue Oct 18, 2023 · 3 comments
Closed
1 task done

Bug: piping output of commands results in BrokenPipeError #2320

guacs opened this issue Oct 18, 2023 · 3 comments
Labels
⬆️ upstream bug Caused by a bug from the upstream dependencies

Comments

@guacs
Copy link

guacs commented Oct 18, 2023

  • I have searched the issue tracker and believe that this is not a duplicate.

Make sure you run commands with -v flag before pasting the output.

Steps to reproduce

Piping the output of a command to something like grep causes a BrokenPipeError. The following command should be able to reproduce the issue (might need to run it a few times since it's somewhat random).

pdm venv create --name test-1
pdm venv create --name test-2
pdm venv create --name test-3
pdm venv list | grep -q "test"

The traceback:

Traceback (most recent call last):
  File "/home/guacs/.local/bin/pdm", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/guacs/.local/share/pdm/venv/lib/python3.11/site-packages/pdm/core.py", line 290, in main
    return Core().main(args or sys.argv[1:])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/guacs/.local/share/pdm/venv/lib/python3.11/site-packages/pdm/core.py", line 208, in main
    raise cast(Exception, err).with_traceback(traceback) from None
  File "/home/guacs/.local/share/pdm/venv/lib/python3.11/site-packages/pdm/core.py", line 203, in main
    self.handle(project, options)
  File "/home/guacs/.local/share/pdm/venv/lib/python3.11/site-packages/pdm/core.py", line 157, in handle
    command.handle(project, options)
  File "/home/guacs/.local/share/pdm/venv/lib/python3.11/site-packages/pdm/cli/commands/venv/list.py", line 23, in handle
    project.core.ui.echo(f"{mark}  [success]{ident}[/]: {venv.root}")
  File "/home/guacs/.local/share/pdm/venv/lib/python3.11/site-packages/pdm/termui.py", line 187, in echo
    console.print(message, **kwargs)
  File "/home/guacs/.local/share/pdm/venv/lib/python3.11/site-packages/rich/console.py", line 1672, in print
    with self:
  File "/home/guacs/.local/share/pdm/venv/lib/python3.11/site-packages/rich/console.py", line 864, in __exit__
    self._exit_buffer()
  File "/home/guacs/.local/share/pdm/venv/lib/python3.11/site-packages/rich/console.py", line 822, in _exit_buffer
    self._check_buffer()
  File "/home/guacs/.local/share/pdm/venv/lib/python3.11/site-packages/rich/console.py", line 2064, in _check_buffer
    self.file.flush()
BrokenPipeError: [Errno 32] Broken pipe
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
BrokenPipeError: [Errno 32] Broken pipe

Actual behavior

A BrokenPipeError is raised.

Expected behavior

No exceptions should be raised the piping should work without any issues.

Environment Information

# Paste the output of `pdm info && pdm info --env` below:
Virtualenv /home/guacs/.local/share/pdm/venvs/litestar-SQGtRF1u-test-python-3-8-pydantic-v1 is reused.
PDM version:
  2.9.3
Python Interpreter:
  ~ (3.12)
Project Root:
  ~
Local Packages:

{
  "implementation_name": "cpython",
  "implementation_version": "3.12.0",
  "os_name": "posix",
  "platform_machine": "x86_64",
  "platform_release": "5.15.90.1-microsoft-standard-WSL2",
  "platform_system": "Linux",
  "platform_version": "#1 SMP Fri Jan 27 02:56:13 UTC 2023",
  "python_full_version": "3.12.0",
  "platform_python_implementation": "CPython",
  "python_version": "3.12",
  "sys_platform": "linux"
}

EDIT: updated the reproduction steps

@guacs
Copy link
Author

guacs commented Oct 18, 2023

This behavior is documented in the official Python documentation here which also provides a workaround.

@frostming
Copy link
Collaborator

It seems not reproducible on my side. Can you apply the workaround you mentioned and check if that works?

@guacs
Copy link
Author

guacs commented Oct 20, 2023

I did some testing, and it seems the steps I had described to reproduce it wasn't correct. I'll update it in the main description itself. The issue is due to rich so I think it might not even be a pdm bug. For example, the following will consistently reproduce the issue:

# file: foo.py
from rich import Console

console = Console()

console.print("foo")
console.print("bar")
console.print("baz")

After this, running python foo.py | grep -q "foo" will consistently reproduce the issue. This was reported previously here.

@frostming frostming added the ⬆️ upstream bug Caused by a bug from the upstream dependencies label Oct 23, 2023
@frostming frostming closed this as not planned Won't fix, can't repro, duplicate, stale Jun 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⬆️ upstream bug Caused by a bug from the upstream dependencies
Projects
None yet
Development

No branches or pull requests

2 participants