File tree Expand file tree Collapse file tree 1 file changed +12
-4
lines changed
learning_loop_node/detector Expand file tree Collapse file tree 1 file changed +12
-4
lines changed Original file line number Diff line number Diff line change 22import contextlib
33import gc
44import logging
5+ import sys
56import os
67import shutil
78import subprocess
@@ -442,8 +443,8 @@ async def _update_model_if_required(self) -> None:
442443 return
443444
444445 match self ._detector :
445- case _ActiveDetector () as d :
446- current_version = d . model_info .version
446+ case _ActiveDetector (model_info = info ) :
447+ current_version = info .version
447448 case _Updating ():
448449 self .log .debug ('not checking for updates; model update already in progress' )
449450 return
@@ -541,9 +542,16 @@ async def _build_and_swap_detector(self, model_dir: str) -> None:
541542 return
542543
543544 if self ._exclusive_model_build and isinstance (self ._detector , _ActiveDetector ):
544- async with self .detection_lock : # wait for in-flight detections to finish
545+ async with self .detection_lock :
546+ old_logic = self ._detector .logic
545547 self ._detector = _Updating (version = model_info .version )
546- gc .collect ()
548+
549+ # Ensure that we actually delete the old DetectorLogic here to
550+ # free resources before building a new detector.
551+ refcount = sys .getrefcount (old_logic ) # 2 = old_logic local + getrefcount arg
552+ assert refcount == 2 , f'expected 2 refs to old detector logic, got { refcount } '
553+ del old_logic
554+ gc .collect ()
547555
548556 try :
549557 new_detector = await self ._detector_factory .build (model_info )
You can’t perform that action at this time.
0 commit comments