Skip to content

Commit

Permalink
Merge pull request #186 from MathieuBordere/detect-EOF
Browse files Browse the repository at this point in the history
driver: Detect EOF in driverError
  • Loading branch information
Mathieu Borderé authored May 31, 2023
2 parents 407917c + 1a24816 commit fd457b7
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func (c *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, e
if c.tracing != client.LogNone {
start = time.Now()
}
err := c.protocol.Call(ctx, &c.request, &c.response);
err := c.protocol.Call(ctx, &c.request, &c.response)
if c.tracing != client.LogNone {
c.log(c.tracing, "%.3fs request prepared: %q", time.Since(start).Seconds(), query)
}
Expand Down Expand Up @@ -392,7 +392,7 @@ func (c *Conn) ExecContext(ctx context.Context, query string, args []driver.Name
if c.tracing != client.LogNone {
start = time.Now()
}
err := c.protocol.Call(ctx, &c.request, &c.response);
err := c.protocol.Call(ctx, &c.request, &c.response)
if c.tracing != client.LogNone {
c.log(c.tracing, "%.3fs request exec: %q", time.Since(start).Seconds(), query)
}
Expand Down Expand Up @@ -428,7 +428,7 @@ func (c *Conn) QueryContext(ctx context.Context, query string, args []driver.Nam
if c.tracing != client.LogNone {
start = time.Now()
}
err := c.protocol.Call(ctx, &c.request, &c.response);
err := c.protocol.Call(ctx, &c.request, &c.response)
if c.tracing != client.LogNone {
c.log(c.tracing, "%.3fs request query: %q", time.Since(start).Seconds(), query)
}
Expand Down Expand Up @@ -588,7 +588,7 @@ func (s *Stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (drive
if s.tracing != client.LogNone {
start = time.Now()
}
err := s.protocol.Call(ctx, s.request, s.response);
err := s.protocol.Call(ctx, s.request, s.response)
if s.tracing != client.LogNone {
s.log(s.tracing, "%.3fs request prepared: %q", time.Since(start).Seconds(), s.sql)
}
Expand Down Expand Up @@ -627,7 +627,7 @@ func (s *Stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driv
if s.tracing != client.LogNone {
start = time.Now()
}
err := s.protocol.Call(ctx, s.request, s.response);
err := s.protocol.Call(ctx, s.request, s.response)
if s.tracing != client.LogNone {
s.log(s.tracing, "%.3fs request prepared: %q", time.Since(start).Seconds(), s.sql)
}
Expand Down Expand Up @@ -787,6 +787,12 @@ type unwrappable interface {
Unwrap() error
}

// TODO driver.ErrBadConn should not be returned when there's a possibility that
// the query has been executed. In our case there is a window in protocol.Call
// between `send` and `recv` where the send has succeeded but the recv has
// failed. In those cases we call driverError on the result of protocol.Call,
// possibly returning ErrBadCon.
// https://cs.opensource.google/go/go/+/refs/tags/go1.20.4:src/database/sql/driver/driver.go;drc=a32a592c8c14927c20ac42808e1fb2e55b2e9470;l=162
func driverError(log client.LogFunc, err error) error {
switch err := errors.Cause(err).(type) {
case syscall.Errno:
Expand Down Expand Up @@ -837,5 +843,9 @@ func driverError(log client.LogFunc, err error) error {
return driver.ErrBadConn
}
}
if errors.Is(err, io.EOF) {
log(client.LogDebug, "EOF detected: %v", err)
return driver.ErrBadConn
}
return err
}

0 comments on commit fd457b7

Please sign in to comment.