{"record":{"id":"4c841c09eb7d5826","repo":"databendlabs/databend","slug":"write-returned-zero-bytes","errorCode":null,"errorMessage":"write returned zero bytes","messagePattern":"write returned zero bytes","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/common/base/src/base/dma.rs","lineNumber":234,"sourceCode":"    pub fn align_down(&self, value: usize) -> usize {\n        self.alignment.align_down(value)\n    }\n\n    /// Return the alignment requirement for this file. The returned alignment value can be used\n    /// to allocate a buffer to use with this file:\n    pub fn alignment(&self) -> Alignment {\n        self.alignment\n    }\n\n    fn write_direct(&mut self, buf: &mut DmaBuffer) -> io::Result<usize> {\n        let data = buf.as_slice();\n        let mut written = 0;\n        let offset = self.align_down(self.offset);\n\n        while written < data.len() {\n            match rustix::io::pwrite(&self.fd, &data[written..], (offset + written) as _) {\n                Ok(0) => {\n                    return Err(io::Error::new(\n                        io::ErrorKind::WriteZero,\n                        \"write returned zero bytes\",\n                    ));\n                }\n                Ok(n) => {\n                    written += n;\n                    self.offset = offset + written\n                }\n                Err(err) => {\n                    if err.kind() == io::ErrorKind::Interrupted {\n                        continue;\n                    }\n                    return Err(err.into());\n                }\n            }\n        }\n        buf.clear();\n        Ok(written)","sourceCodeStart":216,"sourceCodeEnd":252,"githubUrl":"https://github.com/databendlabs/databend/blob/288d84d76e20a2f8f7173bda9691eb6ece301aa9/src/common/base/src/base/dma.rs#L216-L252","documentation":"`write_direct` performs aligned `pwrite` calls on a DMA (O_DIRECT) file in a loop until all data is written. POSIX allows short writes, but a return of 0 bytes means no progress was made — treated as `ErrorKind::WriteZero`. Continuing would loop forever, so the code aborts with this io::Error.","triggerScenarios":"`rustix::io::pwrite` on the direct-I/O file returns `Ok(0)`, e.g. at/behind EOF semantics on certain filesystems, a full disk returning zero, or the fd reaching an unexpected end condition mid-write.","commonSituations":"Disk full or quota exhausted on the backing device, filesystem not honoring O_DIRECT writes at the requested aligned offset, unusual fuse/network filesystems that return 0 instead of an error.","solutions":["Check disk space and quotas on the device holding the data file (`df -h`).","Ensure the file was opened correctly for O_DIRECT and offsets/buffer are properly aligned (the code aligns offsets, but verify the mount supports O_DIRECT).","Verify the fd is still valid and the file wasn't truncated concurrently by another process.","Retry the operation; if it reproduces on a specific filesystem, test on a filesystem known to support O_DIRECT (ext4/xfs) or disable direct I/O if configurable."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// before writing, check space and fd state\nfn can_write(fd: &File, dir: &Path) -> std::io::Result<()> {\n    let meta = fs::metadata(dir)?;\n    let avail = fs2::available_space(dir)?;\n    if avail < meta.len() { return Err(std::io::Error::new(std::io::ErrorKind::StorageFull, \"low disk\")); }\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":"match writer.write_direct(&buf) {\n    Err(e) if e.kind() == std::io::ErrorKind::WriteZero => handle_disk_full_or_io_failure(e),\n    other => other,\n}","preventionTips":["Monitor disk space and quotas on the data volume","Verify the mount supports O_DIRECT","Avoid concurrent truncation of active data files"],"tags":["io","direct-io","write"],"backgroundTag":"file-write-failed","analyzedSha":"288d84d76e20a2f8f7173bda9691eb6ece301aa9","analyzedAt":"2026-09-11T11:29:36.208Z","contentChangedAt":"2026-09-11T11:29:36.208Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}