fix(sandbox): prevent memory leak in file operation locks using WeakValueDictionary (#2096)

* fix(sandbox): prevent memory leak in file operation locks using WeakValueDictionary

* lint: fix lint issue in sandbox tools security
This commit is contained in:
Jin
2026-04-10 22:55:53 +08:00
committed by GitHub
parent 679ca657ee
commit 718dddde75
2 changed files with 41 additions and 1 deletions
@@ -1,8 +1,12 @@
import threading
import weakref
from deerflow.sandbox.sandbox import Sandbox
_FILE_OPERATION_LOCKS: dict[tuple[str, str], threading.Lock] = {}
# Use WeakValueDictionary to prevent memory leak in long-running processes.
# Locks are automatically removed when no longer referenced by any thread.
_LockKey = tuple[str, str]
_FILE_OPERATION_LOCKS: weakref.WeakValueDictionary[_LockKey, threading.Lock] = weakref.WeakValueDictionary()
_FILE_OPERATION_LOCKS_GUARD = threading.Lock()