From d35e33610a8ca62af3b0c343403d5226ed870d7c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 6 Oct 2020 13:12:47 -0500 Subject: [PATCH] Resolve memory leak in recorder (#41349) Avoids a build up of the InstanceState. --- homeassistant/components/recorder/__init__.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/recorder/__init__.py b/homeassistant/components/recorder/__init__.py index 5ecfad5d204..8e6a251f660 100644 --- a/homeassistant/components/recorder/__init__.py +++ b/homeassistant/components/recorder/__init__.py @@ -240,6 +240,7 @@ class Recorder(threading.Thread): self._timechanges_seen = 0 self._keepalive_count = 0 self._old_states = {} + self._pending_expunge = [] self.event_session = None self.get_session = None self._completed_database_setup = False @@ -404,6 +405,7 @@ class Recorder(threading.Thread): self.event_session.add(dbstate) if has_new_state: self._old_states[dbstate.entity_id] = dbstate + self._pending_expunge.append(dbstate) except (TypeError, ValueError): _LOGGER.warning( "State is not JSON serializable: %s", @@ -489,6 +491,12 @@ class Recorder(threading.Thread): def _commit_event_session(self): try: + self.event_session.flush() + for dbstate in self._pending_expunge: + # Expunge the state so its not expired + # until we use it later for dbstate.old_state + self.event_session.expunge(dbstate) + self._pending_expunge = [] self.event_session.commit() except Exception as err: _LOGGER.error("Error executing query: %s", err) @@ -574,9 +582,7 @@ class Recorder(threading.Thread): sqlalchemy_event.listen(self.engine, "connect", setup_recorder_connection) Base.metadata.create_all(self.engine) - self.get_session = scoped_session( - sessionmaker(bind=self.engine, expire_on_commit=False) - ) + self.get_session = scoped_session(sessionmaker(bind=self.engine)) def _close_connection(self): """Close the connection."""