fix: screenshot thread safety - use fresh mss context per capture

- Remove persistent mss.mss() instance from AutoScreenshot
- Use context manager 'with mss.mss() as sct:' for each capture
- Fixes '_thread._local' object has no attribute 'srcdc' error
- Allows screenshots to work from hotkey callbacks in background threads
This commit is contained in:
LemonNexus 2026-02-11 13:14:44 +00:00
parent b06b98e6cc
commit 53f7896dfa
1 changed files with 14 additions and 11 deletions

View File

@ -24,7 +24,6 @@ class AutoScreenshot:
self.screenshot_dir = screenshot_dir or Path.home() / ".lemontropia" / "screenshots"
self.screenshot_dir.mkdir(parents=True, exist_ok=True)
self.capture = mss.mss()
self.session_screenshots: List[Path] = []
# Settings
@ -41,8 +40,10 @@ class AutoScreenshot:
filepath = self.screenshot_dir / filename
try:
monitor = self.capture.monitors[1] # Primary monitor
screenshot = self.capture.grab(monitor)
# Create fresh mss context for thread safety
with mss.mss() as sct:
monitor = sct.monitors[1] # Primary monitor
screenshot = sct.grab(monitor)
img = np.array(screenshot)
img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
cv2.imwrite(str(filepath), img)
@ -63,8 +64,10 @@ class AutoScreenshot:
filepath = self.screenshot_dir / filename
try:
# Create fresh mss context for thread safety
with mss.mss() as sct:
monitor = {"left": x, "top": y, "width": w, "height": h}
screenshot = self.capture.grab(monitor)
screenshot = sct.grab(monitor)
img = np.array(screenshot)
img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
cv2.imwrite(str(filepath), img)