{"record":{"id":"4e70dce13b591a3d","repo":"nats-io/nats-server","slug":"failed-to-load-block-from-disk-w","errorCode":null,"errorMessage":"failed to load block from disk: %w","messagePattern":"failed to load block from disk: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/filestore.go","lineNumber":6878,"sourceCode":"\t\t\t\tif rl > mb.bytes {\n\t\t\t\t\trl = mb.bytes\n\t\t\t\t}\n\t\t\t\tmb.bytes -= rl\n\t\t\t\tmb.rbytes -= rl\n\t\t\t\t// For return accounting.\n\t\t\t\tpurged++\n\t\t\t\tbytes += uint64(rl)\n\t\t\t}\n\t\t}\n\t}\n\n\t// If the block is compressed/encrypted then we have to load it into memory\n\t// and decompress/decrypt it, truncate it and then write it back out.\n\t// Otherwise, truncate the file itself and close the descriptor.\n\tif mb.cmp != NoCompression || mb.bek != nil {\n\t\tbuf, err := mb.loadBlock(nil)\n\t\tif err != nil {\n\t\t\treturn 0, 0, fmt.Errorf(\"failed to load block from disk: %w\", err)\n\t\t}\n\t\tif err = mb.encryptOrDecryptIfNeeded(buf); err != nil {\n\t\t\treturn 0, 0, err\n\t\t}\n\t\tif buf, err = mb.decompressIfNeeded(buf); err != nil {\n\t\t\treturn 0, 0, fmt.Errorf(\"failed to decompress block: %w\", err)\n\t\t}\n\t\tbuf = buf[:eof]\n\t\tcopy(mb.lchk[0:], buf[len(buf)-checksumSize:])\n\t\t// We did decompress but don't recompress the truncated buffer here since we're the last block\n\t\t// and would otherwise have compressed data and allow to write uncompressed data in the same block.\n\t\tif err = mb.atomicOverwriteFile(buf, false); err != nil {\n\t\t\treturn 0, 0, err\n\t\t}\n\t} else if mb.mfd != nil {\n\t\tif err = mb.mfd.Truncate(eof); err != nil {\n\t\t\treturn 0, 0, err\n\t\t}","sourceCodeStart":6860,"sourceCodeEnd":6896,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/filestore.go#L6860-L6896","documentation":"Wraps any error from mb.loadBlock(nil) that occurs while truncating the last message block: if the block is compressed and/or encrypted, it must be fully loaded into memory first. This error means the block file could not be read/validated from disk (IO error, checksum failure, or underlying corruption like 'sanity check failed').","triggerScenarios":"Trimming/compacting the last block of a stream (truncateMsgs path) when the block has compression (mb.cmp != NoCompression) or encryption (mb.bek != nil) and loadBlock fails — unreadable file, bad checksum, corrupt record layout.","commonSituations":"Corrupt or truncated block files after crashes, failing disks, restored backups missing chunks, or read errors at the OS level (EIO).","solutions":["Inspect the wrapped cause (read error vs checksum vs sanity check) with debug logging","Remove/reset the affected stream files so JetStream recreates them (accepting data loss for that stream)","Restore the store directory from a consistent offline backup","Run filesystem/disk health checks and replace failing hardware"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Before trim/compact operations, verify store volume readability and space\nif info, err := os.Stat(storeDir); err != nil || !info.IsDir() {\n    return fmt.Errorf(\"store dir unreadable: %v\", err)\n}","typeGuard":null,"tryCatchPattern":"// Go: inspect the wrapped cause of 'failed to load block from disk'\nif _, _, err := truncateLastBlock(mb, eof); err != nil {\n    var ioErr *os.PathError\n    if errors.As(err, &ioErr) {\n        // OS-level read failure: check disk/filesystem\n    } else if strings.Contains(err.Error(), \"sanity check\") {\n        // corrupt block: restore backup or reset stream\n    }\n    return err\n}","preventionTips":["Monitor disk health (SMART) on jetstream nodes","Back up store directories only while stopped","Avoid kill -9 and power loss on storage volumes","Run fsck after unclean host shutdowns"],"tags":["io","corruption","truncation","filestore"],"backgroundTag":"block-load-failed","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}