{"record":{"id":"908f44a940bb23b8","repo":"SubtitleEdit/subtitleedit","slug":"tesseract-exited-with-code-proc-exitcode-err","errorCode":null,"errorMessage":"Tesseract exited with code {proc.ExitCode}: {err}","messagePattern":"Tesseract exited with code (.+?): (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/seconv/Core/TesseractOcrEngine.cs","lineNumber":100,"sourceCode":"                ArgumentList = { pngPath, \"stdout\", \"-l\", Language, \"--psm\", \"6\" },\n                RedirectStandardOutput = true,\n                RedirectStandardError = true,\n                StandardOutputEncoding = System.Text.Encoding.UTF8,\n                StandardErrorEncoding = System.Text.Encoding.UTF8,\n                UseShellExecute = false,\n                CreateNoWindow = true,\n            };\n            using var proc = Process.Start(psi)\n                ?? throw new InvalidOperationException(\"Failed to start tesseract process.\");\n            // Drain stderr concurrently — Tesseract emits warnings to stderr, and reading\n            // stdout to completion while stderr fills the pipe buffer would deadlock.\n            var stderrTask = proc.StandardError.ReadToEndAsync();\n            var stdout = proc.StandardOutput.ReadToEnd();\n            proc.WaitForExit();\n            if (proc.ExitCode != 0)\n            {\n                var err = stderrTask.GetAwaiter().GetResult();\n                throw new InvalidOperationException($\"Tesseract exited with code {proc.ExitCode}: {err}\");\n            }\n            return stdout.Trim();\n        }\n        finally\n        {\n            try { File.Delete(pngPath); } catch { /* best-effort */ }\n            prepped.Dispose();\n        }\n    }\n\n    private static SKBitmap Preprocess(SKBitmap source)\n    {\n        const int scale = 2;\n        var w = source.Width * scale;\n        var h = source.Height * scale;\n        var prepped = new SKBitmap(new SKImageInfo(w, h, SKColorType.Rgba8888, SKAlphaType.Opaque));\n        using var canvas = new SKCanvas(prepped);\n        canvas.Clear(SKColors.White);","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/seconv/Core/TesseractOcrEngine.cs#L82-L118","documentation":"Thrown after the Tesseract process ran but exited with a non-zero code. The captured stderr (drained concurrently to avoid pipe deadlock) is appended to the message, so it usually names the real cause (missing language data, corrupt image, bad --psm, model errors).","triggerScenarios":"proc.ExitCode != 0 after Process.Start of tesseract with arguments [pngPath, stdout, -l Language, --psm 6]. stderr is awaited and folded into the message.","commonSituations":"Requested a language whose .traineddata is not installed (e.g. -l fra with only eng present); Tesseract version mismatch with the model; malformed/expired PNG written to the temp work dir; insufficient tessdata dir / TESSDATA_PREFIX unset.","solutions":["Install the requested language pack (apt install tesseract-ocr-fra / brew install tesseract-lang, or drop the .traineddata into the tessdata folder).","Set TESSDATA_PREFIX to the directory holding the .traineddata files and retry.","Read the stderr in the message — it states the exact Tesseract error.","Verify the Tesseract version matches the model format."],"exampleFix":"// before: -l fra but only eng installed\nengine = TesseractOcrEngine.Create(\"fra\");\n// after: install pack + keep language\n$ apt install tesseract-ocr-fra\nengine = TesseractOcrEngine.Create(\"fra\");","handlingStrategy":"try-catch","validationCode":"// Verify the requested language's traineddata exists\nvar tessdata = Environment.GetEnvironmentVariable(\"TESSDATA_PREFIX\") ?? Path.Combine(Path.GetDirectoryName(ExecutablePath) ?? \".\", \"tessdata\");\nif (!File.Exists(Path.Combine(tessdata, Language + \".traineddata\")))\n    throw new InvalidOperationException($\"Missing {Language}.traineddata in {tessdata}\");","typeGuard":"null","tryCatchPattern":"try { return engine.Recognize(bmp); }\ncatch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Tesseract exited\")) {\n    logger.Error(\"Tesseract stderr: \" + ex.Message);\n    return string.Empty;\n}","preventionTips":["Install every language pack you intend to request.","Keep TESSDATA_PREFIX pointing at the populated tessdata dir.","Match model versions to the installed Tesseract major version."],"tags":["seconv","ocr","tesseract","language-data","process-exit-code","tessdata"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}