-
Notifications
You must be signed in to change notification settings - Fork 773
bugfix: fix dfget panic when crossWrite is true #1411
Conversation
6da4fc7
to
48867fc
Compare
} | ||
|
||
// RawContent returns raw contents, | ||
// If the piece has wrapper, and the piece content will remove the head and tail. | ||
func (p *Piece) RawContent(noWrapper bool) *bytes.Buffer { | ||
contents := p.Content.Bytes() | ||
length := len(contents) | ||
defer func() { | ||
if p.autoReset { | ||
p.ResetContent() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not invoke TryResetContent
here to automatically release buffer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not invoke
TryResetContent
here to automatically release buffer?
we should release the buffer after the io.Copy() finishes, otherwise the same buffer may be reused for writing at the same time. #1415
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about to provide a method WriteTo
in piece
struct to write the raw content to writer and then release the buffer if necessary, just like :
n, e := piece.WriteTo(writer, noWrapper)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's a good idea,I will try it.
@@ -220,6 +221,7 @@ func (cw *ClientWriter) write(piece *Piece) error { | |||
} | |||
|
|||
if cw.acrossWrite { | |||
atomic.AddInt32(&piece.writerNum, 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to abstract this line as a method of struct Piece
, such as Piece.IncWriter
.
48867fc
to
558bddf
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
} | ||
if length >= 5 { | ||
return bytes.NewBuffer(contents[4 : length-1]).WriteTo(w) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here could reuse p.RawContent(noWrapper)
to get buffer.
558bddf
to
06676b5
Compare
please rebase your code from master to solve the CI problem |
…ile. Signed-off-by: zhouchencheng <[email protected]>
06676b5
to
c6928f8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
bugfix: fix dfget panic when crossWrite is true
Signed-off-by: zhouchencheng [email protected]
Ⅰ. Describe what this PR did
when dfget's
--output
and--home
directories are mounted under different disk device, it makes the hard link between them fails to create. At that time,crossWrite
will be true.And the same piece will be written to different files twice. Currently the
autoreset
field in piece is always true, so the piece buffer will reset after the first written, then the second written will trigger panic.Ⅱ. Does this pull request fix one issue?
fixes #1410
Ⅲ. Why don't you add test cases (unit test/integration test)? (你真的觉得不需要加测试吗?)
Ⅳ. Describe how to verify it
Ⅴ. Special notes for reviews