This update is a comprehensive stability and code quality release, derived from three independent code review reports. It focuses on eliminating crash risks, memory leaks, performance bottlenecks, and security vulnerabilities without changing any UI functionality.
Critical Fixes:
- Unified
_tray_instanceArchitecture: Resolved a critical global variable conflict where_tray_instancewas defined in two places (ui.pyandui_utils.py) with different references, causingcleanup_tray_icon()andcleanup_on_exit()to silently fail. Now a single centralized reference inui_utils.pyis used, ensuring reliable tray icon and server process cleanup on application exit or crash. - Removed Fragile
sys.modulesAccess: Eliminated all race-condition-prone dynamic module lookups viasys.modules['LlamaTray.ui']incleanup_tray_icon(),cleanup_on_exit(), andAboutDialog. All components now use the centralizedui_utils._tray_instancereference directly. - Duplicate Cleanup Registration Prevented: Removed the duplicate
atexit.register(cleanup_on_exit)mechanism. Cleanup is now handled exclusively through the QtaboutToQuitsignal, preventing double-execution and potential crashes. - closeEvent NoneType Crash Prevention: Added
callable(original_close_event)check and safeQMainWindow.closeEvent(self.window, event)delegation to preventTypeError: 'NoneType' object is not callablecrashes when closing the window. - QComboBox Validator Fix: Fixed
QComboBox.setValidator()which silently fails in PyQt6. Now correctly usesself.context_size_combobox.lineEdit().setValidator(QIntValidator(512, 1000000))to properly validate context size input.
Server Process Management Hardening:
- Shell Injection Prevention: Replaced
["bash", "-c", f"lsof -ti:{port} | xargs kill -9"]with safe list-basedsubprocess.run(["lsof", "-ti", str(port)])calls, eliminating shell injection risk. - Graceful Process Termination: Port cleanup now sends
SIGTERMfirst, polls for port release with 0.5s intervals (up to 3 seconds), and only falls back toSIGKILLif the process doesn’t respond gracefully. find_llama_server()Caching: Results ofshutil.which("llama-server")are now cached at module level, eliminating redundantwhichlookups instart_server().
Performance Optimizations:
- Non-blocking CPU Monitoring: Changed
psutil.cpu_percent(interval=0.1)tointerval=0, removing the 100ms UI thread blockage that occurred every second during system monitor updates. - NVML Resource Leak Fix: Added
pynvml.nvmlShutdown()call inSystemMonitor.__del__()and a dedicated_shutdown_gpu()method, preventing NVIDIA driver resource leaks on application exit.
Desktop Environment Compatibility:
- Dynamic QT Theme Detection: Replaced hardcoded
os.environ["QT_QPA_PLATFORM_THEME"] = "kde"with dynamic detection usingXDG_CURRENT_DESKTOP. Now correctly sets the theme for KDE, GNOME, or falls back to Qt defaults for other environments (XFCE, i3, Sway, etc.).
Code Quality Cleanup:
- Removed Unused Imports: Cleaned up unused
import jsonandimport osinprofile_manager.py. - Version Bump: All version strings updated to
v1.1.3.