Repository navigation
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #4977 +/- ##
==========================================
- Coverage 85.31% 85.30% -0.01%
==========================================
Files 894 894
Lines 47196 47197 +1
==========================================
- Hits 40263 40260 -3
- Misses 6933 6937 +4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
e9f8021 to
d75dbf6
Compare
…x platforms updates gogf#4792 gogf#4910 fixed the race on Unix with temp-file+rename. On non-Unix platforms (Windows) os.Rename is blocked by any open reader handle, so the atomic path was not taken there, and the original O_TRUNC + two-Write implementation was kept as-is. This patch closes the remaining gap without changing the platform split: gsession_storage_file.go - Add a sync.RWMutex field to StorageFile, used only by the !unix path. - GetSession now calls readSessionBytes (platform method) instead of gfile.GetBytes directly. gsession_storage_file_nonunix.go (!unix — includes Windows) - setSessionFile: concatenates the 8-byte timestamp header and the JSON payload in memory, then writes the complete buffer in a single file.Write call under the storage write lock. The O_TRUNC-to-Write gap that allowed concurrent readers to see an empty file is eliminated. - readSessionBytes: holds the read lock around gfile.GetBytes so that a read can never overlap an in-progress truncate+write. gsession_storage_file_unix.go (unix — no behaviour change) - Add readSessionBytes stub that delegates to gfile.GetBytes; on Unix the rename path makes a lock unnecessary. gsession_z_unit_storage_file_windows_test.go - Add Test_Issue4792_WindowsConcurrentReadWrite: two writers and four readers share the same session ID for two seconds; asserts zero nil reads. Before this fix: ~700-1000 nil reads in a 2-second run. After: 0. Existing Test_StorageFileSharedReader confirms that an open reader handle does not block SetSession — the write lock is released before the caller returns.
d75dbf6 to
0b21217
Compare
|
The non-Unix
I have not added the |
hailaz
left a comment
There was a problem hiding this comment.
Review / 评审意见
Thanks for closing the remaining gap on this — combining the timestamp and payload into a single Write and guarding it with a read/write lock is the right shape for platforms where os.Rename cannot be atomic. A few points before this can land.
感谢补齐这块遗留缺口——把时间戳和 payload 合并成一次 Write 并用读写锁保护,对于 os.Rename 无法保证原子的平台方向是对的。合并前有几点需要处理。
1. The mutex does not cover all writers (correctness) / 锁没有覆盖所有写入路径(正确性)
The header comment in gsession_storage_file_nonunix.go claims the lock serializes writers against readers, but updateSessionTTl still writes outside it:
gsession_storage_file_nonunix.go 的文件头注释声称该锁能序列化读写,但 updateSessionTTl 仍然绕过了它:
// os/gsession/gsession_storage_file.go
file, err := gfile.OpenWithFlag(path, os.O_WRONLY)
...
if _, err = file.WriteAt(gbinary.EncodeInt64(gtime.TimestampMilli()), 0); err != nil {An 8-byte WriteAt can still tear against a concurrent gfile.GetBytes. If the torn value happens to be small, GetSession treats the session as expired and returns nil, nil — exactly the "empty session read" this PR sets out to eliminate. Please route this path through the same lock (e.g. a small updateSessionTTLStamp(path) platform method), otherwise the invariant documented in the header does not hold.
8 字节的 WriteAt 在并发 gfile.GetBytes 下仍可能撕裂。若撕裂出的值刚好很小,GetSession 会把会话当成已过期并返回 nil, nil,这正是本 PR 想消除的"空会话读取"。请让该路径也走同一把锁(例如抽出一个 updateSessionTTLStamp(path) 平台方法),否则文件头声明的不变式不成立。
Scope note / 范围说明: mu 属于 *StorageFile 实例,两个指向同一目录的 storage 之间仍然不同步(Test_Issue4792 正是这种场景,net/ghttp/ghttp_server.go 也是每个 server 各自 NewStorageFile)。建议在描述里说明"仅保护同实例",避免被误读为对 #4792 的完整修复。
2. The new non-Unix code never runs in CI / 新增的非 Unix 代码在 CI 中零执行
.github/workflows/ci-main.yml is ubuntu-latest only, so gsession_storage_file_nonunix.go is never compiled or executed — the locking behaviour has no test coverage on any CI job. Suggest extracting the snapshot logic into build-tag-free helpers and letting the !unix methods delegate:
CI 只有 ubuntu-latest,gsession_storage_file_nonunix.go 从未被编译或执行,锁的行为在任何 CI job 上都没有覆盖。建议把快照读写抽成无 build tag 的公共函数,!unix 方法只做委托:
func (s *StorageFile) writeSessionSnapshotLocked(path string, content []byte) error
func (s *StorageFile) readSessionSnapshotLocked(path string) []byteThat makes the behaviour testable on Linux and keeps the repo's ">= 80% coverage for new code" rule satisfiable.
这样 Linux 上就能直接验证,也能满足仓库"新增代码覆盖率 ≥ 80%"的要求。
3. The Windows regression test hides the failure it targets / Windows 回归测试会掩盖它要抓的失败
// os/gsession/gsession_z_unit_storage_file_windows_test.go
got, err := storage.GetSession(ctx, sessionId, ttl)
if err == nil && got == nil {
atomic.AddInt64(&nilReads, 1)
}A torn read typically surfaces as a json.UnmarshalUseNumber error rather than nil, so err == nil && lets the test pass while the bug is still present. SetSession's error is also discarded by the writers, and total is incremented but never asserted — if every write fails, nilReads == 0 still succeeds.
撕裂读通常表现为 json.UnmarshalUseNumber 返回错误而不是 nil,err == nil && 会让 bug 存在时测试照样通过。此外写协程丢弃了 SetSession 的错误,total 只自增从未断言——即使所有写都失败,nilReads == 0 依然成立。
Please count err != nil as a failure as well, propagate write errors, and assert total > 0. The 2s time.Sleep can likely drop to a few hundred milliseconds without losing reproduction power.
建议把 err != nil 也算作失败、收集写错误并断言 total > 0;2 秒的 time.Sleep 应可缩短到几百毫秒而不影响复现。
4. Minor / 其他
Test_StorageFileSharedReader's comment still describes "the rename path falls back to direct write", which no longer exists on Windows. — 该测试注释描述的 rename 回退路径在 Windows 上已不存在。gsession_z_unit_storage_file_expiry_test.goputs//go:build !windowsafter the license header; other files in the package keep build tags on line 1. — build tag 位置与包内其它文件不一致。- The new
expiry/ttltests overlap withgsession_z_unit_storage_file_path_internal_test.go(lines 47, 64, 171) and do not touch the code this PR changes. Either drop them or state why they belong here. — 新增测试与既有测试重复,且未覆盖本 PR 改动的代码,建议移除或说明理由。
Verified locally / 本地验证:go test ./os/gsession/ -count=1 -race passes; GOOS=windows, plan9, js/wasm cross-builds all succeed; golangci-lint -c .golangci.yml ./os/gsession/... reports 0 issues.
Happy to re-review once the updateSessionTTl locking and the Windows test assertions are addressed.
处理完 updateSessionTTl 的加锁和 Windows 测试断言后,我很乐意再评审一次。
|
Thanks for keeping at the Windows side of #4792. Putting the timestamp and payload into one
I have not added the 感谢继续补 #4792 在 Windows 上的缺口。
这次还没有添加 |
updates #4792
#4910 fixed the race on Unix using temp-file + rename. On non-Unix platforms
(Windows),
os.Renameis blocked by any open reader handle, so the atomicpath was not safe there and the original
O_TRUNC+ two-Writeimplementationwas kept. This patch closes the remaining gap.
Measured before / after (Windows amd64, 2 s, 2 writers + 4 readers)
Changes
os/gsession/gsession_storage_file.gosync.RWMutextoStorageFile(used only by the!unixpath).GetSessioncallsreadSessionBytes(platform method) instead ofgfile.GetBytes.os/gsession/gsession_storage_file_nonunix.gosetSessionFile: concatenates the timestamp header and JSON payload inmemory, then writes the complete buffer in a single
file.Writecallunder the storage write lock. The
O_TRUNC-to-Writegap that letconcurrent readers observe an empty file is eliminated.
readSessionBytes: holds the read lock aroundgfile.GetBytesso a readcan never overlap an in-progress truncate+write.
os/gsession/gsession_storage_file_unix.goreadSessionBytesstub that delegates togfile.GetBytes; on Unixthe rename path makes a lock unnecessary.
os/gsession/gsession_z_unit_storage_file_windows_test.goTest_Issue4792_WindowsConcurrentReadWrite: two writers + four readerssharing the same session ID for two seconds; asserts zero nil reads.
Test_StorageFileSharedReader(existing) confirms an open reader handledoes not block
SetSession— the write lock is released before returning.