Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leak when run subprocess in python

When I run this I get a leak. I am not sure what it is happening. I guess pipe does not close or might be something else happening.

def deactivateMetadataDevice(input_dmd_lun_wwn):
    #print('pvremove /dev/mapper/' + input_dmd_lun_wwn)
    status_cmd = False
    ps = subprocess.Popen('/sbin/pvremove /dev/mapper/' + input_dmd_lun_wwn, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    for line in iter(ps.stdout.readline, ''):
        print line
        if re.search('wiped', line):
           status_cmd = True
        else:
           # Cleaning metadata and removing from LVM if ok then return true
           status_cmd = False
           raise Warning('\t\t PV /dev/mapper/'+ input_dmd_lun_wwn +' belongs to Volume Group')

    return status_cmd

Getting this issue when I run the code above:

File descriptor 4 (pipe:[323948]) leaked on pvremove invocation. Parent PID 15380: python
like image 207
Askar Avatar asked Mar 22 '23 16:03

Askar


1 Answers

The problem is that you return before reading all of the data in the pipe and you don't issue a wait to get the return code and remove the process from the operating system pid table. I think a few tweaks will do it (I also removed a few things I thought were redundant).

def deactivateMetadataDevice(input_dmd_lun_wwn):
    #print('pvremove /dev/mapper/' + input_dmd_lun_wwn)
    status_cmd = False
    ps = subprocess.Popen('/sbin/pvremove /dev/mapper/' + input_dmd_lun_wwn, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    for line in ps.stdout:
        print line
        if 'wiped' in line:
           status_cmd = True
    ps.wait()
    # need to handle ps.returncode != 0
    if status_cmd is False:
       # Cleaning metadata and removing from LVM if ok then return true
       raise Warning('\t\t PV /dev/mapper/'+ input_dmd_lun_wwn +' belongs to Volume Group')
    return status_cmd # likely not needed because you are using exceptions for errors
like image 138
tdelaney Avatar answered Apr 06 '23 00:04

tdelaney