Skip to content

[Bug] panic: assignment to entry in nil map in sanitizeNonFiniteFloatValue (TPCC warehouse=1000) #26175

Closed

Description

@Ariznawlll

Bug Report

Summary

TPCC warehouse=1000 测试期间,CN 节点触发 panic: assignment to entry in nil map,发生在 pkg/frontend/mysql_cmd_executor.go 的 sanitizeNonFiniteFloatValue 函数中。

CI

Panic Stack Trace

panic: assignment to entry in nil map

runtime.mapassign
    /usr/local/go/src/internal/runtime/maps/runtime.go:208
reflect.mapassign0
    /usr/local/go/src/runtime/map.go:254
reflect.mapassign
    /usr/local/go/src/reflect/value.go:3746
reflect.Value.SetMapIndex
    /usr/local/go/src/reflect/map.go:439
github.com/matrixorigin/matrixone/pkg/frontend.sanitizeNonFiniteFloatValue
    /go/src/github.com/matrixorigin/matrixone/pkg/frontend/mysql_cmd_executor.go:5402   ← panic here
github.com/matrixorigin/matrixone/pkg/frontend.sanitizeNonFiniteFloatValue
    /go/src/github.com/matrixorigin/matrixone/pkg/frontend/mysql_cmd_executor.go:5384   ← Struct recurse
github.com/matrixorigin/matrixone/pkg/frontend.sanitizeNonFiniteFloatValue
    /go/src/github.com/matrixorigin/matrixone/pkg/frontend/mysql_cmd_executor.go:5381   ← Ptr recurse
github.com/matrixorigin/matrixone/pkg/frontend.sanitizeNonFiniteFloatValue
    /go/src/github.com/matrixorigin/matrixone/pkg/frontend/mysql_cmd_executor.go:5388   ← Slice recurse
    ... (deeply nested recursive traversal)

调用路径:Struct → Ptr → Struct → Ptr → Slice → Struct → ... → Map → SetMapIndex → panic

问题分析

sanitizeNonFiniteFloatValue 递归遍历数据结构,将 NaN/Inf float 值替换为 0。在 Map 分支中:

case reflect.Map:
    if v.IsNil() {       // ← nil check
        return
    }
    for _, key := range v.MapKeys() {
        value := v.MapIndex(key)
        ...
        v.SetMapIndex(key, copied)   // ← line 5402: panics here
    }

有 v.IsNil() 检查但 SetMapIndex 仍然 panic,可能原因:

  1. 并发修改:另一个 goroutine 在遍历过程中修改了数据结构中的 map(设为 nil 或替换)
  2. 不可寻址的 map:map 作为 struct 的值字段(非指针),通过 reflect 获取时不可设置
  3. 嵌套 map 中的 nil:递归深入嵌套结构时遇到未初始化的内层 map

复现性

Run Commit 时间 nil map panic
07-25 main ❌ 36f8b76 06:59:59 有
07-23 main ✅ f0ac2c9 全天 无

07-23 完全没有这个 panic。说明是 f0ac2c9bf..36f8b76c3 之间的某个 commit 改变了被遍历的数据结构(query plan/stats),引入了 nil map 字段。

影响

  • 此 panic 被 export/batch_processor.go:180 的 recover 捕获,没有导致 CN crash
  • 但说明 sanitizeNonFiniteFloatValue 存在并发安全或防御性编程缺陷
  • TPCC 测试本身的失败原因是 context canceled(另一个问题),nil map panic 发生在之后(06:59:59 vs 06:59:05)

建议修复

  1. 加防御检查:在 SetMapIndex 前加 v.CanSet() 检查
  2. 加锁或拷贝:遍历前对数据结构做 deep copy,或在遍历时加读锁
  3. nil map 容错:在 SetMapIndex 前再次检查 map 是否仍为 settable
case reflect.Map:
    if v.IsNil() || !v.CanSet() {
        return
    }
    // deep copy keys to avoid concurrent modification
    keys := v.MapKeys()
    for _, key := range keys {
        value := v.MapIndex(key)
        if !value.IsValid() {
            continue
        }
        copied := reflect.New(value.Type()).Elem()
        copied.Set(value)
        sanitizeNonFiniteFloatValue(copied, seen)
        if v.CanSet() {
            v.SetMapIndex(key, copied)
        }
    }

Activity

  1. added
    kind/bug
    on Jul 25, 2026
  2. added theissue type on Jul 25, 2026
  3. added this to the MO-202607 milestone on Jul 25, 2026
  4. XuPeng-SH commented on Jul 25, 2026

    @XuPeng-SH
    Contributor

    Root cause is now identified.

    The introducing change is #25843 (b8a190a10ce, merged 2026-07-23), not #24643 alone. #25843 changed NewJsonPlanHandler from eagerly calling h.Marshal(ctx) during RecordExecPlan to retaining marshalHandler and serializing later in asynchronous statement export.

    ExplainData still shallow-copies PhyPlan, so PhyOperator.OpStats remains shared with the compile graph. The execution path calls RecordExecPlan and then Compile.Release(); release/reuse can reset OperatorStats (including mutable maps) before deferred Marshal runs. The sanitizer added in #24643 then mutates a stale/shared map via SetMapIndex; under TPCC concurrency, reset/reuse between IsNil and SetMapIndex produces assignment to entry in nil map.

    Why it appeared only now: #24643 had existed with eager serialization before Compile.Release; #25843 changed the object lifetime/timing and exposed the race.

    Fix direction: deep-copy PhyPlan before retaining it for deferred marshal, including OperatorStats maps and BackgroundQueries, and add a regression test that releases/resets the original plan before deferred Marshal.

    OperatorMetrics is the most likely map involved, but the retained CI artifacts do not prove the exact map field; the ownership/lifetime root cause is confirmed.

  5. added a commit that references this issue on Jul 26, 2026
    e5fffff
  6. XuPeng-SH commented on Jul 26, 2026

    @XuPeng-SH
    Contributor

    should be fixed

  7. Ariznawlll commented on Jul 26, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试中

  8. changed the title [-]panic: assignment to entry in nil map in sanitizeNonFiniteFloatValue (TPCC warehouse=1000)[/-] [+][Bug] panic: assignment to entry in nil map in sanitizeNonFiniteFloatValue (TPCC warehouse=1000)[/+] on Jul 27, 2026
  9. Ariznawlll commented on Jul 28, 2026

    @Ariznawlll
    CollaboratorAuthor

    验证结果

    #26177 已于 07-26 合入,修复后最近 3 次 main nightly TPCC warehouse=1000 均通过:

    Run 日期 TPCC nil map panic
    30289644679 07-28 ✅ 0 次
    30212370851 07-27 ✅ 0 次
    30167415937 07-26 ✅ 0 次

    Panic 已不再出现,可以 close 了。

  10. added 6 commits that reference this issue on Sep 17, 2026
    070ea35
    8dcc712
    03b93fe
    202d08e
    9bd4723
    3f22fc3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions