Skip to content

[refactor][P3] pack.Writer godoc 声称「不是并发安全的」,实现实际全程持锁串行化——文档失实(反向) #387

Closed

Description

@zy84338719

来源:2026-11-11 全矩阵深度代码审查(6 路并行扫描约 24 万行 Go)。判定已复核;行号基于 2026-11-11 的 main,动手前请核对现场。
归档索引:#372(本仓重构 backlog)

位置

pkg/pack/pack.go:34(对照 :36、:92-93、:156-157,已人工复核)

证据

// Writer 不是并发安全的;一个包由一个 Writer 顺序写出。
type Writer struct {
    mu       sync.Mutex

AddJSONL(:92-93 w.mu.Lock(); defer w.mu.Unlock())与 Close(:156-157 同)全部互斥,w.closed/w.err/w.counts/w.files 均在锁内读写——并发调用 AddJSONL 是安全的(只是条目顺序不保证)。文档低估了保证:调用方会包一层多余的外部锁;两处声明矛盾会让读者怀疑哪句是真的。

建议

二选一对齐——改 doc 为「AddJSONL/Close 可并发调用(内部互斥),条目落包顺序不保证」;或确认无意支持并发则删掉 mu(当前锁没有产生额外语义)。godoc 是契约,与实现必须同向。

Activity

  1. zy84338719 commented on Nov 12, 2026

    @zy84338719
    ContributorAuthor

    已合入修复 ✅(无 SemVer 发布:本次改动不发版本)

    变更内容

    修复已随 PR #421 合入 main(commit bee09a3):pack.Writer godoc 改为如实声明「AddJSONL/Close 全程持内部互斥锁串行化,可并发调用(条目落包顺序不保证)」,并补 TestWriter_ConcurrentAddJSONL 在 -race 下证明并发安全面。同 PR 内 pack 构造期校验(#389)一并落地。SemVer 发版随发版车统一补(车道 L1 不动 CHANGELOG/不推 tag)。

    合入:PR #421(https://github.com/oekit/pkg/pull/421),merge commit 63917515,2026-11-12T17:10:03Z

    CI:link ✅ / Lint & Test / Test ✅ / Lint & Test / Lint ✅ / Zero Third-Party Deps ✅

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    refactor重构机会(代码审查产出)

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions