Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mem.ReadAt/mem.WriteAt violates spec and subject to race conditions #433

Open
k3an3 opened this issue Nov 20, 2024 · 0 comments
Open

mem.ReadAt/mem.WriteAt violates spec and subject to race conditions #433

k3an3 opened this issue Nov 20, 2024 · 0 comments

Comments

@k3an3
Copy link

k3an3 commented Nov 20, 2024

Note in the Go 1.23.3 docs for the io.WriterAt interface:

If WriteAt is writing to a destination with a seek offset, WriteAt should not affect nor be affected by the underlying seek offset.

Clients of WriteAt can execute parallel WriteAt calls on the same destination if the ranges do not overlap.

However, the current implementations of MemFS ReadAt/WriteAt look like this, respectively:

afero/mem/file.go

Lines 226 to 232 in 5c4385a

func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
prev := atomic.LoadInt64(&f.at)
atomic.StoreInt64(&f.at, off)
n, err = f.Read(b)
atomic.StoreInt64(&f.at, prev)
return
}

afero/mem/file.go

Lines 300 to 303 in 5c4385a

func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
atomic.StoreInt64(&f.at, off)
return f.Write(b)
}

If multiple calls are made in parallel on the same file, a race will occur between modifying the stored file offset and reading/writing the file, causing these functions to return or write incorrect data. Additionally, it is not expected that the underlying seek offset is modified at all. A corrected implementation will likely have to extract and re-implement parts of Read/Write in order to not affect the saved offset.

@k3an3 k3an3 changed the title mem.ReadAt/mem.WriteAt` violates spec and subject to race conditions mem.ReadAt/mem.WriteAt violates spec and subject to race conditions Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant