13

Python 2.7.3 から Python 2.7.5 にアップグレードしたときに、subprocess.Popen() を多用する内部ライブラリが自動テストに失敗し始めました。このライブラリは、スレッド環境で使用されます。問題をデバッグした後、失敗したテストで見られるエラーを示す短い Python スクリプトを作成することができました。

これはスクリプトです(「threadedsubprocess.py」と呼ばれます):

import time
import threading
import subprocess

def subprocesscall():
    p = subprocess.Popen(
        ['ls', '-l'],
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        )
    time.sleep(2) # simulate the Popen call takes some time to complete.
    out, err = p.communicate()
    print 'succeeding command in thread:', threading.current_thread().ident

def failingsubprocesscall():
    try:
        p = subprocess.Popen(
            ['thiscommandsurelydoesnotexist'],
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            )
    except Exception as e:
        print 'failing command:', e, 'in thread:', threading.current_thread().ident

print 'main thread is:', threading.current_thread().ident

subprocesscall_thread = threading.Thread(target=subprocesscall)
subprocesscall_thread.start()
failingsubprocesscall()
subprocesscall_thread.join()

注: Python 2.7.3 から実行した場合、このスクリプトは IOError で終了しません。Python 2.7.5 (同じ Ubuntu 12.04 64 ビット VM 上) から実行した場合、少なくとも 50% の確率で失敗します。

Python 2.7.5 で発生するエラーは次のとおりです。

/opt/python/2.7.5/bin/python ./threadedsubprocess.py 
main thread is: 139899583563520
failing command: [Errno 2] No such file or directory 139899583563520
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/opt/python/2.7.5/lib/python2.7/threading.py", line 808, in __bootstrap_inner
    self.run()
  File "/opt/python/2.7.5/lib/python2.7/threading.py", line 761, in run
    self.__target(*self.__args, **self.__kwargs)
  File "./threadedsubprocess.py", line 13, in subprocesscall
    out, err = p.communicate()
  File "/opt/python/2.7.5/lib/python2.7/subprocess.py", line 806, in communicate
    return self._communicate(input)
  File "/opt/python/2.7.5/lib/python2.7/subprocess.py", line 1379, in _communicate
    self.stdin.close()
IOError: [Errno 9] Bad file descriptor

close failed in file object destructor:
IOError: [Errno 9] Bad file descriptor

サブプロセス モジュールを Python 2.7.3 から Python 2.7.5 と比較すると、Popen() の __init__() 呼び出しが、コマンドの実行が何らかの形で失敗した場合に、stdin、stdout、および stderr ファイル記述子を明示的に閉じるようになりました。これは、ファイル記述子のリークを防ぐために Python 2.7.4 に適用された意図的な修正のようです ( http://hg.python.org/cpython/file/ab05e7dd2788/Misc/NEWS#l629 )。

この問題に関連すると思われる Python 2.7.3 と Python 2.7.5 の差分は、Popen __init__() にあります。

@@ -671,12 +702,33 @@
          c2pread, c2pwrite,
          errread, errwrite) = self._get_handles(stdin, stdout, stderr)

-        self._execute_child(args, executable, preexec_fn, close_fds,
-                            cwd, env, universal_newlines,
-                            startupinfo, creationflags, shell,
-                            p2cread, p2cwrite,
-                            c2pread, c2pwrite,
-                            errread, errwrite)
+        try:
+            self._execute_child(args, executable, preexec_fn, close_fds,
+                                cwd, env, universal_newlines,
+                                startupinfo, creationflags, shell,
+                                p2cread, p2cwrite,
+                                c2pread, c2pwrite,
+                                errread, errwrite)
+        except Exception:
+            # Preserve original exception in case os.close raises.
+            exc_type, exc_value, exc_trace = sys.exc_info()
+
+            to_close = []
+            # Only close the pipes we created.
+            if stdin == PIPE:
+                to_close.extend((p2cread, p2cwrite))
+            if stdout == PIPE:
+                to_close.extend((c2pread, c2pwrite))
+            if stderr == PIPE:
+                to_close.extend((errread, errwrite))
+
+            for fd in to_close:
+                try:
+                    os.close(fd)
+                except EnvironmentError:
+                    pass
+
+            raise exc_type, exc_value, exc_trace

次の 3 つの質問があると思います。

1) スレッド化された環境で、stdin、stdout、および stderr に PIPE を使用して subprocess.Popen を使用することが原則的に可能であるべきというのは本当ですか?

2) スレッドの 1 つで Popen() が失敗したときに、stdin、stdout、および stderr のファイル記述子が閉じられないようにするにはどうすればよいですか?

3) 私はここで何か間違ったことをしていますか?

4

2 に答える 2

0

開いたファイルを処理していない場合 (API のビルド時など) の他の解決策。

Windll API 呼び出しを実行して、既に開いているすべてのファイル記述子を「継承不可」としてマークすることで、問題の回避策を見つけました。これはややハックであり、Q&A はここで入手できます。

ハウツー: close_fds=True の回避策と Windows での stdout/stderr のリダイレクト

Python 2.7 のバグを回避します。

他の解決策は、Python 3.4+を使用することです:)修正されました

于 2018-02-07T18:58:03.320 に答える