Skip to content

[Bug]: 4.2-dev fileservice panics on read-buffer ENOMEM during CN checkpoint reads #33095

Done

Description

@iamlinjunhong

Branch and commit

Reproduced on 4.2-dev at 6faa4f240f38c56e89866e7eb1845b0f4cf3ec0c (Go 1.27.1, darwin/arm64). The original incident was on a Linux CN running 5c16f56639.

Actual behavior and impact

When a file read needs a new buffer and the IO allocator returns ENOMEM, IOEntry.prepareData panics. The caller, ReadFromOSFile, has an error return. The panic happens before reading any file data.

In QA on 2026-11-23 (UTC+8), allocator diagnostics reported 65,530 process mappings against a limit of 65,530. At 17:44:14 the logtail subscription worker hit panic: cannot allocate memory while reading checkpoint metadata. The stack included ConsumeCheckpointEntries → CKPReader.ReadMeta → … → DiskCache.Read → IOEntry.ReadFromOSFile → IOEntry.prepareData. The CN process exited with code 2 at 17:44:18 and the container restarted at 17:44:22. The workload responsible for the mapping growth is still unknown.

Expected behavior

Return an error matching ENOMEM to the caller and leave the entry incomplete. A file read that already has enough buffer capacity should still succeed.

Isolated regression case

Place the following file at pkg/fileservice/io_entry_enomem_regression_test.go in the revision above. The test reads one byte and substitutes a test allocator. Run only this test since the allocator getter is package-wide.

go test -mod=readonly -count=1 -run '^TestReadFromOSFileAllocatorENOMEM$' -v ./pkg/fileservice

Observed output (abridged):

wanted returned ENOMEM, got panic=cannot allocate memory (allocations=1)
  (*IOEntry).prepareData     io_entry.go:121
  (*IOEntry).ReadFromOSFile  io_entry.go:59
--- PASS: TestReadFromOSFileAllocatorENOMEM/existing_buffer
--- FAIL: TestReadFromOSFileAllocatorENOMEM/needs_buffer
Go test source
package fileservice

import (
	"context"
	"errors"
	"os"
	"path/filepath"
	"runtime/debug"
	"syscall"
	"testing"

	"github.com/matrixorigin/matrixone/pkg/common/malloc"
)

type enomemReadAllocator struct{ calls int }

func (a *enomemReadAllocator) Allocate(uint64, malloc.Hints) ([]byte, malloc.Deallocator, error) {
	a.calls++
	return nil, nil, syscall.ENOMEM
}

func TestReadFromOSFileAllocatorENOMEM(t *testing.T) {
	// Run this test in isolation because ioAllocator is package-wide.
	fake := new(enomemReadAllocator)
	original := ioAllocator
	ioAllocator = func() malloc.Allocator { return fake }
	t.Cleanup(func() { ioAllocator = original })

	path := filepath.Join(t.TempDir(), "input")
	if err := os.WriteFile(path, []byte("x"), 0600); err != nil {
		t.Fatal(err)
	}
	read := func(t *testing.T, entry *IOEntry) error {
		t.Helper()
		f, err := os.Open(path)
		if err != nil {
			t.Fatal(err)
		}
		defer f.Close()
		return entry.ReadFromOSFile(context.Background(), f, nil)
	}

	t.Run("existing_buffer", func(t *testing.T) {
		entry := IOEntry{Size: 1, Data: make([]byte, 1)}
		if err := read(t, &entry); err != nil || string(entry.Data) != "x" || fake.calls != 0 {
			t.Fatalf("err=%v data=%q allocations=%d", err, entry.Data, fake.calls)
		}
	})
	t.Run("needs_buffer", func(t *testing.T) {
		entry := IOEntry{Size: 1}
		defer func() {
			if p := recover(); p != nil {
				t.Fatalf("wanted returned ENOMEM, got panic=%v (allocations=%d)\n%s", p, fake.calls, debug.Stack())
			}
		}()
		err := read(t, &entry)
		if !errors.Is(err, syscall.ENOMEM) || fake.calls != 1 || entry.done {
			t.Fatalf("err=%v allocations=%d done=%v", err, fake.calls, entry.done)
		}
	})
}

Related work

#28736 and #28739 cover ENOMEM during mapping release and the allocator's recovery behavior. This case exercises the file-read caller when a subsequent allocation is refused. The unit test establishes the failure in that caller. The CN restart and the mapping count come from the separate QA incident.

