使用stderr = PIPE和Popen.communicate()获取竞争条件

时间:2018-08-31 13:51:06

标签: python subprocess lvm

我正在开发一个kubernetes flexvolume驱动程序,该驱动程序可以创建LVM设备,并创建和安装文件系统。

出于某种原因,我偶尔会遇到死锁,根据文档说明,使用Popen.communicate()时不应发生死锁。

 Traceback (most recent call last):
  File "/usr/libexec/kubernetes/kubelet/plugins/volume/exec/example~lvm/lvm", line 356, in <module>
    attach(cfg)
  File "/usr/libexec/kubernetes/kubelet/plugins/volume/exec/example~lvm/lvm", line 231, in attach
    result = _lvcreate(cfg['lv_name'], cfg['lv_size'], cfg['vg_name'])
  File "/usr/libexec/kubernetes/kubelet/plugins/volume/exec/example~lvm/lvm", line 148, in _lvcreate
    _out, _err = proc.communicate()
  File "/usr/lib64/python2.7/subprocess.py", line 800, in communicate
    return self._communicate(input)
  File "/usr/lib64/python2.7/subprocess.py", line 1401, in _communicate
    stdout, stderr = self._communicate_with_poll(input)
  File "/usr/lib64/python2.7/subprocess.py", line 1455, in _communicate_with_poll
    ready = poller.poll()
KeyboardInterrupt

这有时在我的lvcreate和mkfs调用期间发生。设置shell = True似乎无关紧要。

_lv  = None
_cmd = [ '/sbin/lvcreate', '--type', 'linear', '--size', lv_size, '--name', lv_name, vg_name ]
_out, _err = None, None
proc = subprocess.Popen(_cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
_out, _err = proc.communicate()
if proc.returncode != 0:
    return (_lv, _err, proc.returncode)

环境:

$ uname -a
Linux myhost.example.com 4.1.12-124.17.2.el7uek.x86_64 #2 SMP Tue Jul 17 20:28:07 PDT 2018 x86_64 x86_64 x86_64 GNU/Linux

# python -V
Python 2.7.5

如果我设置stderr = None而不是stderr = subprocess.PIPE,我将永远不会看到此问题。

感谢您的帮助。

0 个答案:

没有答案