Skip to content

Commit e881df4

Browse files
committed
Prevent uninstall subprocess from exiting when the service is stopped.
1 parent e4a76a4 commit e881df4

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

myDevices/cloud/client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ def ProcessAgentCommand(self, message):
422422
error = None
423423
try:
424424
if message['suffix'] == 'uninstall':
425-
output, result = executeCommand('sudo /etc/myDevices/uninstall/uninstall.sh')
425+
output, result = executeCommand('sudo /etc/myDevices/uninstall/uninstall.sh', disablePipe=True)
426426
debug('ProcessAgentCommand: {}, result: {}, output: {}'.format(message, result, output))
427427
if result != 0:
428428
error = 'Error uninstalling agent'
@@ -434,6 +434,8 @@ def ProcessAgentCommand(self, message):
434434
# else:
435435
# info('Set config item: {} {}'.format(key, value))
436436
# self.config.set('Agent', key, value)
437+
else:
438+
error = 'Unknown agent command: {}'.format(message['suffix'])
437439
except Exception as ex:
438440
error = '{}: {}'.format(type(ex).__name__, ex)
439441
self.EnqueueCommandResponse(message, error)
@@ -495,7 +497,7 @@ def ProcessDeviceCommand(self, message):
495497
elif message['suffix'] == 'delete':
496498
result = self.sensorsClient.RemoveSensor(payload['sensorId'])
497499
else:
498-
info('Unknown device command: {}'.format(message['suffix']))
500+
error = 'Unknown device command: {}'.format(message['suffix'])
499501
debug('ProcessDeviceCommand result: {}'.format(result))
500502
if result is False:
501503
error = 'Device command failed'

myDevices/utils/subprocess.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
This module contains functions for launching subprocesses and returning output from them.
33
"""
4-
from subprocess import Popen, PIPE
4+
from subprocess import Popen, PIPE, DEVNULL
55
from myDevices.utils.logger import debug, info, error, exception
66

77
def setMemoryLimits():
@@ -13,16 +13,20 @@ def setMemoryLimits():
1313
except:
1414
pass
1515

16-
def executeCommand(command, increaseMemoryLimit=False):
16+
def executeCommand(command, increaseMemoryLimit=False, disablePipe=False):
1717
"""Execute a specified command, increasing the processes memory limits if specified"""
1818
debug('executeCommand: ' + command)
1919
output = ''
2020
returncode = 1
2121
try:
22-
setLimit = None
22+
preexec = None
23+
pipe = PIPE
2324
if increaseMemoryLimit:
24-
setLimit = setMemoryLimits
25-
process = Popen(command, stdout=PIPE, stderr=PIPE, shell=True, preexec_fn=setLimit)
25+
preexec = setMemoryLimits
26+
if disablePipe:
27+
debug('Disable pipe to prevent child exiting when parent exits')
28+
pipe = DEVNULL
29+
process = Popen(command, stdout=pipe, stderr=pipe, shell=True, preexec_fn=preexec)
2630
(stdout_data, stderr_data) = process.communicate()
2731
returncode = process.wait()
2832
returncode = process.returncode

0 commit comments

Comments
 (0)