{"record":{"id":"8f24109f69025025","repo":"microsoft/semantic-kernel","slug":"an-error-occurred-while-accessing-the-file","errorCode":null,"errorMessage":"An error occurred while accessing the file.","messagePattern":"An error occurred while accessing the file\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"dotnet/samples/GettingStartedWithProcesses/Utilities/MermaidRenderer.cs","lineNumber":105,"sourceCode":"\n        // Create a temporary HTML file with the Mermaid code\n        string tempHtmlFile = Path.Combine(Path.GetTempPath(), \"mermaid_temp.html\");\n        try\n        {\n            await File.WriteAllTextAsync(tempHtmlFile, htmlContent);\n\n            // Launch Puppeteer-Sharp with a headless browser to render the Mermaid diagram\n            using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))\n            using (var page = await browser.NewPageAsync())\n            {\n                await page.GoToAsync($\"file://{tempHtmlFile}\");\n                await page.WaitForSelectorAsync(\".mermaid\"); // Wait for Mermaid to render\n                await page.ScreenshotAsync(outputFilePath, new ScreenshotOptions { FullPage = true });\n            }\n        }\n        catch (IOException ex)\n        {\n            throw new IOException(\"An error occurred while accessing the file.\", ex);\n        }\n        catch (Exception ex) // Catch any other exceptions that might occur  \n        {\n            throw new InvalidOperationException(\n                \"An unexpected error occurred during the Mermaid diagram rendering.\", ex);\n        }\n        finally\n        {\n            // Clean up the temporary HTML file  \n            if (File.Exists(tempHtmlFile))\n            {\n                File.Delete(tempHtmlFile);\n            }\n        }\n\n        return outputFilePath;\n    }\n}","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/samples/GettingStartedWithProcesses/Utilities/MermaidRenderer.cs#L87-L123","documentation":"An IOException re-thrown with a generic wrapper when any IOException occurs during the Puppeteer-Sharp rendering pipeline (temp HTML creation, screenshot write). The original exception is preserved as InnerException, so the cause is a file-access problem rather than rendering logic.","triggerScenarios":"IOException during temp file write/read or page.ScreenshotAsync(outputFilePath): file locked, permission denied, disk full, path too long, or a temp HTML cleanup conflict.","commonSituations":"Antivirus locking temp files; concurrent runs writing the same output path; insufficient permissions on output folder; disk-full or path-length limits on Windows.","solutions":["Inspect the InnerException for the specific IO error code/message.","Ensure write permissions and that the output path is not locked by another process.","Run one render at a time or use unique temp file names to avoid contention.","Free disk space and shorten the output path."],"exampleFix":"// before\ncatch (IOException ex) { throw new IOException(\"An error occurred while accessing the file.\", ex); }\n// after - surface the real path and HResult\ncatch (IOException ex) { throw new IOException($\"IO error writing '{outputFilePath}' (0x{ex.HResult:X}).\", ex); }","handlingStrategy":"try-catch","validationCode":"string dir = Path.GetDirectoryName(Path.GetFullPath(outputFilePath))!;\nif (!HasWriteAccess(dir)) throw new IOException($\"No write access to {dir}\");\n// where HasWriteAccess attempts a temp file create/delete probe","typeGuard":null,"tryCatchPattern":"try { await MermaidRenderer.GenerateMermaidImageAsync(code, path); }\ncatch (IOException ex) { logger.LogError(ex, \"IO failure rendering {Path}\", path); /* retry with unique temp name or report */ }","preventionTips":["Use unique temp file names per render to avoid lock contention.","Ensure the output directory is writable.","Keep output paths short to avoid path-length limits."],"tags":["mermaid","filesystem","io","puppeteer"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}