{"record":{"id":"2f4740e09437210f","repo":"tursodatabase/turso","slug":"mmap-failed","errorCode":null,"errorMessage":"mmap failed: {}","messagePattern":"mmap failed: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"core/storage/buffer_pool.rs","lineNumber":458,"sourceCode":"}\n\n#[cfg(all(unix, not(miri)))]\nmod arena {\n    use libc::MAP_ANONYMOUS;\n    use libc::{mmap, munmap, MAP_PRIVATE, PROT_READ, PROT_WRITE};\n    use std::ffi::c_void;\n\n    pub unsafe fn alloc(len: usize) -> *mut u8 {\n        let ptr = mmap(\n            std::ptr::null_mut(),\n            len,\n            PROT_READ | PROT_WRITE,\n            MAP_PRIVATE | MAP_ANONYMOUS,\n            -1,\n            0,\n        );\n        if ptr == libc::MAP_FAILED {\n            panic!(\"mmap failed: {}\", std::io::Error::last_os_error());\n        }\n        #[cfg(target_os = \"linux\")]\n        {\n            libc::madvise(ptr, len, libc::MADV_HUGEPAGE);\n        }\n        ptr as *mut u8\n    }\n\n    pub unsafe fn dealloc(ptr: *mut u8, len: usize) {\n        let result = munmap(ptr as *mut c_void, len);\n        if result != 0 {\n            panic!(\"munmap failed: {}\", std::io::Error::last_os_error());\n        }\n    }\n}\n\n#[cfg(any(not(unix), miri))]\nmod arena {","sourceCodeStart":440,"sourceCodeEnd":476,"githubUrl":"https://github.com/tursodatabase/turso/blob/244cde92a7df7f9b8b8b7a4075c35a12977e303e/core/storage/buffer_pool.rs#L440-L476","documentation":"The Unix buffer-pool arena backs its allocation with one anonymous mmap (PROT_READ|PROT_WRITE, plus MADV_HUGEPAGE on Linux). If mmap returns MAP_FAILED, alloc panics with the OS error - typically ENOMEM from exhausted address space, strict overcommit accounting, or container memory caps.","triggerScenarios":"Creating or growing the buffer pool beyond available virtual memory: vm.overcommit_memory=2 with a small CommitLimit, cgroup or RLIMIT_AS container limits, 32-bit targets, or pathologically huge pool sizes.","commonSituations":"Containers with strict memory limits; CI runners; strict overcommit configuration; disabled swap with commit accounting.","solutions":["Size the buffer pool to fit the container/VM memory budget","Raise the limit: increase the cgroup memory limit or RLIMIT_AS (ulimit -v), or relax overcommit (vm.overcommit_memory=1)","Check dmesg for OOM/commit-accounting messages to confirm the ENOMEM source","Note the non-Unix/miri build falls back to the std allocator when mmap is unavailable"],"exampleFix":"# before\n# container capped at 2G; pool asks for far more -> panic: mmap failed: Cannot allocate memory\n\n# after\ndocker run -m 8g ...              # raise the memory limit\n# or: sysctl vm.overcommit_memory=1  # allow the reservation\n# or configure a smaller buffer pool","handlingStrategy":"fallback","validationCode":"// check headroom before creating the pool\nlet avail = available_virtual_memory()?; // read /proc/meminfo CommitLimit or sysinfo crate\nassert!(pool_bytes + reserved <= avail, \"buffer pool exceeds address space\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Derive pool size from the container/cgroup limit rather than hardcoding","Run smoke tests under the same memory limits as production","Avoid large pools on 32-bit targets"],"tags":["memory","mmap","buffer-pool","unix","allocation","panic"],"backgroundTag":"memory-allocation-failed","analyzedSha":"244cde92a7df7f9b8b8b7a4075c35a12977e303e","analyzedAt":"2026-08-20T07:02:18.389Z","contentChangedAt":"2026-08-20T07:02:18.389Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}