diff --git a/src/agentscope/manager/_monitor.py b/src/agentscope/manager/_monitor.py index 3fc9677dd..b194c770f 100644 --- a/src/agentscope/manager/_monitor.py +++ b/src/agentscope/manager/_monitor.py @@ -171,6 +171,15 @@ def _create_monitor_db(self) -> None: self.session = sessionmaker(bind=self.engine) + def _close_monitor_db(self) -> None: + """Close the monitor database to avoid file occupation error in + windows.""" + if self.session is not None: + self.session.dispose() + + if self.engine is not None: + self.engine.close_all() + def update_image_tokens( self, model_name: str, @@ -318,6 +327,9 @@ def deserialize(self, data: dict) -> None: def flush(self) -> None: """Flush the monitor manager.""" + # Close the database before flushing + self._close_monitor_db() + self.use_monitor = False self.session = None self.engine = None diff --git a/tests/monitor_test.py b/tests/monitor_test.py index 6ae5d74fb..3808e6661 100644 --- a/tests/monitor_test.py +++ b/tests/monitor_test.py @@ -7,10 +7,9 @@ import os import shutil from pathlib import Path -from loguru import logger import agentscope -from agentscope.manager import MonitorManager +from agentscope.manager import MonitorManager, ASManager class MonitorManagerTest(unittest.TestCase): @@ -93,6 +92,7 @@ def test_monitor(self) -> None: def tearDown(self) -> None: """Tear down the test environment.""" + ASManager.get_instance().flush() shutil.rmtree("./test_runs") @@ -148,7 +148,7 @@ def test_disabled_monitor(self) -> None: def tearDown(self) -> None: """Tear down the test environment.""" - logger.remove() + ASManager.get_instance().flush() shutil.rmtree("./test_runs")