|
12 | 12 | from codeflash.api.aiservice import AiServiceClient, LocalAiServiceClient
|
13 | 13 | from codeflash.cli_cmds.console import console, logger, progress_bar
|
14 | 14 | from codeflash.code_utils import env_utils
|
15 |
| -from codeflash.code_utils.code_utils import cleanup_paths |
| 15 | +from codeflash.code_utils.code_utils import cleanup_paths, get_run_tmp_file |
16 | 16 | from codeflash.code_utils.env_utils import get_pr_number
|
17 | 17 | from codeflash.either import is_successful
|
18 | 18 | from codeflash.models.models import ValidCode
|
@@ -289,31 +289,36 @@ def run(self) -> None:
|
289 | 289 | f"{function_to_optimize.qualified_name}"
|
290 | 290 | )
|
291 | 291 | console.rule()
|
| 292 | + function_optimizer = None |
| 293 | + try: |
| 294 | + function_optimizer = self.create_function_optimizer( |
| 295 | + function_to_optimize, |
| 296 | + function_to_tests=function_to_tests, |
| 297 | + function_to_optimize_source_code=validated_original_code[original_module_path].source_code, |
| 298 | + function_benchmark_timings=function_benchmark_timings, |
| 299 | + total_benchmark_timings=total_benchmark_timings, |
| 300 | + original_module_ast=original_module_ast, |
| 301 | + original_module_path=original_module_path, |
| 302 | + ) |
292 | 303 |
|
293 |
| - function_optimizer = self.create_function_optimizer( |
294 |
| - function_to_optimize, |
295 |
| - function_to_tests=function_to_tests, |
296 |
| - function_to_optimize_source_code=validated_original_code[original_module_path].source_code, |
297 |
| - function_benchmark_timings=function_benchmark_timings, |
298 |
| - total_benchmark_timings=total_benchmark_timings, |
299 |
| - original_module_ast=original_module_ast, |
300 |
| - original_module_path=original_module_path, |
301 |
| - ) |
302 |
| - |
303 |
| - self.current_function_optimizer = ( |
304 |
| - function_optimizer # needed to clean up from the outside of this function |
305 |
| - ) |
306 |
| - best_optimization = function_optimizer.optimize_function() |
307 |
| - if self.functions_checkpoint: |
308 |
| - self.functions_checkpoint.add_function_to_checkpoint( |
309 |
| - function_to_optimize.qualified_name_with_modules_from_root(self.args.project_root) |
| 304 | + self.current_function_optimizer = ( |
| 305 | + function_optimizer # needed to clean up from the outside of this function |
310 | 306 | )
|
311 |
| - if is_successful(best_optimization): |
312 |
| - optimizations_found += 1 |
313 |
| - else: |
314 |
| - logger.warning(best_optimization.failure()) |
315 |
| - console.rule() |
316 |
| - continue |
| 307 | + best_optimization = function_optimizer.optimize_function() |
| 308 | + if self.functions_checkpoint: |
| 309 | + self.functions_checkpoint.add_function_to_checkpoint( |
| 310 | + function_to_optimize.qualified_name_with_modules_from_root(self.args.project_root) |
| 311 | + ) |
| 312 | + if is_successful(best_optimization): |
| 313 | + optimizations_found += 1 |
| 314 | + else: |
| 315 | + logger.warning(best_optimization.failure()) |
| 316 | + console.rule() |
| 317 | + continue |
| 318 | + finally: |
| 319 | + if function_optimizer is not None: |
| 320 | + function_optimizer.cleanup_generated_files() |
| 321 | + |
317 | 322 | ph("cli-optimize-run-finished", {"optimizations_found": optimizations_found})
|
318 | 323 | if self.functions_checkpoint:
|
319 | 324 | self.functions_checkpoint.cleanup()
|
@@ -351,6 +356,9 @@ def cleanup_temporary_paths(self) -> None:
|
351 | 356 | if self.current_function_optimizer:
|
352 | 357 | self.current_function_optimizer.cleanup_generated_files()
|
353 | 358 |
|
| 359 | + if hasattr(get_run_tmp_file, "tmpdir"): |
| 360 | + get_run_tmp_file.tmpdir.cleanup() |
| 361 | + del get_run_tmp_file.tmpdir |
354 | 362 | cleanup_paths([self.test_cfg.concolic_test_root_dir, self.replay_tests_dir])
|
355 | 363 |
|
356 | 364 |
|
|
0 commit comments