From 1a7c2299c6d6b7d973ac73af9f116ca51b941ca7 Mon Sep 17 00:00:00 2001 From: gnattu Date: Sun, 19 Jan 2025 00:40:13 +0800 Subject: [PATCH] Catch IOExceptions for GetFileSystemMetadata Our `GetFileSystemEntries` method will throw when enumerating the file system, but its callers might consider the unhandled exceptions as the whole path is not available. This would cause a single problematic file to fail the enumeration, and could lead to unexpected side effects. HandleIOException gracefully by marking the files throwing as not exist to let the caller skip that file. --- Emby.Server.Implementations/IO/ManagedFileSystem.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Emby.Server.Implementations/IO/ManagedFileSystem.cs b/Emby.Server.Implementations/IO/ManagedFileSystem.cs index 4b68f21d55..55945e3374 100644 --- a/Emby.Server.Implementations/IO/ManagedFileSystem.cs +++ b/Emby.Server.Implementations/IO/ManagedFileSystem.cs @@ -276,6 +276,13 @@ namespace Emby.Server.Implementations.IO { _logger.LogError(ex, "Reading the file at {Path} failed due to a permissions exception.", fileInfo.FullName); } + catch (IOException ex) + { + // IOException generally means the file is not accessible due to filesystem issues + // Catch this exception and mark the file as not exist to ignore it + _logger.LogError(ex, "Reading the file at {Path} failed due to an IO Exception. Marking the file as not existing", fileInfo.FullName); + result.Exists = false; + } } } @@ -590,6 +597,9 @@ namespace Emby.Server.Implementations.IO /// public virtual IEnumerable GetFileSystemEntries(string path, bool recursive = false) { + // Note: any of unhandled exceptions thrown by this method may cause the caller to believe the whole path is not accessible. + // But what causing the exception may be a single file under that path. This could lead to unexpected behavior. + // For example, the scanner will remove everything in that path due to unhandled errors. var directoryInfo = new DirectoryInfo(path); var enumerationOptions = GetEnumerationOptions(recursive);