Skip to content
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

Fix - Race condition when trying to add a log file that's been scheduled for removal #1302

Merged
merged 5 commits into from
Oct 21, 2024

Conversation

alesnovak-s1
Copy link
Collaborator

@alesnovak-s1 alesnovak-s1 commented Oct 16, 2024

Based on the comments the lock was initially meant to lock status fields used in generate_status.
Now it ensures synchronized access to fields needed for asynchonous workflow of log files.
This is already causing a timeout when retrieving status from the agent (https://sentinelone.atlassian.net/browse/DTIN-4769) and will be handled in another PR.

The Bug:

When a log file (path, worker_id) is scheduled for removal, the removal is done in two separate steps:

  • __remove_logs_scheduled_for_deletion
    • Instructs all matchers from __logs_pending_removal to finish
    • Clears __logs_pending_removal
  • __purge_finished_log_matchers
    • Removes all finished matchers from __logs_pending_removal, __dynamic_matchers, __dynamic_paths

The race condition can be reached when a path is scheduled for removal and the matchers are still finishing.
When add_log_config is called it considers log path to be active and skips adding it. The log file is later removed.

In log we can see sequence like this one:

Log ( log_path='xy', worker_id='default' ) for monitor 'scalyr_agent.builtin_monitors.kubernetes_monitor' is pending removal

Tried to add new log file 'xy' for monitor 'scalyr_agent.builtin_monitors.kubernetes_monitor', but it is already being monitored by 'scalyr_agent.builtin_monitors.kubernetes_monitor'

Removing log file ( log_path='xy', worker_id='default' ) for 'scheduled-deletion'

Copy link

github-actions bot commented Oct 16, 2024

Test Results

     20 files  ±  0       20 suites  ±0   31m 28s ⏱️ -11s
1 497 tests +  8  1 477 ✔️ +  8    20 💤 ±0  0 ±0 
7 156 runs  +40  6 929 ✔️ +40  227 💤 ±0  0 ±0 

Results for commit 423aea8. ± Comparison against base commit c189190.

♻️ This comment has been updated with latest results.

@alesnovak-s1 alesnovak-s1 force-pushed the DTIN-4631-friendly-fix branch from ea66872 to 59f09e8 Compare October 17, 2024 09:44
@alesnovak-s1 alesnovak-s1 changed the title Fixes Fix - Race condition when trying to add a log file that's been scheduled for removal Oct 17, 2024
@alesnovak-s1 alesnovak-s1 marked this pull request as ready for review October 17, 2024 12:43
# We don't want to be creating an empty default dict by get.
if path not in self.__paths:
return {}.items()

return self.__paths[path].items()
Copy link
Contributor

Choose a reason for hiding this comment

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

Up to you but I think this is ok: return self.__paths.get(path, {}).items()

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, definitely!

@@ -435,7 +445,13 @@ def keys(self):

def pop(self, path, worker_id, default=None):
# type: (six.text_type, six.text_type, Optional[object]) -> Optional[object]
return self.__paths[path].pop(worker_id, default)

value = self.__paths[path].pop(worker_id, default)
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't we need to be careful that path exists in self.__paths?
Maybe better to be value = self.__paths.get(path, {}).pop(worker_id, default) or something similar

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

self.__path is a defaultdict. But this way it's not consistent and a bit confusing. I will ditch the defaultdict, use this way and modify a set method as well so it's obvious what's expected to happen.


value = self.__paths[path].pop(worker_id, default)

if not self.__paths[path]:
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly this could be if path in self.__paths and not self.__paths[path]:

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It will work because of defaultdict, but it's confusing, I will change it. Thanks!

Copy link
Contributor

@jmakar-s1 jmakar-s1 left a comment

Choose a reason for hiding this comment

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

Looks good. One consideration for future work, to make the use of the lock clearer could use with self.__lock: to avoid having to explicitly release it

@alesnovak-s1 alesnovak-s1 merged commit 0894391 into master Oct 21, 2024
97 of 113 checks passed
@alesnovak-s1 alesnovak-s1 deleted the DTIN-4631-friendly-fix branch October 21, 2024 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants