Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/extension/src/ProcessHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,23 @@ def is_process_running(self, pid):
# According to "man 2 kill" possible error values are (EINVAL, EPERM, ESRCH) Thus considering this as an error
return False

def is_process_patching_operation(self, pid):
try:
with self.env_layer.file_system.open("/proc/{0}/cmdline".format(str(pid)), mode="r") as cmdline_file:
cmdline = cmdline_file.read()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment/share how the output of the read line would look like.

if Constants.CORE_CODE_FILE_NAME in cmdline:
self.logger.log_debug("Process is a patching operation. [PID={0}]".format(str(pid)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is machine-specific log, we can use log_verbose instead of log_debug. (used for fleet wide diagnosing)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok to change to log_verbose

return True
else:
self.logger.log_debug("Process is not a patching operation. [PID={0}]".format(str(pid)))
return False

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment to use log_verbose

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please leave as log_debug and include what it actually shows up as instead of our operation. This demonstrates if this actually fixed or changed any outcome at scale.

except Exception as error:
self.logger.log_debug("Error checking if process is a patching operation. [PID={0}] [Error={1}]".format(str(pid), repr(error)))
return True # If we cannot determine, assume it is a patching operation to be safe
Comment on lines +202 to +214

def kill_process(self, pid):
try:
if self.is_process_running(pid):
if self.is_process_running(pid) and self.is_process_patching_operation(pid):
self.logger.log("Terminating process: [PID={0}]".format(str(pid)))
os.kill(pid, signal.SIGTERM)
except OSError as error:
Expand Down
6 changes: 6 additions & 0 deletions src/extension/tests/Test_ProcessHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def tearDown(self):
def mock_is_process_running_to_return_true(self, pid):
return True

def mock_is_process_patching_operation_to_return_true(self, pid):
return True

def mock_os_kill_to_raise_exception(self, pid, sig):
raise OSError
Comment on lines 49 to 56

Expand Down Expand Up @@ -133,6 +136,8 @@ def test_kill_process(self):
# setting mocks
is_process_running_backup = ProcessHandler.is_process_running
ProcessHandler.is_process_running = self.mock_is_process_running_to_return_true
is_process_patching_operation_backup = ProcessHandler.is_process_patching_operation
ProcessHandler.is_process_patching_operation = self.mock_is_process_patching_operation_to_return_true

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mock_is_process_patching_operation_to_return_true is not defined
Might have accidently defined mock_is_process_running_to_return_true again.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it. I noticed it too.

os_kill_backup = os.kill
os.kill = self.mock_os_kill_to_raise_exception

Expand All @@ -143,6 +148,7 @@ def test_kill_process(self):

# reseting mocks
ProcessHandler.is_process_running = is_process_running_backup
ProcessHandler.is_process_patching_operation = is_process_patching_operation_backup
os.kill = os_kill_backup

def test_get_python_cmd(self):
Expand Down
Loading