Skip to content

Commit d869462

Browse files
committed
Ensure detector resources are freed in mutual exclusive update
1 parent 35cf41d commit d869462

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

learning_loop_node/detector/detector_node.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import contextlib
33
import gc
44
import logging
5+
import sys
56
import os
67
import shutil
78
import 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)

0 commit comments

Comments
 (0)