fix: docker container cleanup to prevent accumulation and system slowdown (#975)

* Initial plan for issue

* Fix Docker container cleanup issue by using try-finally block

Co-authored-by: peteryang1 <25981102+peteryang1@users.noreply.github.com>

* Fix additional Docker container leaks in health_check and GPU test functions

Co-authored-by: peteryang1 <25981102+peteryang1@users.noreply.github.com>

* Remove temporary test files and finalize Docker container cleanup fix

Co-authored-by: peteryang1 <25981102+peteryang1@users.noreply.github.com>

* Refactor container cleanup code to reduce duplication as requested in review feedback

Co-authored-by: peteryang1 <25981102+peteryang1@users.noreply.github.com>

* Refactor container cleanup to use shared function and always stop before remove

Co-authored-by: peteryang1 <25981102+peteryang1@users.noreply.github.com>

* fix CI

* Fix mypy type checking errors for Docker container cleanup

Co-authored-by: peteryang1 <25981102+peteryang1@users.noreply.github.com>

* fix CI

* Remove unnecessary _cleanup_container wrapper method in DockerEnv class

Co-authored-by: peteryang1 <25981102+peteryang1@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: peteryang1 <25981102+peteryang1@users.noreply.github.com>
Co-authored-by: Xu Yang <xuyang1@microsoft.com>
This commit is contained in:
Copilot
2025-06-19 18:32:50 +08:00
committed by GitHub
parent 977136f1f3
commit e03fca3206
5 changed files with 64 additions and 21 deletions
+13
View File
@@ -13,6 +13,7 @@ from rdagent.utils.env import (
LocalEnv,
QlibDockerConf,
QTDockerEnv,
cleanup_container,
)
DIRNAME = Path(__file__).absolute().resolve().parent
@@ -142,6 +143,18 @@ class EnvUtils(unittest.TestCase):
# docker run --memory=10m -it --rm local_qlib:latest python -c 'import numpy as np; print(123); size_mb = 1; size = size_mb * 1024 * 1024 // 8; array = np.random.randn(size).astype(np.float64); array[0], array[-1] = 1.0, 1.0; print(321)'
# docker run --memory=10g -it --rm local_qlib:latest python -c 'import numpy as np; print(123); size_mb = 1; size = size_mb * 1024 * 1024 // 8; array = np.random.randn(size).astype(np.float64); array[0], array[-1] = 1.0, 1.0; print(321)'
def test_cleanup_container_import(self):
"""Test that cleanup_container function can be imported and has correct interface."""
# Test that the function exists and can be called
self.assertTrue(callable(cleanup_container))
# Test with None (should not raise an exception)
cleanup_container(None, "test context")
# The function should accept positional and keyword arguments
cleanup_container(None)
cleanup_container(None, context="test")
if __name__ == "__main__":
unittest.main()