Skip to content

Commit 183268a

Browse files
committed
Make read after close stateful.
1 parent d8c4cd0 commit 183268a

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

reader.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ func (a *reader) fill() (err error) {
250250

251251
// Read will return the next available data.
252252
func (a *reader) Read(p []byte) (n int, err error) {
253+
if a.err != nil {
254+
return 0, a.err
255+
}
253256
// Swap buffer and maybe return error
254257
err = a.fill()
255258
if err != nil {
@@ -300,6 +303,9 @@ func (a *seekable) Seek(offset int64, whence int) (res int64, err error) {
300303
// The return value n is the number of bytes written.
301304
// Any error encountered during the write is also returned.
302305
func (a *reader) WriteTo(w io.Writer) (n int64, err error) {
306+
if a.err != nil {
307+
return 0, a.err
308+
}
303309
n = 0
304310
for {
305311
err = a.fill()
@@ -338,6 +344,7 @@ func (a *reader) Close() (err error) {
338344
a.closer = nil
339345
return c.Close()
340346
}
347+
a.err = errors.New("readahead: read after Close")
341348
return nil
342349
}
343350

reader_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,11 @@ func TestWriteTo(t *testing.T) {
334334
if err != nil {
335335
t.Fatal("error when closing:", err)
336336
}
337+
// Test Read after close
338+
_, err = io.Copy(dst, ar)
339+
if err == nil {
340+
t.Fatal("want error when closing, got:", err)
341+
}
337342
}
338343

339344
func TestNilReader(t *testing.T) {

0 commit comments

Comments
 (0)