Track down and hopefully fix some problems with memory management.

This commit is contained in:
Storm Dragon
2025-11-01 23:43:00 -04:00
parent ecbddde9cd
commit 2ae85fe25c

View File

@@ -32,6 +32,8 @@ class MpvPlayer:
self.endFileCallback = None # Callback for when file finishes self.endFileCallback = None # Callback for when file finishes
self.playlist = [] # Current playlist (for multi-file audiobooks) self.playlist = [] # Current playlist (for multi-file audiobooks)
self.currentPlaylistIndex = 0 # Current file index in playlist self.currentPlaylistIndex = 0 # Current file index in playlist
self.activeTempFiles = [] # Track temp files for cleanup
self.tempFileLock = threading.Lock() # Protect temp file list
if not HAS_MPV: if not HAS_MPV:
print("Warning: python-mpv not installed. Audio playback will not work.") print("Warning: python-mpv not installed. Audio playback will not work.")
@@ -90,21 +92,31 @@ class MpvPlayer:
if playbackSpeed is None: if playbackSpeed is None:
playbackSpeed = self.playbackSpeed playbackSpeed = self.playbackSpeed
# Track this temp file for cleanup
with self.tempFileLock:
self.activeTempFiles.append(tempFile.name)
# Load and play the temp file # Load and play the temp file
success = self.load_audio_file(tempFile.name, playbackSpeed=playbackSpeed) success = self.load_audio_file(tempFile.name, playbackSpeed=playbackSpeed)
if success: if success:
success = self.play_audio_file() success = self.play_audio_file()
# Clean up temp file after a delay (mpv needs time to load it) # Schedule cleanup after mpv loads the file
# Use shorter delay and clean up old files too
if tempFile: if tempFile:
import threading
import time import time
def cleanup_temp_file(filepath): def cleanup_temp_file(filepath):
time.sleep(5) # Wait for mpv to fully load the file time.sleep(2) # Reduced from 5s - mpv loads files quickly
try: try:
os.unlink(filepath) os.unlink(filepath)
except: except:
pass pass
# Remove from active list
with self.tempFileLock:
try:
self.activeTempFiles.remove(filepath)
except ValueError:
pass # Already removed
threading.Thread(target=cleanup_temp_file, args=(tempFile.name,), daemon=True).start() threading.Thread(target=cleanup_temp_file, args=(tempFile.name,), daemon=True).start()
return success return success
@@ -131,6 +143,8 @@ class MpvPlayer:
if self.isInitialized and self.player: if self.isInitialized and self.player:
self.player.stop() self.player.stop()
self.isPaused = False self.isPaused = False
# Immediately clean up any temp files
self._cleanup_temp_files()
def is_playing(self): def is_playing(self):
"""Check if audio is currently playing""" """Check if audio is currently playing"""
@@ -247,6 +261,19 @@ class MpvPlayer:
except Exception as e: except Exception as e:
print(f"Error setting volume: {e}") print(f"Error setting volume: {e}")
def _cleanup_temp_files(self):
"""Immediately clean up all active temp files"""
with self.tempFileLock:
for tempPath in self.activeTempFiles:
try:
if os.path.exists(tempPath):
os.unlink(tempPath)
except Exception as e:
# Ignore errors - file might already be deleted
pass
# Clear the list
self.activeTempFiles = []
def cleanup(self): def cleanup(self):
"""Cleanup resources""" """Cleanup resources"""
if self.isInitialized and self.player: if self.isInitialized and self.player:
@@ -259,6 +286,8 @@ class MpvPlayer:
except: except:
pass pass
self.isInitialized = False self.isInitialized = False
# Clean up any remaining temp files
self._cleanup_temp_files()
def is_available(self): def is_available(self):
"""Check if mpv is available""" """Check if mpv is available"""
@@ -420,6 +449,8 @@ class MpvPlayer:
except: except:
pass pass
self.isPaused = False self.isPaused = False
# Clean up temp files when stopping
self._cleanup_temp_files()
def is_audio_file_playing(self): def is_audio_file_playing(self):
"""Check if audio file is currently playing""" """Check if audio file is currently playing"""