diff --git a/spanner/session.go b/spanner/session.go index 1ee56269d5b..d4ed725c453 100644 --- a/spanner/session.go +++ b/spanner/session.go @@ -25,6 +25,7 @@ import ( "math" "math/rand" "runtime/debug" + "strings" "sync" "time" @@ -1723,3 +1724,12 @@ func isSessionNotFoundError(err error) bool { } return false } + +// isClientClosing returns true if the given error is a +// `Connection is closing` error. +func isClientClosing(err error) bool { + if err == nil { + return false + } + return ErrCode(err) == codes.Canceled && strings.Contains(err.Error(), "the client connection is closing") +} diff --git a/spanner/transaction.go b/spanner/transaction.go index 4fec6d7efce..143aae1c4b0 100644 --- a/spanner/transaction.go +++ b/spanner/transaction.go @@ -779,7 +779,7 @@ func (t *ReadOnlyTransaction) release(err error) { sh := t.sh t.mu.Unlock() if sh != nil { // sh could be nil if t.acquire() fails. - if isSessionNotFoundError(err) { + if isSessionNotFoundError(err) || isClientClosing(err) { sh.destroy() } if t.singleUse {