{"record":{"id":"4e31f97e8b099c9b","repo":"LykosAI/StabilityMatrix","slug":"not-enough-free-space-to-download-file-free-freespace-bytes","errorCode":null,"errorMessage":"Not enough free space to download file. Free: {freeSpace} bytes, Required: {contentLength} bytes","messagePattern":"Not enough free space to download file\\. Free: (.+?) bytes, Required: (.+?) bytes","errorType":"exception","errorClass":"ApplicationException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Core/Services/DownloadService.cs","lineNumber":86,"sourceCode":"                break;\n            logger.LogDebug(\"Retrying get-headers for content-length\");\n            await Task.Delay(delay, cancellationToken).ConfigureAwait(false);\n            response = await client\n                .GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead, cancellationToken)\n                .ConfigureAwait(false);\n            contentLength = response.Content.Headers.ContentLength ?? 0;\n        }\n        var isIndeterminate = contentLength == 0;\n\n        if (contentLength > 0)\n        {\n            // check free space\n            if (\n                SystemInfo.GetDiskFreeSpaceBytes(Path.GetDirectoryName(downloadPath)) is { } freeSpace\n                && freeSpace < contentLength\n            )\n            {\n                throw new ApplicationException(\n                    $\"Not enough free space to download file. Free: {freeSpace} bytes, Required: {contentLength} bytes\"\n                );\n            }\n        }\n\n        await using var stream = await response\n            .Content.ReadAsStreamAsync(cancellationToken)\n            .ConfigureAwait(false);\n        var stopwatch = Stopwatch.StartNew();\n        var totalBytesRead = 0L;\n        var buffer = new byte[BufferSize];\n        while (true)\n        {\n            var bytesRead = await stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);\n            if (bytesRead == 0)\n                break;\n            await file.WriteAsync(buffer.AsMemory(0, bytesRead), cancellationToken).ConfigureAwait(false);\n","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Core/Services/DownloadService.cs#L68-L104","documentation":"DownloadToFileAsync checks the Content-Length of the response against the free disk space on the drive that will hold downloadPath. If free space is less than the required content length, it throws ApplicationException before writing anything. It is a pre-flight guard so large downloads fail fast instead of mid-stream with an out-of-space IO error.","triggerScenarios":"Calling DownloadToFileAsync for a file whose Content-Length exceeds free space on the target drive. Only triggered when Content-Length > 0 (known-size downloads); chunked/indeterminate downloads skip the check and may fail later with IOException.","commonSituations":"Downloading multi-GB model checkpoints to a nearly-full drive (often C: on Windows); downloadPath on a small system drive or a full RAM disk/network share; downloadUrl pointing at a huge file by mistake.","solutions":["Free disk space on the drive containing downloadPath (delete old packages/models) and retry","Choose a downloadPath on a drive with enough free space (query DriveInfo to pick one)","Catch ApplicationException, surface the free/required byte counts to the user, and let them pick another location","Check contentLength in advance for URLs you control and warn before starting the download"],"exampleFix":"// before\nawait downloadService.DownloadToFileAsync(url, downloadPath);\n// after\nvar drive = new DriveInfo(Path.GetPathRoot(Path.GetFullPath(downloadPath))!);\nif (drive.AvailableFreeSpace < requiredBytes)\n    throw new InvalidOperationException($\"Need {requiredBytes} bytes, only {drive.AvailableFreeSpace} free on {drive.Name}\");\nawait downloadService.DownloadToFileAsync(url, downloadPath);","handlingStrategy":"validation","validationCode":"var dir = Path.GetDirectoryName(Path.GetFullPath(downloadPath));\nvar freeSpace = SystemInfo.GetDiskFreeSpaceBytes(dir);\n// contentLength from a HEAD request:\nusing var head = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, url));\nvar required = head.Content.Headers.ContentLength ?? 0;\nif (freeSpace.HasValue && freeSpace.Value < required)\n    throw new InvalidOperationException($\"Need {required} bytes, only {freeSpace.Value} free on {dir}\");","typeGuard":null,"tryCatchPattern":"try\n{\n    await downloadService.DownloadToFileAsync(url, path);\n}\ncatch (ApplicationException ex) when (ex.Message.StartsWith(\"Not enough free space\"))\n{\n    logger.LogWarning(ex, \"Insufficient disk space for {Path}\", path);\n    PromptUserForAnotherDrive();\n}","preventionTips":["Check DriveInfo.AvailableFreeSpace (plus headroom for extraction) before large downloads","Place downloadPath on the drive with the most free space","Surface free/required byte counts in the UI before starting","Watch for disk filling during other concurrent operations (caches, temp files)"],"tags":["download","disk-space","io","validation"],"backgroundTag":"insufficient-disk-space","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}