-
-
Notifications
You must be signed in to change notification settings - Fork 632
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
Mix callable alias with pure subprocess.run
: Bad file descriptor
#4792
Comments
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
Bad file descriptor
using coreutils
Bad file descriptor
using coreutilssubprocess.run
: Bad file descriptor
The Line 33 in 9f0bf03
Here xonsh --no-rc
$XONSH_SHOW_TRACEBACK = True
$XONSH_TRACE_SUBPROC = 3
import subprocess
@aliases.register('mygrep')
def _mygrep(args, stdin=None, stdout=None, stderr=None):
return subprocess.run(["/usr/bin/grep"]+args, stdin=stdin, stdout=stdout, stderr=stderr).returncode
echo hovercraft | mygrep raft # here echo is binary
# Trace run_subproc({'cmds': (['echo', 'hovercraft'], '|', ['mygrep', 'raft']), 'captured': 'hiddenobject'})
# 0: {'cmd': ['echo', 'hovercraft'], 'cls': 'subprocess.Popen', 'binary_loc': '/opt/homebrew/Cellar/coreutils/9.5/libexec/gnubin/echo', 'threadable': True, 'background': False, 'stdout': 7, 'captured': 'hiddenobject'}
# 1: {'cmd': ['mygrep', 'raft'], 'cls': 'xonsh.procs.proxies.ProcProxyThread', 'alias_name': 'mygrep', 'threadable': True, 'background': False, 'stdin': 3, 'stdout': <_io.TextIOWrapper name=9 mode='w' encoding='utf-8'>, 'stderr': <_io.TextIOWrapper name=11 mode='w' encoding='utf-8'>, 'captured': 'hiddenobject', 'captured_stdout': <_io.TextIOWrapper name=8 mode='r' encoding='utf-8'>, 'captured_stderr': <_io.TextIOWrapper name=10 mode='r' encoding='utf-8'>}
# hovercraft
from xonsh.xoreutils.echo import echo
aliases["echo"] = echo
echo hovercraft | mygrep raft # here echo is callable alias and first call is working
echo hovercraft | mygrep raft
# Trace run_subproc({'cmds': (['echo', 'hovercraft'], '|', ['mygrep', 'raft']), 'captured': 'hiddenobject'})
# 0: {'cmd': ['echo', 'hovercraft'], 'cls': 'xonsh.procs.proxies.ProcProxyThread', 'alias_name': 'echo', 'threadable': True, 'background': False, 'stdout': 7, 'captured': 'hiddenobject'}
# 1: {'cmd': ['mygrep', 'raft'], 'cls': 'xonsh.procs.proxies.ProcProxyThread', 'alias_name': 'mygrep', 'threadable': True, 'background': False, 'stdin': 3, 'stdout': <_io.TextIOWrapper name=9 mode='w' encoding='utf-8'>, 'stderr': <_io.TextIOWrapper name=11 mode='w' encoding='utf-8'>, 'captured': 'hiddenobject', 'captured_stdout': <_io.TextIOWrapper name=8 mode='r' encoding='utf-8'>, 'captured_stderr': <_io.TextIOWrapper name=10 mode='r' encoding='utf-8'>}
# Exception # Exception in thread {'cls': 'ProcProxyThread', 'name': 'Thread-17', 'func': FuncAlias({'name': 'mygrep', 'func': '_mygrep'}), 'alias': 'mygrep', 'pid': None}
# hovercraft
# Traceback (most recent call last):
File "/Users/pc/.local/xonsh-env/lib/python3.12/site-packages/xonsh/procs/proxies.py", line 470, in run
r = run_with_partial_args(
^^^^^^^^^^^^^^^^^^^^^^
File "/Users/pc/.local/xonsh-env/lib/python3.12/site-packages/xonsh/cli_utils.py", line 381, in run_with_partial_args
return func(**kwargs)
^^^^^^^^^^^^^^
File "/Users/pc/.local/xonsh-env/lib/python3.12/site-packages/xonsh/aliases.py", line 86, in __call__
return self.func(*func_args)
^^^^^^^^^^^^^^^^^^^^^
File "<stdin>", line 8, in _mygrep
File "/Users/pc/.local/xonsh-env/lib/python3.12/subprocess.py", line 548, in run
with Popen(*popenargs, **kwargs) as process:
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/pc/.local/xonsh-env/lib/python3.12/subprocess.py", line 1026, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Users/pc/.local/xonsh-env/lib/python3.12/subprocess.py", line 1914, in _execute_child
os.close(errpipe_read)
OSError: [Errno 9] Bad file descriptor
# NOTE! The error `os.close(errpipe_read): Bad file descriptor`
# occurs in `subprocess.run(['/usr/bin/grep', 'raft'], ...)` call. As you can see So let's set $XONSH_SHOW_TRACEBACK = True
import subprocess
@aliases.register('mygrep')
def _mygrep(args, stdin=None, stdout=None, stderr=None):
print('***',stdin,stdout,stderr)
return subprocess.run(["/usr/bin/grep"]+args, stdin=stdin, stdout=stdout, stderr=stderr, close_fds=False).returncode
from xonsh.xoreutils.echo import echo
aliases["echo"] = echo
echo hovercraft | mygrep raft
echo hovercraft | mygrep raft
echo hovercraft | mygrep raft
echo hovercraft | mygrep raft
echo hovercraft | mygrep raft
# It works. Conclusions:
|
I'm going to close this because the case fully captured. |
xonfig
Expected Behavior
I am trying to create a complex alias with redirected stdin. As a test I created the following wrapper function for
grep
, since it's a command that accepts input either from stdin or a file:I expect this alias to work exactly like grep. For instance:
Current Behavior
With an empty rc file the above function works as expected. However, if I first load the coreutils xontrib I see an OSError about 50% of the time. The error prints directly to the console, so redirecting the output doesn't hide the error.
Surprisingly, this behavior happens even for commands like grep that are not replaced by coreutils.
Traceback (if applicable)
Steps to Reproduce
For community
⬇️ Please click the 👍 reaction instead of leaving a
+1
or 👍 commentThe text was updated successfully, but these errors were encountered: