Simple example raise OSError: [Errno 9] Bad file descriptor.

Issue #27 invalid
pahaz NA created an issue

Hi, I try work with you solution and get a strange behavior.

My test_sarger.py:

from sarge import Feeder, run, Capture

feeder = Feeder()
pipe = run('cat | python -c "while 1: print(input().upper())"', input=feeder, stdout=Capture(), stderr=Capture(), async=True)

feeder.feed('Hello')
feeder.feed(b'Goodbye')
feeder.close()

pipe.wait()

print(pipe.stdout.text)
# print(pipe.stderr.text)

When I run it and in 1/10 case I have got an error:

(venv)root@precise64:/vagrant# python test_sarger.py
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.2/threading.py", line 740, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.2/threading.py", line 693, in run
    self._target(*self._args, **self._kwargs)
  File "/var/lib/wflow/venv/lib/python3.2/site-packages/sarge/__init__.py", line 1136, in run_node
    result = getattr(self, method)(node, input, async)
  File "/var/lib/wflow/venv/lib/python3.2/site-packages/sarge/__init__.py", line 1247, in run_logical_node
    curr.cmd.run(input=stdin, async=use_async)
  File "/var/lib/wflow/venv/lib/python3.2/site-packages/sarge/__init__.py", line 632, in run
    self.process = p = Popen(self.args, **self.kwargs)
  File "/usr/lib/python3.2/subprocess.py", line 745, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.2/subprocess.py", line 1361, in _execute_child
    raise child_exception_type(errno_num, err_msg)
OSError: [Errno 9] Bad file descriptor

Is this a normal behavior?

Comments (4)

  1. pahaz NA reporter

    I found the solution and probably problem reason:

    from sarge import Feeder, run, Capture, get_both
    import logging
    
    from sarge import logger
    l = logger
    l.setLevel(logging.DEBUG)
    l.addHandler(logging.StreamHandler())
    
    feeder = Feeder()
    cin = Capture(buffer_size=1)
    cout = Capture(buffer_size=1)
    pipe = run('cat | python -c "while 1: print(input().upper())"', input=feeder, stdout=cin, stderr=cout, async=True)
    pipe.wait_events()
    
    feeder.feed('Hello')
    feeder.feed(b'Goodbye\n')
    feeder.close()
    
    pipe.close()
    
    print(pipe.stdout.text, pipe.stderr.text)
    # print(pipe.stderr.text)
    

    The main problem solver is pipe.wait_events(). I think that main problem in feeder.close(), because without pipe.wait_events() command proces may start after feeder is closed.

    pipe.wait_events() - sync point for all the commands have been created.

    But now, I have a new issuse: https://bitbucket.org/vinay.sajip/sarge/issue/28/eoferror-eof-when-reading-a-line

  2. Vinay Sajip repo owner

    I am not sure if a wait_events() call should be added here - it may be that the caller of run wants to do other work, and only needs to wait for all the events at a later time. This could be mentioned in the documentation. If we implement this call here, we are forcing the caller to synchronise when they may not want to.

  3. Log in to comment