using System; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using ClassNotify.Service.Ipc; using ClassNotify.Service.Services; using ClassNotify.Shared.Enums; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace ClassNotify.Service.Workers; public class ManagedClientInfo { public int ProcessId { get; set; } public uint SessionId { get; set; } public string LaunchToken { get; set; } = string.Empty; public DateTime LaunchTimeUtc { get; set; } = DateTime.UtcNow; } public class ClientWatchdogWorker : BackgroundService { private readonly ILogger _logger; private readonly NamedPipeServer _pipeServer; private readonly IConfiguration _configuration; private readonly SemaphoreSlim _recoveryLock = new(1, 1); [DllImport("kernel32.dll", SetLastError = true)] private static extern uint WTSGetActiveConsoleSessionId(); [DllImport("wtsapi32.dll", SetLastError = true)] private static extern bool WTSQueryUserToken(uint sessionId, out IntPtr phToken); [DllImport("kernel32.dll", SetLastError = true)] private static extern bool CloseHandle(IntPtr hObject); private ManagedClientInfo? _managedClient; private readonly object _managedClientLock = new(); private int _failureCount = 0; private int _recoveryFailureCount = 0; private DateTime _nextRecoveryAllowedUtc = DateTime.MinValue; private const int HungThreshold = 3; private readonly TimeSpan _checkInterval = TimeSpan.FromSeconds(3); private readonly TimeSpan _heartbeatTimeout = TimeSpan.FromSeconds(15); private readonly TimeSpan _startupGrace = TimeSpan.FromSeconds(15); private readonly DeviceCredentialsManager _credentialsManager; public ClientWatchdogWorker( ILogger logger, NamedPipeServer pipeServer, IConfiguration configuration, DeviceCredentialsManager credentialsManager) { _logger = logger; _pipeServer = pipeServer; _configuration = configuration; _credentialsManager = credentialsManager; // Wire Bootstrap pipe requests from relay launcher _pipeServer.OnBootstrapRequested += (sessionId, showUi) => { _logger.LogInformation("[ClientWatchdog] Bootstrap requested for SessionId={SessionId}, ShowUi={ShowUi}", sessionId, showUi); var intent = showUi ? ClientLaunchIntent.UserInteractive : ClientLaunchIntent.BackgroundRecovery; _ = Task.Run(async () => { await EnsureClientRunningAsync(sessionId, intent, CancellationToken.None); }); }; } private TimeSpan GetRecoveryBackoff() { return _recoveryFailureCount switch { 1 => TimeSpan.FromSeconds(5), 2 => TimeSpan.FromSeconds(10), _ => TimeSpan.FromSeconds(30) }; } private void RecordRecoveryFailure() { _recoveryFailureCount++; var backoff = GetRecoveryBackoff(); _nextRecoveryAllowedUtc = DateTime.UtcNow + backoff; _logger.LogWarning("[ClientWatchdog] Recovery failed #{Count}, next attempt in {Seconds}s", _recoveryFailureCount, (int)backoff.TotalSeconds); } private void RecordRecoverySuccess() { _recoveryFailureCount = 0; _nextRecoveryAllowedUtc = DateTime.MinValue; _failureCount = 0; _logger.LogInformation("[ClientWatchdog] Pong received, Client healthy."); } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { _logger.LogInformation("ClientWatchdogWorker started. Initializing Client lifecycle monitoring..."); // Initial delay to give client time to start during system boot/logon await Task.Delay(3000, stoppingToken); while (!stoppingToken.IsCancellationRequested) { try { await MonitorClientLifecycleAsync(stoppingToken); } catch (OperationCanceledException) { break; } catch (Exception ex) { _logger.LogError(ex, "Error in ClientWatchdogWorker cycle."); } await Task.Delay(_checkInterval, stoppingToken); } _logger.LogInformation("ClientWatchdogWorker stopped."); } private async Task MonitorClientLifecycleAsync(CancellationToken stoppingToken) { // 1. Check for active interactive user session if (!HasActiveUserSession(out uint activeSessionId)) { _logger.LogInformation("[ClientWatchdog] No active interactive user session detected. Standing by."); return; } // Ensure bootstrap pipe is also active for this session _pipeServer.EnsureBootstrapPipeRunning(activeSessionId, stoppingToken); // 2. Check current ManagedClient state ManagedClientInfo? currentManaged; lock (_managedClientLock) { currentManaged = _managedClient; } if (currentManaged == null) { _logger.LogInformation("[ClientWatchdog] No managed client running in Session {SessionId}. Recovering in background (BackgroundRecovery)...", activeSessionId); _failureCount = 0; await EnsureClientRunningAsync(activeSessionId, ClientLaunchIntent.BackgroundRecovery, stoppingToken); return; } // Check if managed client process has exited bool isProcessAlive = false; try { var proc = Process.GetProcessById(currentManaged.ProcessId); isProcessAlive = !proc.HasExited; } catch { isProcessAlive = false; } if (!isProcessAlive) { _logger.LogWarning("[ClientWatchdog] ManagedClient PID={Pid} has exited. Resetting state and recovering in background...", currentManaged.ProcessId); lock (_managedClientLock) { _managedClient = null; } _pipeServer.InvalidateAuthorization(); _failureCount = 0; await EnsureClientRunningAsync(activeSessionId, ClientLaunchIntent.BackgroundRecovery, stoppingToken); return; } // 3. Turn 48 Architecture Invariant: 15s StartupGrace Period // During the first 15 seconds after launch, never kill or flag the process as hung! var startupAge = DateTime.UtcNow - currentManaged.LaunchTimeUtc; if (startupAge < _startupGrace) { _logger.LogDebug("[ClientWatchdog] ManagedClient PID={Pid} within StartupGrace ({Elapsed}s < 15s). Skipping hung check.", currentManaged.ProcessId, (int)startupAge.TotalSeconds); return; } // 4. Check IPC connection & ping-pong heartbeat bool isConnected = _pipeServer.IsClientConnected; var heartbeatAge = DateTimeOffset.UtcNow - _pipeServer.LastClientHeartbeatUtc; if (isConnected) { await _pipeServer.SendPingAsync(); if (heartbeatAge < _heartbeatTimeout) { if (_failureCount > 0 || _recoveryFailureCount > 0) { RecordRecoverySuccess(); } return; } } // 5. Client disconnected or heartbeat timed out if (DateTime.UtcNow < _nextRecoveryAllowedUtc) { _logger.LogDebug("[ClientWatchdog] Recovery in backoff until {AllowedUtc}. Skipping this cycle.", _nextRecoveryAllowedUtc); return; } _failureCount++; if (_failureCount < HungThreshold) { _logger.LogWarning("[ClientWatchdog] Heartbeat timeout {Count}/{Threshold} for ManagedClient PID={Pid}", _failureCount, HungThreshold, currentManaged.ProcessId); } else { _logger.LogWarning("[ClientWatchdog] Consecutive timeouts reached threshold. Terminating hung ManagedClient PID={Pid}...", currentManaged.ProcessId); await TerminateAndRecoverAsync(currentManaged, activeSessionId, stoppingToken); } } public async Task EnsureClientRunningAsync(uint activeSessionId, ClientLaunchIntent intent, CancellationToken stoppingToken) { if (!await _recoveryLock.WaitAsync(0, stoppingToken)) { return false; } try { lock (_managedClientLock) { if (_managedClient != null) { try { var proc = Process.GetProcessById(_managedClient.ProcessId); if (!proc.HasExited) { _logger.LogInformation("[ClientWatchdog] ManagedClient PID={Pid} is already active.", _managedClient.ProcessId); return true; } } catch { } _managedClient = null; } } var clientExe = FindClientExecutable(); if (string.IsNullOrEmpty(clientExe) || !File.Exists(clientExe)) { _logger.LogError("[ClientWatchdog] ClassNotify.Client.exe not found at path: {Path}", clientExe); RecordRecoveryFailure(); return false; } _pipeServer.InvalidateAuthorization(); string launchToken = Guid.NewGuid().ToString("N"); string? extraArgs = intent switch { ClientLaunchIntent.UserInteractive => "--user-interactive --show", ClientLaunchIntent.InstallerFirstRun => "--first-run --show", _ => null // BackgroundRecovery: strictly no foreground parameters! Zero Disturbance Invariant. }; _logger.LogInformation("[ClientWatchdog] Launching authorized client: SessionId={SessionId}, Intent={Intent}...", activeSessionId, intent); var (launched, pid, sessId, token) = SessionLauncher.LaunchProcessInActiveUserSession(clientExe, launchToken, _logger, extraArgs); if (!launched || pid <= 0 || string.IsNullOrEmpty(token)) { _logger.LogError("[ClientWatchdog] SessionLauncher failed to launch client in Session {SessionId}.", activeSessionId); RecordRecoveryFailure(); return false; } var info = new ManagedClientInfo { ProcessId = pid, SessionId = sessId, LaunchToken = token, LaunchTimeUtc = DateTime.UtcNow }; lock (_managedClientLock) { _managedClient = info; } _pipeServer.SetAuthorizedClient(token, pid, sessId); _logger.LogInformation("[ClientWatchdog] Registered new ManagedClient PID={Pid}, SessionId={SessionId}.", pid, sessId); // Wait for pipe handshake (up to 15s) for (int i = 0; i < 30; i++) { await Task.Delay(500, stoppingToken); if (_pipeServer.IsClientConnected) { await _pipeServer.SendPingAsync(); await Task.Delay(500, stoppingToken); var age = DateTimeOffset.UtcNow - _pipeServer.LastClientHeartbeatUtc; if (age < TimeSpan.FromSeconds(5)) { RecordRecoverySuccess(); return true; } } } _logger.LogWarning("[ClientWatchdog] Client launched (PID={Pid}) but initial handshake not completed within 15s.", pid); return true; } finally { _recoveryLock.Release(); } } private async Task TerminateAndRecoverAsync(ManagedClientInfo hungClient, uint activeSessionId, CancellationToken stoppingToken) { if (!await _recoveryLock.WaitAsync(0, stoppingToken)) { return; } try { _pipeServer.InvalidateAuthorization(); // Strict Turn 48 Invariant: Only kill the exact registered ManagedClient PID try { var proc = Process.GetProcessById(hungClient.ProcessId); if (!proc.HasExited) { _logger.LogWarning("[ClientWatchdog] Killing hung ManagedClient PID={Pid}...", hungClient.ProcessId); proc.Kill(); proc.WaitForExit(3000); } } catch (Exception ex) { _logger.LogWarning("[ClientWatchdog] Error killing ManagedClient PID={Pid}: {Message}", hungClient.ProcessId, ex.Message); } lock (_managedClientLock) { _managedClient = null; } _failureCount = 0; RecordRecoveryFailure(); } finally { _recoveryLock.Release(); } } private bool HasActiveUserSession(out uint activeSessionId) { activeSessionId = WTSGetActiveConsoleSessionId(); if (activeSessionId != 0xFFFFFFFF && WTSQueryUserToken(activeSessionId, out IntPtr consoleToken)) { CloseHandle(consoleToken); return true; } for (uint s = 1; s <= 32; s++) { if (WTSQueryUserToken(s, out IntPtr userToken)) { CloseHandle(userToken); activeSessionId = s; return true; } } activeSessionId = 0xFFFFFFFF; return false; } private string? FindClientExecutable() { var configuredPath = _configuration["ClassNotify:ClientExePath"]; if (!string.IsNullOrEmpty(configuredPath) && File.Exists(configuredPath)) { return configuredPath; } var progFiles = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "ClassNotify", "Client", "ClassNotify.Client.exe"); if (File.Exists(progFiles)) { return progFiles; } var baseDir = AppContext.BaseDirectory; var relativeToService = Path.GetFullPath(Path.Combine(baseDir, @"..\Client\ClassNotify.Client.exe")); if (File.Exists(relativeToService)) { return relativeToService; } var devPaths = new[] { Path.GetFullPath(Path.Combine(baseDir, @"..\Client\ClassNotify.Client.exe")), Path.GetFullPath(Path.Combine(baseDir, @"..\..\..\..\..\ClassNotify.Client\bin\Debug\net8.0-windows\ClassNotify.Client.exe")), Path.GetFullPath(Path.Combine(baseDir, @"..\..\..\..\..\ClassNotify.Client\bin\Release\net8.0-windows\win-x64\ClassNotify.Client.exe")), Path.GetFullPath(Path.Combine(baseDir, @"..\ClassNotify.Client\ClassNotify.Client.exe")), Path.GetFullPath(Path.Combine(baseDir, @"ClassNotify.Client.exe")) }; foreach (var p in devPaths) { if (File.Exists(p)) return p; } return null; } }