{"record":{"id":"7dc532c008869d63","repo":"elastic/elasticsearch","slug":"madvise-failed-with-error-errno-strerror","errorCode":null,"errorMessage":"madvise failed with (error={errno}): {strerror}","messagePattern":"madvise failed with \\(error=(.+?)\\): (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"warning","filePath":"libs/native/src/main/java/org/elasticsearch/nativeaccess/jdk/PosixMappedSegment.java","lineNumber":69,"sourceCode":"        Objects.checkFromIndexSize(offset, length, segment.byteSize());\n        // Align offset with the page size, this is required for madvise.\n        // Compute the offset of the current position in the OS's page.\n        final long offsetInPage = (segment.address() + offset) % PAGE_SIZE;\n        offset -= offsetInPage;\n        length += offsetInPage;\n        if (offset < 0) {\n            // start of the page is before the start of this segment, ignore the first page.\n            offset += PAGE_SIZE;\n            length -= PAGE_SIZE;\n            if (length <= 0) {\n                // This segment has no data beyond the first page.\n                return;\n            }\n        }\n        int ret = LIB.madvise(segment, offset, length, advice);\n        if (ret != 0) {\n            int errno = LIB.errno();\n            throw new RuntimeException(\"madvise failed with (error=\" + errno + \"): \" + LIB.strerror(errno));\n        }\n    }\n}\n","sourceCodeStart":51,"sourceCodeEnd":73,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/libs/native/src/main/java/org/elasticsearch/nativeaccess/jdk/PosixMappedSegment.java#L51-L73","documentation":"Thrown when the POSIX madvise(3) syscall returns non-zero for a mapped memory segment. The wrapper reads errno via the POSIX library and formats both the numeric errno and strerror in the message. madvise advises the kernel about access patterns (prefetch/WILLNEED etc.); a failure generally means the kernel rejected the advice for the given address range, not that data was lost. RuntimeException (unchecked).","triggerScenarios":"Calling prefetch(offset,length) or madvise(...) on a memory-mapped segment where [offset, offset+length) is not page-aligned or not backed by a valid mapping; passing an advice constant the running kernel does not support; the underlying mapping was unmapped/closed concurrently; offset/length math produces a range outside the segment.","commonSituations":"Running on an older or non-Linux POSIX kernel (e.g. some BSDs) that does not implement POSIX_MADV_WILLNEED; a concurrent close()/unmap racing the prefetch; containerized runtimes where madvise is a no-op or returns EINVAL for certain advice values; miscomputed length after the page-alignment adjustments earlier in the method.","solutions":["Treat madvise as advisory: wrap the call in try/catch(RuntimeException) and log at DEBUG rather than failing the read/prefetch — the kernel will still page-fault correctly on demand.","Confirm the mapping is still open and the segment is valid before issuing the advice (synchronize with the segment's lifecycle).","If on a kernel lacking the advice constant, gate the call behind a capability check (LIB capability probe) or skip POSIX_MADV_WILLNEED.","Inspect errno: EINVAL usually means unsupported advice or misaligned range; ENOMEM means the range is not backed by a mapping."],"exampleFix":"// before\nsegment.prefetch(offset, length);\n\n// after: prefetch is advisory, never let it abort the read\ntry {\n    segment.prefetch(offset, length);\n} catch (RuntimeException e) {\n    logger.debug(\"madvise/prefetch ignored\", e);\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// madvise/prefetch is advisory — never let a syscall failure abort the read path.\ntry {\n    segment.prefetch(offset, length);\n} catch (RuntimeException e) {\n    logger.debug(\"madvise/prefetch ignored for segment at offset={} length={}\", offset, length, e);\n}","preventionTips":["Treat madvise as advisory; log and continue on failure.","Confirm the mapping is still open and aligned before issuing the advice.","Gate OS-specific advice constants behind a capability probe on kernels that may not support them."],"tags":["native","posix","mmap","madvise","memory"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:17:08.281Z"}