Kareadita/Kavita · error · FileNotFoundException

Source file not found

Error message

Source file not found

What it means

A standard .NET FileNotFoundException thrown by CopyFile when the source file does not exist. The method checks File.Exists(sourcePath) before attempting the copy. Unlike KavitaException, this is a raw BCL exception that propagates as a 500 if uncaught. The source path is included as the fileName parameter of the exception.

Source

Thrown at Kavita.Services/DirectoryService.cs:890

    {
        foreach (var file in files)
        {
            try
            {
                FileSystem.FileInfo.New(file).Delete();
            }
            catch (Exception)
            {
                /* Swallow exception */
            }
        }
    }

    public void CopyFile(string sourcePath, string destinationPath, bool overwrite = true)
    {
        if (!File.Exists(sourcePath))
        {
            throw new FileNotFoundException("Source file not found", sourcePath);
        }

        var destinationDirectory = Path.GetDirectoryName(destinationPath);
        if (string.IsNullOrEmpty(destinationDirectory))
        {
            throw new ArgumentException("Destination path does not contain a directory", nameof(destinationPath));
        }

        if (!Directory.Exists(destinationDirectory))
        {
            FileSystem.Directory.CreateDirectory(destinationDirectory);
        }

        FileSystem.File.Copy(sourcePath, destinationPath, overwrite);
    }

    /// <summary>
    /// Returns the human-readable file size for an arbitrary, 64-bit file size

View on GitHub (pinned to 9c3e540000)

Solutions

  1. Verify the source file exists at the specified path before the operation
  2. Ensure no external processes are modifying or deleting files in the library during operations
  3. Wrap the CopyFile call in a try-catch for FileNotFoundException and log a warning rather than crashing
  4. For library operations, run a scan to synchronize the database with disk state before retrying

Example fix

// Defensive caller pattern:
// try {
//     directoryService.CopyFile(sourcePath, destPath);
// } catch (FileNotFoundException ex) {
//     logger.LogWarning(ex, "Source file vanished before copy: {Path}", sourcePath);
//     // handle gracefully — skip or re-scan
// }
Defensive patterns

Strategy: try-catch

Validate before calling

// Check file existence before copying:
// if (!File.Exists(sourcePath)) {
//     logger.LogWarning("Source file not found: {Path}", sourcePath);
//     return false;
// }

Try / catch

// try {
//     directoryService.CopyFile(sourcePath, destinationPath, overwrite);
// } catch (FileNotFoundException ex) {
//     logger.LogWarning(ex, "Source file vanished before copy: {Path}", ex.FileName);
//     // handle gracefully — skip, re-scan, or report media issue
// }

Prevention

When it happens

Trigger: Source file was deleted or moved between the time it was enumerated and the copy was attempted; source path is misspelled or refers to a file on an unmounted share; file permissions prevent the Exists check from seeing the file.

Common situations: Race condition during library processing where files are modified by external tools; concurrent scans and user-initiated file operations; backup or sync tools modifying the library while Kavita reads it; temp files cleaned up before the copy completes.

Related errors


AI-assisted analysis of Kareadita/Kavita@9c3e540000 (2026-08-13). Data as JSON: /api/errors/2d8934c9ef5451f6. Report an issue: GitHub.