From 68d2ad01120c30d173fc26b61eb8ee3c215c5def Mon Sep 17 00:00:00 2001 From: vinay Date: Wed, 13 May 2026 22:16:48 +0100 Subject: [PATCH] Fix Windows file-locking error --- src/controller/alpasim_controller/system.py | 9 +++++++++ src/controller/tests/test_create_system_registry.py | 3 +++ 2 files changed, 12 insertions(+) diff --git a/src/controller/alpasim_controller/system.py b/src/controller/alpasim_controller/system.py index beca40e4..a8484b74 100644 --- a/src/controller/alpasim_controller/system.py +++ b/src/controller/alpasim_controller/system.py @@ -80,6 +80,15 @@ def __init__( self._solve_time_ms: float = 0.0 self._last_mpc_time_us: int | None = None + def close(self) -> None: + """Close the CSV log file handle. + + Callers must close before removing ``log_file`` on Windows, where open + files cannot be deleted. + """ + if not self._log_file_handle.closed: + self._log_file_handle.close() + def _dynamic_state_to_cg_velocity( self, dynamic_state: common_pb2.DynamicState ) -> np.ndarray: diff --git a/src/controller/tests/test_create_system_registry.py b/src/controller/tests/test_create_system_registry.py index 6e31b192..5cce5ad6 100644 --- a/src/controller/tests/test_create_system_registry.py +++ b/src/controller/tests/test_create_system_registry.py @@ -35,6 +35,7 @@ def test_create_system_via_registry(mpc_impl: MPCImplementation) -> None: """create_system() returns a System with a controller from the plugin registry.""" with tempfile.NamedTemporaryFile(mode="w", suffix=".csv", delete=False) as f: log_file = f.name + system = None try: initial_state = _make_initial_state() system = create_system( @@ -46,5 +47,7 @@ def test_create_system_via_registry(mpc_impl: MPCImplementation) -> None: assert hasattr(system, "_controller") assert hasattr(system._controller, "compute_control") finally: + if system is not None: + system.close() if os.path.exists(log_file): os.unlink(log_file)