Skip to content
Closed
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
8 changes: 7 additions & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Steps to reproduce the behavior:
**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots and/or Logs **
**Screenshots and/or Logs**
If applicable, attach screenshots or container logs (e.g., the relevant bits of `./scripts/logs`) to help explain your problem.

**Malcolm Version:**
Expand All @@ -35,5 +35,11 @@ If applicable, attach screenshots or container logs (e.g., the relevant bits of
- [ ] via [Kubernetes](https://cisagov.github.io/Malcolm/docs/kubernetes.html#Kubernetes) (please provide platform details: e.g., on-prem K3s, Amazon AWS EKS, Microsoft Azure AKS, etc.)
- [ ] other (please describe)

**Was AI used in discovering, diagnosing, or writing up this bug?**
- [ ] No
- [ ] Yes (please describe below)

If yes, describe how: which tool(s), what they did (e.g., found the issue, analyzed logs, drafted this report), and whether you confirmed the behavior yourself on a running Malcolm instance.

**Additional context**
Add any other context about the problem here.
6 changes: 6 additions & 0 deletions .github/ISSUE_TEMPLATE/feature-enhancement-request.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@ A clear and concise description of what you want to happen.
**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Was AI used in developing or writing this request?**
- [ ] No
- [ ] Yes (please describe below)

If yes, describe how: which tool(s), and what part they played (e.g., suggested the idea, researched alternatives, drafted this text).

**Additional context**
Add any other context or screenshots about the feature request here.
26 changes: 26 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
**Description**
What does this pull request change, and why?

**Related issue(s)**
Link any related issues (e.g., `Closes #123`).

**Type of change**
- [ ] Bug fix
- [ ] New feature or enhancement
- [ ] Documentation
- [ ] Other (please describe)

**How has this been tested?**
Describe how you verified the change, including how you ran Malcolm (ISO, Docker on Linux/Windows/macOS, Kubernetes) and any PCAP or log data you tested with.

**Documentation**
- [ ] I have updated the [documentation](https://cisagov.github.io/Malcolm/) where needed, or no update is needed

**Was AI used in writing the code, tests, or description for this pull request?**
- [ ] No
- [ ] Yes (please describe below)

If yes, describe how: which tool(s), which parts of the change they produced or modified, and how you reviewed and tested that output.

**Additional context**
Add any other context about the pull request here.
10 changes: 5 additions & 5 deletions filebeat/scripts/clean-processed-folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,15 @@ def main() -> None:
logfmt='%(message)s',
)

with open(lock_filename, 'w') as lock_file:
# Keep the lock path after the file descriptor closes. Unlinking a locked
# file lets another process create a new inode and acquire a second lock,
# so concurrent cleanups could run even while the first process is active.
with open(lock_filename, 'a') as lock_file:
try:
fcntl.flock(lock_file, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
return
else:
prune_files()
finally:
os.remove(lock_filename)
prune_files()


if __name__ == '__main__':
Expand Down
98 changes: 98 additions & 0 deletions scripts/installer/tests/unit/test_filebeat_cleanup_lockfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/env python3
# Copyright (c) 2026 Battelle Energy Alliance, LLC. All rights reserved.
"""Regressions for the Filebeat cleanup process lock.

Extract the production entrypoint to avoid container-only magic and Malcolm imports.
"""

import ast
import fcntl
import os
from pathlib import Path
import tempfile
import unittest
from unittest import mock

_SOURCE = Path(__file__).resolve().parents[4] / 'filebeat/scripts/clean-processed-folder.py'


def _load_main(lock_path, cleanup):
source = ast.parse(_SOURCE.read_text())
function = next(
entry for entry in source.body
if isinstance(entry, ast.FunctionDef) and entry.name == 'main'
)
context = {
'fcntl': fcntl,
'os': os,
'lock_filename': str(lock_path),
'prune_files': cleanup,
'set_logging': lambda *args, **kwargs: None,
'get_verbosity_env_var_count': lambda *args: 0,
}
exec(compile(ast.Module(body=[function], type_ignores=[]),
str(_SOURCE), 'exec'), context)
return context['main']


class CleanupLockfileTest(unittest.TestCase):

def setUp(self):
self.directory = tempfile.TemporaryDirectory()
self.addCleanup(self.directory.cleanup)
self.lockfile = Path(self.directory.name) / 'filebeat-cleanup.lock'
self.cleanup = mock.Mock()
self.run_cleanup = _load_main(self.lockfile, self.cleanup)

def test_contender_does_not_unlink_other_process_lock(self):
self.lockfile.touch()
with self.lockfile.open('a') as owner:
fcntl.flock(owner, fcntl.LOCK_EX | fcntl.LOCK_NB)
inode = os.stat(self.lockfile).st_ino
self.run_cleanup()
self.cleanup.assert_not_called()
self.assertTrue(self.lockfile.exists())
self.assertEqual(os.stat(self.lockfile).st_ino, inode)
# A third process must not get a new inode either.
self.run_cleanup()
self.cleanup.assert_not_called()
self.assertEqual(os.stat(self.lockfile).st_ino, inode)
fcntl.flock(owner, fcntl.LOCK_UN)
self.run_cleanup()
self.cleanup.assert_called_once_with()

def test_success_keeps_persistent_lock_inode(self):
self.run_cleanup()
self.cleanup.assert_called_once_with()
self.assertTrue(self.lockfile.exists())
inode = os.stat(self.lockfile).st_ino
self.run_cleanup()
self.assertEqual(self.cleanup.call_count, 2)
self.assertEqual(os.stat(self.lockfile).st_ino, inode)

def test_failed_prune_preserves_lock_and_allows_retry(self):
self.cleanup.side_effect = RuntimeError('cleanup failed')
with self.assertRaisesRegex(RuntimeError, 'cleanup failed'):
self.run_cleanup()
self.assertTrue(self.lockfile.exists())
inode = os.stat(self.lockfile).st_ino
self.cleanup.side_effect = None
self.run_cleanup()
self.assertEqual(self.cleanup.call_count, 2)
self.assertEqual(os.stat(self.lockfile).st_ino, inode)

def test_recursive_attempt_cannot_run_concurrent_prune(self):
calls = []

def cleanup():
calls.append('cleanup')
self.run_cleanup()

self.run_cleanup = _load_main(self.lockfile, cleanup)
self.run_cleanup()
self.assertEqual(calls, ['cleanup'])
self.assertTrue(self.lockfile.exists())


if __name__ == '__main__':
unittest.main()