Skip to content

[BUG]: piping failing subprocess stderr to /dev/null changes exception type #13208

@emmettbutler

Description

@emmettbutler

Tracer Version(s)

3.4.1

Python Version(s)

Python 3.9 and newer

Pip Version(s)

pip 23.0.1 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)

Bug Report

When ddtrace instrumentation is active, subprocess.run() with a nonexistent binary and stderr directed to /dev/null raises OSError. This differs from the error raised from the same code when ddtrace instrumentation is not active.

Reproduction Code

$ docker run --rm -it python:3.9 /bin/bash
# pip install ddtrace


# ddtrace-run python -c 'from subprocess import run, DEVNULL; run(["fake"], stderr=DEVNULL)'
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/subprocess.py", line 1793, in _execute_child
    part = os.read(errpipe_read, 50000)
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.9/subprocess.py", line 505, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/usr/local/lib/python3.9/site-packages/ddtrace/contrib/internal/trace_utils.py", line 361, in wrapper
    return func(mod, pin, wrapped, instance, args, kwargs)
  File "/usr/local/lib/python3.9/site-packages/ddtrace/contrib/internal/subprocess/patch.py", line 431, in _traced_subprocess_init
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python3.9/subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/local/lib/python3.9/subprocess.py", line 1799, in _execute_child
    os.close(errpipe_read)
OSError: [Errno 9] Bad file descriptor



# python -c 'from subprocess import run, DEVNULL; run(["fake"], stderr=DEVNULL)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.9/subprocess.py", line 505, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/usr/local/lib/python3.9/subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/local/lib/python3.9/subprocess.py", line 1837, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'fake'

Error Logs

No response

Libraries in Use

No response

Operating System

Linux d8fe41e749a5 6.5.11-linuxkit #1 SMP PREEMPT Wed Dec 6 17:08:31 UTC 2023 aarch64 GNU/Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions