Getting race condition using stderr=PIPE with Popen.communicate()

你说的曾经没有我的故事 提交于 2020-01-06 05:07:08

问题


I'm developing a kubernetes flexvolume driver that creates LVM devices, and creates and mounts filesystems.

For some reason I occasionally get deadlocks that according to the documentation shouldn't happen when using 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

This sometimes occurs during my lvcreate, and mkfs calls. Setting shell=True doesn't seem to matter.

_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)

Environment:

$ 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

If I set stderr=None instead of stderr=subprocess.PIPE I never see this issue.

来源:https://stackoverflow.com/questions/52116952/getting-race-condition-using-stderr-pipe-with-popen-communicate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!