-
Notifications
You must be signed in to change notification settings - Fork 17
Validate that process is actually a patching operation before terminating #367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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() | ||
| if Constants.CORE_CODE_FILE_NAME in cmdline: | ||
| self.logger.log_debug("Process is a patching operation. [PID={0}]".format(str(pid))) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar comment to use log_verbose
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
|
||
|
|
||
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mock_is_process_patching_operation_to_return_true is not defined
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
|
|
@@ -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): | ||
|
|
||
There was a problem hiding this comment.
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.