Properly abort CopyFrom on reader error
This commit is contained in:
@@ -876,11 +876,10 @@ func (pgConn *PgConn) CopyFrom(ctx context.Context, r io.Reader, sql string) (Co
|
||||
buf = make([]byte, 0, 65536)
|
||||
buf = append(buf, 'd')
|
||||
sp := len(buf)
|
||||
for {
|
||||
n, err := r.Read(buf[5:cap(buf)])
|
||||
if err == io.EOF && n == 0 {
|
||||
break
|
||||
}
|
||||
var readErr error
|
||||
for readErr == nil {
|
||||
n, readErr = r.Read(buf[5:cap(buf)])
|
||||
if n > 0 {
|
||||
buf = buf[0 : n+5]
|
||||
pgio.SetInt32(buf[sp:], int32(n+4))
|
||||
|
||||
@@ -898,12 +897,16 @@ func (pgConn *PgConn) CopyFrom(ctx context.Context, r io.Reader, sql string) (Co
|
||||
return "", preferContextOverNetTimeoutError(ctx, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Send copy done
|
||||
buf = buf[:0]
|
||||
if readErr == io.EOF {
|
||||
copyDone := &pgproto3.CopyDone{}
|
||||
buf = copyDone.Encode(buf)
|
||||
|
||||
} else {
|
||||
copyFail := &pgproto3.CopyFail{Error: readErr.Error()}
|
||||
buf = copyFail.Encode(buf)
|
||||
}
|
||||
_, err = pgConn.conn.Write(buf)
|
||||
if err != nil {
|
||||
pgConn.conn.Close()
|
||||
|
||||
Reference in New Issue
Block a user