Skip to content

Commit 2185748

Browse files
author
matdev83
committed
fix: Address memory leaks
- Fix a potential memory leak in secure_state_service.py by ensuring the deletion of frame objects created by inspect.currentframe(). - Correct a logic error in in_memory_session_repository.py to prevent memory leaks from empty user session lists.
1 parent 911eb8c commit 2185748

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

data/test_suite_state.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"test_count": 5046,
3-
"last_updated": "1762217835.0"
2+
"test_count": 5119,
3+
"last_updated": "1762168167.0802596"
44
}

src/core/repositories/in_memory_session_repository.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ async def update(self, entity: Session) -> Session:
7777
tracked_sessions = self._user_sessions.get(previous_user_id)
7878
if tracked_sessions and entity.id in tracked_sessions:
7979
tracked_sessions.remove(entity.id)
80-
if not tracked_sessions:
81-
del self._user_sessions[previous_user_id]
80+
if not self._user_sessions[previous_user_id]:
81+
del self._user_sessions[previous_user_id]
8282

8383
if new_user_id:
8484
tracked_sessions = self._user_sessions.setdefault(new_user_id, [])

src/core/services/secure_state_service.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,18 @@ def __getattr__(self, name: str) -> Any:
209209
import inspect
210210

211211
frame = inspect.currentframe()
212-
if frame and frame.f_back:
213-
caller_locals = frame.f_back.f_locals
214-
215-
# Check if 'self' in caller is an instance of allowed interfaces
216-
caller_self = caller_locals.get("self")
217-
if caller_self:
218-
for interface in self._allowed_interfaces:
219-
if isinstance(caller_self, interface):
220-
return getattr(self._target, name)
212+
try:
213+
if frame and frame.f_back:
214+
caller_locals = frame.f_back.f_locals
215+
216+
# Check if 'self' in caller is an instance of allowed interfaces
217+
caller_self = caller_locals.get("self")
218+
if caller_self:
219+
for interface in self._allowed_interfaces:
220+
if isinstance(caller_self, interface):
221+
return getattr(self._target, name)
222+
finally:
223+
del frame
221224

222225
# If we get here, the access is not through an allowed interface
223226
raise StateAccessViolationError(
@@ -245,15 +248,18 @@ def __setattr__(self, name: str, value: Any) -> None:
245248
import inspect
246249

247250
frame = inspect.currentframe()
248-
if frame and frame.f_back:
249-
caller_locals = frame.f_back.f_locals
250-
caller_self = caller_locals.get("self")
251-
252-
if caller_self:
253-
for interface in self._allowed_interfaces:
254-
if isinstance(caller_self, interface):
255-
setattr(self._target, name, value)
256-
return
251+
try:
252+
if frame and frame.f_back:
253+
caller_locals = frame.f_back.f_locals
254+
caller_self = caller_locals.get("self")
255+
256+
if caller_self:
257+
for interface in self._allowed_interfaces:
258+
if isinstance(caller_self, interface):
259+
setattr(self._target, name, value)
260+
return
261+
finally:
262+
del frame
257263

258264
raise StateAccessViolationError(
259265
f"Direct setting of '{name}' is not allowed. "

0 commit comments

Comments
 (0)