{"record":{"id":"456f20b3531de58a","repo":"k1tbyte/Wand-Enhancer","slug":"failed-to-kill-wemod","errorCode":null,"errorMessage":"Failed to kill WeMod","messagePattern":"Failed to kill WeMod","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"warning","filePath":"WandEnhancer/Utils/Common.cs","lineNumber":39,"sourceCode":"                foreach (var process in processes)\n                {\n                    try\n                    {\n                        process.Kill();\n                    }\n                    catch\n                    {\n                        // ignored\n                    }\n                }\n                \n                processes = Process.GetProcessesByName(processName);\n                Thread.Sleep(250);\n            }\n            \n            if (processes.Length > 0)\n            {\n                throw new Exception(\"Failed to kill WeMod\");\n            }\n        }\n\n        public static string GetCurrentDir()\n        {\n            var assemblyLocation = Assembly.GetExecutingAssembly().Location;\n            return Path.GetDirectoryName(assemblyLocation) ?? throw new InvalidOperationException();\n        }\n        \n        public static string ComputeSha256Hash(string input)\n        {\n            using (var sha256 = System.Security.Cryptography.SHA256.Create())\n            {\n                var bytes = System.Text.Encoding.UTF8.GetBytes(input);\n                var hashBytes = sha256.ComputeHash(bytes);\n                return BitConverter.ToString(hashBytes).Replace(\"-\", \"\").ToLowerInvariant();\n            }\n        }","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/k1tbyte/Wand-Enhancer/blob/643c8f8b62cbb26fd3ac105b92497d9a9c445f08/WandEnhancer/Utils/Common.cs#L21-L57","documentation":"Common.TryKillProcess loops up to 5 times (~250 ms apart, ~1.25 s total) calling Process.Kill on every instance of processName. If GetProcessesByName still returns instances after the loop, it throws. It is called from Enhancer.Patch() with _weModConfig.BrandName ('Wand' or 'WeMod').","triggerScenarios":"Enhancer.Patch() -> Common.TryKillProcess(BrandName) at Common.cs:11-41: after 5 iterations, Process.GetProcessesByName(processName).Length > 0. Each Kill() exception is swallowed, so a process that cannot be killed persists silently until the final check.","commonSituations":"Wand runs elevated but the patcher does not, so Kill() is access-denied and ignored; Wand's auto-updater/auto-start relaunches it within the retry window; the process is hung in a kernel call and ignores Kill; a watchdog respawns it.","solutions":["Close Wand fully (tray -> Quit) before patching.","Run the patcher as administrator so it can kill an elevated Wand process.","Disable Wand auto-start / background relaunch during patching.","Make the loop more robust: after Kill, call process.WaitForExit(timeout) instead of a fixed Thread.Sleep.","Check Task Manager for lingering Wand/WeMod processes and end them manually."],"exampleFix":"// before\nforeach (var process in processes) {\n    try { process.Kill(); } catch { /* ignored */ }\n}\nprocesses = Process.GetProcessesByName(processName);\nThread.Sleep(250);\n// after\nforeach (var process in processes) {\n    try { process.Kill(); process.WaitForExit(2000); }\n    catch { /* ignored */ }\n    finally { process.Dispose(); }\n}","handlingStrategy":"retry","validationCode":"// Check liveness before patching and prompt the user instead of hard-failing\nif (Process.GetProcessesByName(_weModConfig.BrandName).Length > 0) {\n    if (!CanElevate())\n        throw new InvalidOperationException(\"Wand is running elevated; relaunch the patcher as administrator.\");\n}","typeGuard":"static bool IsProcessKillable(string processName) {\n    var p = Process.GetProcessesByName(processName).FirstOrDefault();\n    if (p == null) return true;\n    try { return p.HasExited || p.Responding; } catch { return false; } finally { p.Dispose(); }\n}","tryCatchPattern":"try {\n    enhancer.Patch(); // calls TryKillProcess internally\n} catch (Exception ex) when (ex.Message == \"Failed to kill WeMod\") {\n    // ask user to quit Wand from the tray, then retry; relaunch elevated if needed\n}","preventionTips":["Ask the user to quit Wand from the tray before invoking Patch().","Run the patcher elevated when Wand is installed elevated.","Disable Wand auto-start/auto-update during patching so it does not respawn.","Replace Thread.Sleep in the kill loop with process.WaitForExit for reliable termination."],"tags":["process","permissions","kill","elevation"],"backgroundTag":null,"analyzedSha":"643c8f8b62cbb26fd3ac105b92497d9a9c445f08","analyzedAt":"2026-08-13T14:17:43.823Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}