Activity

  1. added
    severity/s0
    on Nov 23, 2026
  2. iamlinjunhong commented on Nov 23, 2026

    @iamlinjunhong
    ContributorAuthor

    Confirmed on upstream main at e17d5541537d30252009a9a2d4c70ab5480d3bc3 (darwin/arm64, Go 1.27.1). I ran the exact regression test from the issue body after building the native dependencies for that revision.

    One-byte file read Result on main
    Caller supplies a one-byte buffer PASS. Returned the expected byte with zero allocator calls.
    Reader must allocate a buffer FAIL. After one allocation returned ENOMEM, the reader panicked with cannot allocate memory.

    The stack was ReadFromOSFile (line 56) → readFrom (line 64) → prepareData (line 126). Both branches convert the read-buffer allocation error into a panic. Please track both main and 4.2-dev here and run the same regression on each fix. The main result is a local fileservice test. The CN process exit reported above was observed in QA on the deployed revision.

    main source at the failing line

  3. added
    ai-easy
    on Nov 24, 2026
  4. Ariznawlll commented on Nov 24, 2026

    @Ariznawlll
    Collaborator

    结论

    测试不通过。最新 4.2-dev 的原始 ENOMEM 复现仍触发 panic;main 的对应文件读取与资源释放定向验证已通过。两条分支的验收不能合并为整体通过,本次不关闭 Issue。

    关键证据

    • 2026-11-24,darwin/arm64,隔离 checkout:main@568f0f4fdee3c34373f27b04316a2adf7d32a78a、4.2-dev@9ed1645371c63ad2c417022de9e4d56db8697ecc。测试直接编译这些源码,未替换其他正在运行的 MO。
    • 原样使用本 Issue 的 TestReadFromOSFileAllocatorENOMEM。4.2-dev 连续 3 次:existing_buffer 通过、分配器调用数为 0;needs_buffer 连续 3 次失败,分配器返回一次 ENOMEM 后,在 IOEntry.prepareData 的 io_entry.go:121 panic:cannot allocate memory。测试捕获 panic 后以 FAIL 退出,并非将 panic 当成功。
    • main 的同一原始用例,以及 TestIOEntryReadBufferAllocation、TestLocalFSReadBufferAllocationRollback、TestLocalFSReadPublishesReusedDataOnSuccess,普通与 -race 均连续 3 次通过。断言覆盖返回 ENOMEM、不将失败 entry 标记完成、不消费文件偏移、部分分配资源释放、复用已有缓冲区,以及短读/转换失败的状态保持。
    • 可复跑:go test -mod=readonly -count=3 -run '^Test(ReadFromOSFileAllocatorENOMEM|IOEntryReadBufferAllocation|LocalFSReadBufferAllocationRollback|LocalFSReadPublishesReusedDataOnSuccess)$' ./pkg/fileservice;main 再加 -race。原始复现源码来自本 Issue,新增回归已在 main 修复 PR #33129 中,无另提主库测试 PR。
    • 核对时 4.2-dev 修复 PR #33131 仍为 OPEN;本轮 4.2-dev 的失败来自未合入修复的官方分支,不能据此断言该 PR 修复无效。

    待解决点

    • 待 Return fileservice read buffer allocation errors #33131 合入后,在实际官方 4.2-dev 重跑原始用例、释放/复用和 race 验证。
    • 本轮是确定性文件读取分配失败注入,未复现 Linux CN checkpoint 现场的 VMA 耗尽全过程,未查明映射增长来源;不把定向通过描述为现场压力与重启专项已通过。
  5. XuPeng-SH commented on Nov 24, 2026

    @XuPeng-SH
    Contributor

    补充 main 转测范围,保留现有 QA 负责人:PR #33162 已合入 main,merge 240df17666a179e69c98c2ea00c6668ca99be373。盘缓存读到 ENOMEM 时不再当普通未命中去对象存储再次分配。部分命中后失败会清除已释放的 vector-owned buffer 别名,避免原向量重试读到失效引用;短读等普通缓存错误仍回退。

    独立评审在早期 head b1c00d1b 的 11 项定向测试普通/race 通过,已有活动下载的 pin 释放用例 race 100 次;未过滤的全量普通/race 遇到基线也会出现的下载用例失败,不能当全量 race 通过。本轮未重跑合并 SHA。请在最新 main 验证盘缓存全文件/范围、混合批部分命中后 ENOMEM 同向量重试、资源 pin 与普通短读回退,并保留 Linux CN 映射耗尽事故另行验收。4.2-dev #33131 尚未合入,前次 4.2 定向失败仍有效;不能据此关闭工单。

  6. Ariznawlll commented on Nov 24, 2026

    @Ariznawlll
    Collaborator

    main 验收结论

    #33162 合入后的 main 已通过原始 ENOMEM 复现及新增盘缓存读取回归,按 main 的文件读取分配失败应返回错误而不是 panic 范围关闭本 Issue。依照本轮验收约定,修复尚未合入的 4.2-dev 不再作为本次关闭前置;这里不声称 4.2-dev 已修复。

    精确版本与方法

    覆盖的实际断言

    1. 原始文件读取:已有 1 byte buffer 时不调用分配器并成功读回 x;需要新 buffer 时分配器恰好调用一次,返回可 errors.Is(...,syscall.ENOMEM) 匹配的错误,entry.done=false,不再 panic。
    2. 分配状态回滚与缓冲区复用:覆盖 TestIOEntryReadBufferAllocation、TestLocalFSReadBufferAllocationRollback、TestLocalFSReadPublishesReusedDataOnSuccess;成功、短读和转换错误均保持相应状态与资源所有权。
    3. 盘缓存 ENOMEM 边界:TestS3FSReadDiskCacheAllocationFailure 的 range 和 full 两个子测试均通过;分配失败保留 ENOMEM,不把它当普通 cache miss 再去对象存储分配。
    4. 普通短读仍可回退:TestS3FSReadDiskCacheShortReadFallsBack 通过,确认修复没有把普通缓存短读也变成硬失败。
    5. 混合批部分命中与活动下载:TestDiskCacheAllocationFailureKeepsEarlierHitOwned、TestDiskCacheDownloadAllocationFailure 通过,覆盖前序命中后的 ENOMEM 清理、同向量重试所有权及活动下载 reader pin 释放。

    范围边界

    本关闭依据是确定性的文件读取/盘缓存 ENOMEM 注入和生产 fileservice 代码路径。没有复现 Linux CN 的 65,530 VMA 耗尽全过程,也没有把该测试写成映射增长来源已查明、生产压力或容器重启专项已通过;这些不属于本次 main 文件读取错误处理关闭结论。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ai-easykind/bugneeds-triageseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions