fix(gsession): atomic write in SetSession to prevent concurrent read race (#4792) - #4814
Open
waterWang wants to merge 2 commits into
Open
fix(gsession): atomic write in SetSession to prevent concurrent read race (#4792)#4814waterWang wants to merge 2 commits into
waterWang wants to merge 2 commits into
Conversation
…f#4787) When Result.Structs / Model.Scan is called with a *[]map[string]interface{} pointer, the destination element type is reflect.Map, but converter.Structs routes it through c.Struct which expects a struct type. This causes GetCachedStructInfo to return nil for non-struct types, silently producing empty maps. Fix: add a reflect.Map branch that calls c.MapToMap for each element instead of c.Struct. This matches the behavior of gconv.Scan which already handles map-slice conversion via MapToMaps.
| return err | ||
| // Build content with 8-byte timestamp prefix (same format as before). | ||
| timestamp := gbinary.EncodeInt64(gtime.TimestampMilli()) | ||
| data := make([]byte, 8+len(content)) |
| // Atomic write: write to temp file first, then rename. | ||
| // This prevents concurrent readers from seeing an empty/partial file. | ||
| tmpPath := path + ".tmp" | ||
| if err := os.WriteFile(tmpPath, data, os.ModePerm); err != nil { |
| if _, err = file.Write(content); err != nil { | ||
| err = gerror.Wrapf(err, `write data failed to file "%s"`, path) | ||
| return err | ||
| if err := os.Rename(tmpPath, path); err != nil { |
| if _, err = file.Write(content); err != nil { | ||
| err = gerror.Wrapf(err, `write data failed to file "%s"`, path) | ||
| return err | ||
| if err := os.Rename(tmpPath, path); err != nil { |
| err = gerror.Wrapf(err, `write data failed to file "%s"`, path) | ||
| return err | ||
| if err := os.Rename(tmpPath, path); err != nil { | ||
| os.Remove(tmpPath) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: Atomic write in
SetSessionto prevent concurrent read raceDescription
StorageFile.SetSessionuses a non-atomic file write pattern (open withO_TRUNCthen two separateWritecalls), creating a race window where concurrent readers see an empty or partially-written file.Root Cause
The write path:
O_TRUNC— immediately empties itfile.Write(timestamp)— writes 8 bytesfile.Write(content)— writes JSON bodyBetween steps 1 and 3, any concurrent reader calling
GetSessionsees a file with ≤8 bytes and returnsnil— the user appears "not logged in".Fix
Replace the
O_TRUNC+ twoWrite()calls with atomic write via temp file +os.Rename():.tmpfileos.Rename()the temp file to the target pathos.Rename()is atomic on the same filesystem (POSIX guarantee), so readers always see a complete file.Testing
os/gsession/gsession_z_unit_storage_file_test.gocover the session lifecycleos.Renameis used elsewhere in the Go ecosystem for atomic file writes (e.g.,os.WriteFileitself is atomic on most systems, but we use temp+rename for explicit POSIX atomicity)Fixes #4792