Ensure object descriptions get claened up

This commit is contained in:
Nicolas Mowen 2025-12-20 17:11:51 -07:00
parent f657020e99
commit 4582cfbbca
2 changed files with 17 additions and 3 deletions

View File

@ -131,6 +131,8 @@ class ObjectDescriptionProcessor(PostProcessorApi):
) )
): ):
self._process_genai_description(event, camera_config, thumbnail) self._process_genai_description(event, camera_config, thumbnail)
else:
self.cleanup_event(event.id)
def __regenerate_description(self, event_id: str, source: str, force: bool) -> None: def __regenerate_description(self, event_id: str, source: str, force: bool) -> None:
"""Regenerate the description for an event.""" """Regenerate the description for an event."""
@ -204,6 +206,17 @@ class ObjectDescriptionProcessor(PostProcessorApi):
) )
return None return None
def cleanup_event(self, event_id: str) -> None:
"""Clean up tracked event data to prevent memory leaks.
This should be called when an event ends, regardless of whether
genai processing is triggered.
"""
if event_id in self.tracked_events:
del self.tracked_events[event_id]
if event_id in self.early_request_sent:
del self.early_request_sent[event_id]
def _read_and_crop_snapshot(self, event: Event) -> bytes | None: def _read_and_crop_snapshot(self, event: Event) -> bytes | None:
"""Read, decode, and crop the snapshot image.""" """Read, decode, and crop the snapshot image."""
@ -299,9 +312,8 @@ class ObjectDescriptionProcessor(PostProcessorApi):
), ),
).start() ).start()
# Delete tracked events based on the event_id # Clean up tracked events and early request state
if event.id in self.tracked_events: self.cleanup_event(event.id)
del self.tracked_events[event.id]
def _genai_embed_description(self, event: Event, thumbnails: list[bytes]) -> None: def _genai_embed_description(self, event: Event, thumbnails: list[bytes]) -> None:
"""Embed the description for an event.""" """Embed the description for an event."""

View File

@ -522,6 +522,8 @@ class EmbeddingMaintainer(threading.Thread):
) )
elif isinstance(processor, ObjectDescriptionProcessor): elif isinstance(processor, ObjectDescriptionProcessor):
if not updated_db: if not updated_db:
# Still need to cleanup tracked events even if not processing
processor.cleanup_event(event_id)
continue continue
processor.process_data( processor.process_data(