Skip to content

Commit fd457b7

Browse files
author
Mathieu Borderé
authored
Merge pull request #186 from MathieuBordere/detect-EOF
driver: Detect EOF in driverError
2 parents 407917c + 1a24816 commit fd457b7

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

driver/driver.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ func (c *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, e
353353
if c.tracing != client.LogNone {
354354
start = time.Now()
355355
}
356-
err := c.protocol.Call(ctx, &c.request, &c.response);
356+
err := c.protocol.Call(ctx, &c.request, &c.response)
357357
if c.tracing != client.LogNone {
358358
c.log(c.tracing, "%.3fs request prepared: %q", time.Since(start).Seconds(), query)
359359
}
@@ -392,7 +392,7 @@ func (c *Conn) ExecContext(ctx context.Context, query string, args []driver.Name
392392
if c.tracing != client.LogNone {
393393
start = time.Now()
394394
}
395-
err := c.protocol.Call(ctx, &c.request, &c.response);
395+
err := c.protocol.Call(ctx, &c.request, &c.response)
396396
if c.tracing != client.LogNone {
397397
c.log(c.tracing, "%.3fs request exec: %q", time.Since(start).Seconds(), query)
398398
}
@@ -428,7 +428,7 @@ func (c *Conn) QueryContext(ctx context.Context, query string, args []driver.Nam
428428
if c.tracing != client.LogNone {
429429
start = time.Now()
430430
}
431-
err := c.protocol.Call(ctx, &c.request, &c.response);
431+
err := c.protocol.Call(ctx, &c.request, &c.response)
432432
if c.tracing != client.LogNone {
433433
c.log(c.tracing, "%.3fs request query: %q", time.Since(start).Seconds(), query)
434434
}
@@ -588,7 +588,7 @@ func (s *Stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (drive
588588
if s.tracing != client.LogNone {
589589
start = time.Now()
590590
}
591-
err := s.protocol.Call(ctx, s.request, s.response);
591+
err := s.protocol.Call(ctx, s.request, s.response)
592592
if s.tracing != client.LogNone {
593593
s.log(s.tracing, "%.3fs request prepared: %q", time.Since(start).Seconds(), s.sql)
594594
}
@@ -627,7 +627,7 @@ func (s *Stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driv
627627
if s.tracing != client.LogNone {
628628
start = time.Now()
629629
}
630-
err := s.protocol.Call(ctx, s.request, s.response);
630+
err := s.protocol.Call(ctx, s.request, s.response)
631631
if s.tracing != client.LogNone {
632632
s.log(s.tracing, "%.3fs request prepared: %q", time.Since(start).Seconds(), s.sql)
633633
}
@@ -787,6 +787,12 @@ type unwrappable interface {
787787
Unwrap() error
788788
}
789789

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

0 commit comments

Comments
 (0)