From 8d08022554fdea12ebd00b05b14bf450d6083316 Mon Sep 17 00:00:00 2001 From: Michael Pysson Date: Thu, 29 Oct 2026 17:05:09 -0700 Subject: [PATCH] Fix scan failures caused by cleanup errors Handle I/O and access errors during cleanup inventory and PipReport report deletion without aborting scans. Skip artifact cleanup when inventory is incomplete, and log the path, exception type, and message without exception stack traces. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 993305b5-0b71-4bbb-9d82-c123b1da01d7 --- .../FileComponentDetectorWithCleanup.cs | 9 ++++++--- .../pip/PipReportComponentDetector.cs | 13 ++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.ComponentDetection.Contracts/FileComponentDetectorWithCleanup.cs b/src/Microsoft.ComponentDetection.Contracts/FileComponentDetectorWithCleanup.cs index 32ce18fc6..2a6e96dc5 100644 --- a/src/Microsoft.ComponentDetection.Contracts/FileComponentDetectorWithCleanup.cs +++ b/src/Microsoft.ComponentDetection.Contracts/FileComponentDetectorWithCleanup.cs @@ -160,10 +160,13 @@ private bool TryGetCleanupFileDirectory(ProcessRequest processRequest, out strin var (files, directories) = this.DirectoryUtilityService.GetFilesAndDirectories(root, patterns, depth); return (true, files, directories); } - catch (UnauthorizedAccessException e) + catch (Exception e) when (e is IOException or UnauthorizedAccessException) { - // log and return false if we are unauthorized to get files and directories - this.Logger.LogDebug(e, "Unauthorized to get files and directories for {Root}", root); + this.Logger.LogDebug( + "Failed to get files and directories for {Root}. Skipping cleanup. {ExceptionType}: {Message}", + root, + e.GetType().Name, + e.Message); return (false, new HashSet(), new HashSet()); } } diff --git a/src/Microsoft.ComponentDetection.Detectors/pip/PipReportComponentDetector.cs b/src/Microsoft.ComponentDetection.Detectors/pip/PipReportComponentDetector.cs index 4dd9a2b27..91d6f9a51 100644 --- a/src/Microsoft.ComponentDetection.Detectors/pip/PipReportComponentDetector.cs +++ b/src/Microsoft.ComponentDetection.Detectors/pip/PipReportComponentDetector.cs @@ -347,7 +347,18 @@ protected override async Task OnFileFoundAsync(ProcessRequest processRequest, ID { if (reportFile is not null && reportFile.Exists) { - reportFile.Delete(); + try + { + reportFile.Delete(); + } + catch (Exception e) when (e is IOException or UnauthorizedAccessException) + { + this.Logger.LogDebug( + "PipReport: Failed to delete report file {File}. {ExceptionType}: {Message}", + reportFile.FullName, + e.GetType().Name, + e.Message); + } } } }