laskoviymishka commented on code in PR #2743:
URL: https://github.com/apache/iceberg-go/pull/2743#discussion_r4727490688


##########
io/mem.go:
##########
@@ -344,18 +344,43 @@ func (fi *memFileInfo) IsDir() bool        { return 
fi.isDir }
 func (fi *memFileInfo) Sys() any           { return nil }
 
 type memWriter struct {
-       fs   *MemFS
-       name string
-       ctx  context.Context
-       buf  bytes.Buffer
+       fs       *MemFS
+       name     string
+       ctx      context.Context
+       buf      bytes.Buffer
+       closed   bool

Review Comment:
   Should we add a short note on `memWriter` that it isn't safe for concurrent 
use? `closed` and `closeErr` are plain fields, so two goroutines calling Close 
can both pass the check and publish. At base two Closes only read `buf` and 
called the locked `WriteFile`, so the race on `closed` is new, though nothing 
in the repo closes a writer from two goroutines.



##########
io/mem.go:
##########
@@ -344,18 +344,43 @@ func (fi *memFileInfo) IsDir() bool        { return 
fi.isDir }
 func (fi *memFileInfo) Sys() any           { return nil }
 
 type memWriter struct {
-       fs   *MemFS
-       name string
-       ctx  context.Context
-       buf  bytes.Buffer
+       fs       *MemFS
+       name     string
+       ctx      context.Context
+       buf      bytes.Buffer
+       closed   bool
+       closeErr error
+}
+
+func (w *memWriter) Write(p []byte) (int, error) {
+       if w.closed {
+               return 0, &fs.PathError{Op: "write", Path: w.name, Err: 
fs.ErrClosed}
+       }
+
+       return w.buf.Write(p)
+}
+
+func (w *memWriter) ReadFrom(r io.Reader) (int64, error) {
+       if w.closed {
+               return 0, &fs.PathError{Op: "readfrom", Path: w.name, Err: 
fs.ErrClosed}
+       }
+
+       return w.buf.ReadFrom(r)
 }
 
-func (w *memWriter) Write(p []byte) (int, error)         { return 
w.buf.Write(p) }
-func (w *memWriter) ReadFrom(r io.Reader) (int64, error) { return 
w.buf.ReadFrom(r) }
 func (w *memWriter) Close() error {
+       if w.closed {
+               return w.closeErr
+       }
+       w.closed = true
+
        if err := w.ctx.Err(); err != nil {
+               w.closeErr = err
+
                return err
        }
 
-       return w.fs.WriteFile(w.ctx, w.name, w.buf.Bytes())
+       w.closeErr = w.fs.WriteFile(w.ctx, w.name, w.buf.Bytes())

Review Comment:
   Could we drop the buffer once it's published? `WriteFile` stores its own 
copy 
([mem.go#L103-L112](https://github.com/apache/iceberg-go/blob/c03bddaf614b142803065a4fc4fe9f8627b2c9f7/io/mem.go#L103-L112)),
 so a closed writer doesn't need to keep it alive. Optional.
   
   ```suggestion
        w.closeErr = w.fs.WriteFile(w.ctx, w.name, w.buf.Bytes())
        w.buf = bytes.Buffer{}
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to