elastic/elasticsearch · warning · RuntimeException

madvise failed with (error={errno}): {strerror}

Error message

madvise failed with (error={errno}): {strerror}

What it means

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).

Source

Thrown at libs/native/src/main/java/org/elasticsearch/nativeaccess/jdk/PosixMappedSegment.java:69

        Objects.checkFromIndexSize(offset, length, segment.byteSize());
        // Align offset with the page size, this is required for madvise.
        // Compute the offset of the current position in the OS's page.
        final long offsetInPage = (segment.address() + offset) % PAGE_SIZE;
        offset -= offsetInPage;
        length += offsetInPage;
        if (offset < 0) {
            // start of the page is before the start of this segment, ignore the first page.
            offset += PAGE_SIZE;
            length -= PAGE_SIZE;
            if (length <= 0) {
                // This segment has no data beyond the first page.
                return;
            }
        }
        int ret = LIB.madvise(segment, offset, length, advice);
        if (ret != 0) {
            int errno = LIB.errno();
            throw new RuntimeException("madvise failed with (error=" + errno + "): " + LIB.strerror(errno));
        }
    }
}

View on GitHub (pinned to db6a809a66)

Solutions

  1. 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.
  2. Confirm the mapping is still open and the segment is valid before issuing the advice (synchronize with the segment's lifecycle).
  3. If on a kernel lacking the advice constant, gate the call behind a capability check (LIB capability probe) or skip POSIX_MADV_WILLNEED.
  4. Inspect errno: EINVAL usually means unsupported advice or misaligned range; ENOMEM means the range is not backed by a mapping.

Example fix

// before
segment.prefetch(offset, length);

// after: prefetch is advisory, never let it abort the read
try {
    segment.prefetch(offset, length);
} catch (RuntimeException e) {
    logger.debug("madvise/prefetch ignored", e);
}
Defensive patterns

Strategy: try-catch

Try / catch

// madvise/prefetch is advisory — never let a syscall failure abort the read path.
try {
    segment.prefetch(offset, length);
} catch (RuntimeException e) {
    logger.debug("madvise/prefetch ignored for segment at offset={} length={}", offset, length, e);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12). Data as JSON: /api/errors/7dc532c008869d63. Report an issue: GitHub.