{"record":{"id":"ebf98b2b6312312d","repo":"SubtitleEdit/subtitleedit","slug":"llama-server-exited-during-startup-code-process","errorCode":null,"errorMessage":"llama-server exited during startup (code {process.ExitCode}). Output: {tail}","messagePattern":"llama-server exited during startup \\(code (.+?)\\)\\. Output: (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/libuilogic/LlamaCpp/LlamaCppServerManager.cs","lineNumber":616,"sourceCode":"            process.BeginOutputReadLine();\n\n            _serverProcess = process;\n            _serverPort = port;\n            _serverModelPath = modelPath;\n            _serverContextSize = contextSize;\n            HookProcessExitOnce();\n\n            var deadline = DateTime.UtcNow.AddMinutes(5);\n            while (DateTime.UtcNow < deadline)\n            {\n                cancellationToken.ThrowIfCancellationRequested();\n                if (process.HasExited)\n                {\n                    var tail = SnapshotServerLog();\n                    _serverProcess = null;\n                    _serverPort = 0;\n                    _serverModelPath = null;\n                    throw new InvalidOperationException(\n                        $\"llama-server exited during startup (code {process.ExitCode}). Output: {tail}\");\n                }\n\n                if (await ProbeHealthAsync(port, TimeSpan.FromSeconds(2), cancellationToken))\n                {\n                    Configuration.Settings.Tools.LlamaCppApiUrl = ApiUrl;\n                    return;\n                }\n\n                await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);\n            }\n\n            var lastOutput = SnapshotServerLog();\n            StopServerInternal();\n            throw new TimeoutException(\n                $\"llama-server did not report healthy within 5 minutes. Last output: {lastOutput}\");\n        }\n        finally","sourceCodeStart":598,"sourceCodeEnd":634,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/libuilogic/LlamaCpp/LlamaCppServerManager.cs#L598-L634","documentation":"InvalidOperationException thrown inside the startup-health loop when the llama-server process exits before its /health endpoint answers. The message embeds the process exit code and the tail of captured server output, so the underlying crash reason is in the exception text. Reaching it means the binary launched but crashed during model load or context init.","triggerScenarios":"llama-server exits non-zero within the 5-minute startup window. Typical causes: missing CUDA/cuDNN shared libraries, out-of-memory (GPU VRAM or system RAM), an unreadable or unsupported .gguf, mismatched --chat-template/--no-jinja flags, or an invalid port/argument.","commonSituations":"First run on a machine without matching CUDA toolkit; model larger than available VRAM; mmproj version mismatched to the model; recent llama.cpp upgrade changed required flags.","solutions":["Read the embedded tail in the exception — llama-server prints the real error (CUDA error, 'failed to load model', gguf parse error) to stderr.","If CUDA is missing/broken, install matching CUDA + cuDNN or force CPU mode by removing any --n-gpu-layers flag in the launch args.","For VRAM exhaustion, pick a smaller quant or pass fewer GPU layers; verify free memory with nvidia-smi before starting.","Re-verify the .gguf integrity (file size vs HuggingFace) and that --chat-template matches the model family.","Reproduce the launch with the exact FormatLaunchCommand output in a terminal to see live stderr."],"exampleFix":"// before\nthrow new InvalidOperationException(\n    $\"llama-server exited during startup (code {process.ExitCode}). Output: {tail}\");\n\n// after — keep the tail, but also stash last exit code for callers\nteardown(process);\nthrow new InvalidOperationException(\n    $\"llama-server exited during startup (code {process.ExitCode}). \" +\n    $\"Rerun manually: {FormatLaunchCommand(exe, psi.ArgumentList)}. Output: {tail}\");","handlingStrategy":"try-catch","validationCode":"public static void PreFlight(string exe, string modelPath)\n{\n    if (!File.Exists(exe) || !File.Exists(modelPath)) throw new InvalidOperationException(\"Missing llama-server or model.\");\n    if (OperatingSystem.IsLinux() && !HasCudaLibs()) LogWarning(\"CUDA libs missing; llama-server may crash on GPU init.\");\n}","typeGuard":"null","tryCatchPattern":"try { await StartServerAsync(...); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"exited during startup\"))\n{ SeLogger.Error(ex, \"llama-server crashed at startup; rerun command manually.\"); throw; }","preventionTips":["Always log the full embedded tail when this fires — the real cause is in there.","Run the formatted launch command in a shell once to confirm it boots standalone.","Match CUDA/cuDNN versions to the llama.cpp build before invoking."],"tags":["llama-cpp","process-crash","startup","cuda","diagnostics"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}