398 lines
14 KiB
Python
398 lines
14 KiB
Python
"""
|
|
Windows-spezifische Fixes für Thread- und Socket-Probleme
|
|
Behebt bekannte Issues mit Flask Auto-Reload auf Windows.
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import signal
|
|
import threading
|
|
import time
|
|
import atexit
|
|
from typing import List, Callable
|
|
from utils.logging_config import get_logger
|
|
|
|
# Logger für Windows-Fixes
|
|
windows_logger = get_logger("windows_fixes")
|
|
|
|
# Exportierte Funktionen
|
|
__all__ = [
|
|
'WindowsThreadManager',
|
|
'get_windows_thread_manager',
|
|
'fix_windows_socket_issues',
|
|
'apply_safe_socket_options',
|
|
'setup_windows_environment',
|
|
'is_flask_reloader_process',
|
|
'apply_all_windows_fixes',
|
|
'safe_subprocess_run',
|
|
'patch_subprocess',
|
|
'apply_global_subprocess_patch',
|
|
'apply_encoding_fixes',
|
|
'apply_threading_fixes',
|
|
'apply_signal_fixes'
|
|
]
|
|
|
|
# Globale Flags um doppelte Anwendung zu verhindern
|
|
_windows_fixes_applied = False
|
|
_socket_patches_applied = False
|
|
|
|
class WindowsThreadManager:
|
|
"""
|
|
Verwaltet Threads und deren ordnungsgemäße Beendigung auf Windows.
|
|
Behebt Socket-Fehler beim Flask Auto-Reload.
|
|
"""
|
|
|
|
def __init__(self):
|
|
self.managed_threads: List[threading.Thread] = []
|
|
self.cleanup_functions: List[Callable] = []
|
|
self.shutdown_event = threading.Event()
|
|
self._lock = threading.Lock()
|
|
self._is_shutting_down = False
|
|
|
|
# Signal-Handler nur auf Windows registrieren
|
|
if os.name == 'nt':
|
|
self._register_signal_handlers()
|
|
|
|
def _register_signal_handlers(self):
|
|
"""Registriert Windows-spezifische Signal-Handler."""
|
|
try:
|
|
signal.signal(signal.SIGINT, self._signal_handler)
|
|
signal.signal(signal.SIGTERM, self._signal_handler)
|
|
# Windows-spezifisches SIGBREAK
|
|
if hasattr(signal, 'SIGBREAK'):
|
|
signal.signal(signal.SIGBREAK, self._signal_handler)
|
|
windows_logger.debug("✅ Windows Signal-Handler registriert")
|
|
except Exception as e:
|
|
windows_logger.warning(f"⚠️ Signal-Handler konnten nicht registriert werden: {str(e)}")
|
|
|
|
def _signal_handler(self, sig, frame):
|
|
"""Signal-Handler für ordnungsgemäßes Shutdown."""
|
|
if not self._is_shutting_down:
|
|
windows_logger.warning(f"🛑 Windows Signal {sig} empfangen - initiiere Shutdown")
|
|
self.shutdown_all()
|
|
|
|
def register_thread(self, thread: threading.Thread):
|
|
"""Registriert einen Thread für ordnungsgemäße Beendigung."""
|
|
with self._lock:
|
|
if thread not in self.managed_threads:
|
|
self.managed_threads.append(thread)
|
|
windows_logger.debug(f"📝 Thread {thread.name} registriert")
|
|
|
|
def register_cleanup_function(self, func: Callable):
|
|
"""Registriert eine Cleanup-Funktion."""
|
|
with self._lock:
|
|
if func not in self.cleanup_functions:
|
|
self.cleanup_functions.append(func)
|
|
windows_logger.debug(f"📝 Cleanup-Funktion registriert")
|
|
|
|
def shutdown_all(self):
|
|
"""Beendet alle verwalteten Threads und führt Cleanup durch."""
|
|
if self._is_shutting_down:
|
|
return
|
|
|
|
with self._lock:
|
|
self._is_shutting_down = True
|
|
windows_logger.info("🔄 Starte Windows Thread-Shutdown...")
|
|
|
|
# Shutdown-Event setzen
|
|
self.shutdown_event.set()
|
|
|
|
# Cleanup-Funktionen ausführen
|
|
for func in self.cleanup_functions:
|
|
try:
|
|
windows_logger.debug(f"🧹 Führe Cleanup-Funktion aus: {func.__name__}")
|
|
func()
|
|
except Exception as e:
|
|
windows_logger.error(f"❌ Fehler bei Cleanup-Funktion {func.__name__}: {str(e)}")
|
|
|
|
# Threads beenden
|
|
active_threads = [t for t in self.managed_threads if t.is_alive()]
|
|
if active_threads:
|
|
windows_logger.info(f"⏳ Warte auf {len(active_threads)} aktive Threads...")
|
|
|
|
for thread in active_threads:
|
|
try:
|
|
windows_logger.debug(f"🔄 Beende Thread: {thread.name}")
|
|
thread.join(timeout=5)
|
|
|
|
if thread.is_alive():
|
|
windows_logger.warning(f"⚠️ Thread {thread.name} konnte nicht ordnungsgemäß beendet werden")
|
|
else:
|
|
windows_logger.debug(f"✅ Thread {thread.name} erfolgreich beendet")
|
|
except Exception as e:
|
|
windows_logger.error(f"❌ Fehler beim Beenden von Thread {thread.name}: {str(e)}")
|
|
|
|
windows_logger.info("✅ Windows Thread-Shutdown abgeschlossen")
|
|
|
|
# Globale Instanz
|
|
_windows_thread_manager = None
|
|
|
|
def get_windows_thread_manager() -> WindowsThreadManager:
|
|
"""Gibt die globale Instanz des Windows Thread-Managers zurück."""
|
|
global _windows_thread_manager
|
|
if _windows_thread_manager is None:
|
|
_windows_thread_manager = WindowsThreadManager()
|
|
return _windows_thread_manager
|
|
|
|
def fix_windows_socket_issues():
|
|
"""
|
|
Anwendung von Windows-spezifischen Socket-Fixes.
|
|
Vereinfachte, sichere Version ohne Monkey-Patching.
|
|
"""
|
|
global _socket_patches_applied
|
|
|
|
if os.name != 'nt':
|
|
return
|
|
|
|
if _socket_patches_applied:
|
|
windows_logger.debug("⏭️ Socket-Patches bereits angewendet")
|
|
return
|
|
|
|
try:
|
|
# SICHERERE Alternative: Nur TCP Socket-Optionen setzen ohne Monkey-Patching
|
|
import socket
|
|
|
|
# Erweitere die Socket-Klasse mit einer Hilfsmethode
|
|
if not hasattr(socket.socket, 'windows_bind_with_reuse'):
|
|
|
|
def windows_bind_with_reuse(self, address):
|
|
"""Windows-optimierte bind-Methode mit SO_REUSEADDR."""
|
|
try:
|
|
# SO_REUSEADDR aktivieren
|
|
self.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
windows_logger.debug(f"SO_REUSEADDR aktiviert für Socket {address}")
|
|
except Exception as e:
|
|
windows_logger.debug(f"SO_REUSEADDR konnte nicht gesetzt werden: {str(e)}")
|
|
|
|
# Standard-bind ausführen
|
|
return self.bind(address)
|
|
|
|
# Füge die Hilfsmethode hinzu ohne die ursprüngliche bind-Methode zu überschreiben
|
|
socket.socket.windows_bind_with_reuse = windows_bind_with_reuse
|
|
|
|
# Setze globale Socket-Optionen für bessere Windows-Kompatibilität
|
|
socket.setdefaulttimeout(30) # 30 Sekunden Standard-Timeout
|
|
|
|
_socket_patches_applied = True
|
|
windows_logger.debug("✅ Windows Socket-Optimierungen angewendet (sicher)")
|
|
|
|
except Exception as e:
|
|
windows_logger.warning(f"⚠️ Socket-Optimierungen konnten nicht angewendet werden: {str(e)}")
|
|
|
|
def apply_safe_socket_options():
|
|
"""
|
|
Wendet sichere Socket-Optionen für Windows an ohne Monkey-Patching.
|
|
"""
|
|
if os.name != 'nt':
|
|
return
|
|
|
|
try:
|
|
import socket
|
|
|
|
# Sichere Socket-Defaults für Windows
|
|
if hasattr(socket, 'TCP_NODELAY'):
|
|
# TCP_NODELAY als Standard aktivieren für bessere Performance
|
|
pass # Wird pro Socket gesetzt, nicht global
|
|
|
|
windows_logger.debug("✅ Sichere Socket-Optionen angewendet")
|
|
|
|
except Exception as e:
|
|
windows_logger.debug(f"Socket-Optionen konnten nicht gesetzt werden: {str(e)}")
|
|
|
|
def setup_windows_environment():
|
|
"""
|
|
Richtet die Windows-Umgebung für bessere Flask-Kompatibilität ein.
|
|
"""
|
|
if os.name != 'nt':
|
|
return
|
|
|
|
try:
|
|
# Umgebungsvariablen für bessere Windows-Kompatibilität
|
|
os.environ['PYTHONIOENCODING'] = 'utf-8'
|
|
os.environ['PYTHONUTF8'] = '1'
|
|
|
|
windows_logger.debug("✅ Windows-Umgebung optimiert")
|
|
|
|
except Exception as e:
|
|
windows_logger.warning(f"⚠️ Windows-Umgebung konnte nicht optimiert werden: {str(e)}")
|
|
|
|
def is_flask_reloader_process() -> bool:
|
|
"""
|
|
Prüft, ob der aktuelle Prozess der Flask-Reloader-Prozess ist.
|
|
"""
|
|
return os.environ.get('WERKZEUG_RUN_MAIN') != 'true'
|
|
|
|
# ===== ENCODING-FIXES =====
|
|
|
|
def apply_encoding_fixes():
|
|
"""Wendet Windows-spezifische Encoding-Fixes an."""
|
|
try:
|
|
# Umgebungsvariablen für bessere Windows-Kompatibilität
|
|
os.environ['PYTHONIOENCODING'] = 'utf-8'
|
|
os.environ['PYTHONUTF8'] = '1'
|
|
|
|
windows_logger.debug("✅ Windows-Encoding-Fixes angewendet")
|
|
|
|
except Exception as e:
|
|
windows_logger.warning(f"⚠️ Encoding-Fixes konnten nicht angewendet werden: {str(e)}")
|
|
|
|
# ===== THREADING-FIXES =====
|
|
|
|
def apply_threading_fixes():
|
|
"""Wendet Windows-spezifische Threading-Fixes an."""
|
|
try:
|
|
# Thread-Manager initialisieren
|
|
get_windows_thread_manager()
|
|
|
|
# Socket-Fixes anwenden
|
|
fix_windows_socket_issues()
|
|
apply_safe_socket_options()
|
|
|
|
windows_logger.debug("✅ Windows-Threading-Fixes angewendet")
|
|
|
|
except Exception as e:
|
|
windows_logger.warning(f"⚠️ Threading-Fixes konnten nicht angewendet werden: {str(e)}")
|
|
|
|
# ===== SIGNAL-FIXES =====
|
|
|
|
def apply_signal_fixes():
|
|
"""Wendet Windows-spezifische Signal-Handler-Fixes an."""
|
|
try:
|
|
# Signal-Handler werden bereits im WindowsThreadManager registriert
|
|
windows_logger.debug("✅ Windows-Signal-Fixes angewendet")
|
|
|
|
except Exception as e:
|
|
windows_logger.warning(f"⚠️ Signal-Fixes konnten nicht angewendet werden: {str(e)}")
|
|
|
|
# ===== SICHERE SUBPROCESS-WRAPPER =====
|
|
|
|
def safe_subprocess_run(*args, **kwargs):
|
|
"""
|
|
Sicherer subprocess.run Wrapper für Windows mit UTF-8 Encoding.
|
|
Verhindert charmap-Fehler durch explizite Encoding-Einstellungen.
|
|
"""
|
|
import subprocess
|
|
|
|
# Standard-Encoding für Windows setzen
|
|
if 'encoding' not in kwargs and kwargs.get('text', False):
|
|
kwargs['encoding'] = 'utf-8'
|
|
kwargs['errors'] = 'replace'
|
|
|
|
# Timeout-Standard setzen falls nicht vorhanden
|
|
if 'timeout' not in kwargs:
|
|
kwargs['timeout'] = 30
|
|
|
|
try:
|
|
return subprocess.run(*args, **kwargs)
|
|
except subprocess.TimeoutExpired as e:
|
|
windows_logger.warning(f"Subprocess-Timeout nach {kwargs.get('timeout', 30)}s: {' '.join(args[0]) if args and isinstance(args[0], list) else str(args)}")
|
|
raise e
|
|
except UnicodeDecodeError as e:
|
|
windows_logger.error(f"Unicode-Decode-Fehler in subprocess: {str(e)}")
|
|
# Fallback ohne text=True
|
|
kwargs_fallback = kwargs.copy()
|
|
kwargs_fallback.pop('text', None)
|
|
kwargs_fallback.pop('encoding', None)
|
|
kwargs_fallback.pop('errors', None)
|
|
return subprocess.run(*args, **kwargs_fallback)
|
|
except Exception as e:
|
|
windows_logger.error(f"Subprocess-Fehler: {str(e)}")
|
|
raise e
|
|
|
|
# ===== SUBPROCESS-MONKEY-PATCH =====
|
|
|
|
def patch_subprocess():
|
|
"""
|
|
Patcht subprocess.run und subprocess.Popen um automatisch sichere Encoding-Einstellungen zu verwenden.
|
|
"""
|
|
import subprocess
|
|
|
|
# Original-Funktionen speichern
|
|
if not hasattr(subprocess, '_original_run'):
|
|
subprocess._original_run = subprocess.run
|
|
subprocess._original_popen = subprocess.Popen
|
|
|
|
def patched_run(*args, **kwargs):
|
|
# Automatisch UTF-8 Encoding für text=True setzen
|
|
if kwargs.get('text', False) and 'encoding' not in kwargs:
|
|
kwargs['encoding'] = 'utf-8'
|
|
kwargs['errors'] = 'replace'
|
|
|
|
return subprocess._original_run(*args, **kwargs)
|
|
|
|
def patched_popen(*args, **kwargs):
|
|
# Automatisch UTF-8 Encoding für text=True setzen
|
|
if kwargs.get('text', False) and 'encoding' not in kwargs:
|
|
kwargs['encoding'] = 'utf-8'
|
|
kwargs['errors'] = 'replace'
|
|
|
|
# Auch für universal_newlines (ältere Python-Versionen)
|
|
if kwargs.get('universal_newlines', False) and 'encoding' not in kwargs:
|
|
kwargs['encoding'] = 'utf-8'
|
|
kwargs['errors'] = 'replace'
|
|
|
|
return subprocess._original_popen(*args, **kwargs)
|
|
|
|
subprocess.run = patched_run
|
|
subprocess.Popen = patched_popen
|
|
windows_logger.info("✅ Subprocess automatisch gepatcht für UTF-8 Encoding (run + Popen)")
|
|
|
|
# ===== GLOBALER SUBPROCESS-PATCH =====
|
|
|
|
def apply_global_subprocess_patch():
|
|
"""
|
|
Wendet den subprocess-Patch global an, auch für bereits importierte Module.
|
|
"""
|
|
import sys
|
|
import subprocess
|
|
|
|
# Patch subprocess direkt
|
|
patch_subprocess()
|
|
|
|
# Patch auch in bereits importierten Modulen
|
|
for module_name, module in sys.modules.items():
|
|
if hasattr(module, 'subprocess') and module.subprocess is subprocess:
|
|
# Modul verwendet subprocess - patch es
|
|
module.subprocess = subprocess
|
|
windows_logger.debug(f"✅ Subprocess in Modul {module_name} gepatcht")
|
|
|
|
windows_logger.info("✅ Globaler subprocess-Patch angewendet")
|
|
|
|
def apply_all_windows_fixes():
|
|
"""Wendet alle Windows-spezifischen Fixes an."""
|
|
global _windows_fixes_applied
|
|
|
|
if _windows_fixes_applied:
|
|
return
|
|
|
|
try:
|
|
windows_logger.info("🔧 Wende Windows-spezifische Fixes an...")
|
|
|
|
# 1. Encoding-Fixes
|
|
apply_encoding_fixes()
|
|
|
|
# 2. Threading-Fixes
|
|
apply_threading_fixes()
|
|
|
|
# 3. Signal-Handler-Fixes
|
|
apply_signal_fixes()
|
|
|
|
# 4. Subprocess-Patch für UTF-8 Encoding
|
|
patch_subprocess()
|
|
|
|
# 5. Globaler Subprocess-Patch für bereits importierte Module
|
|
apply_global_subprocess_patch()
|
|
|
|
_windows_fixes_applied = True
|
|
windows_logger.info("✅ Alle Windows-Fixes erfolgreich angewendet")
|
|
|
|
except Exception as e:
|
|
windows_logger.error(f"❌ Fehler beim Anwenden der Windows-Fixes: {str(e)}")
|
|
raise e
|
|
|
|
# Automatisch Windows-Fixes beim Import anwenden (nur einmal)
|
|
if os.name == 'nt' and not _windows_fixes_applied:
|
|
try:
|
|
apply_all_windows_fixes()
|
|
except Exception as e:
|
|
windows_logger.warning(f"⚠️ Windows-Fixes konnten nicht automatisch angewendet werden: {str(e)}") |