From 380d04cf952ed531fe640ce589c0d9be73ef322d Mon Sep 17 00:00:00 2001 From: Rahul Yadav Date: Tue, 20 Sep 2022 19:35:42 +0530 Subject: [PATCH 1/2] fix(spanner): destroy session when client is closing. --- spanner/transaction.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spanner/transaction.go b/spanner/transaction.go index 4fec6d7efce..63ea11a9e63 100644 --- a/spanner/transaction.go +++ b/spanner/transaction.go @@ -18,6 +18,7 @@ package spanner import ( "context" + "strings" "sync" "sync/atomic" "time" @@ -779,7 +780,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) || strings.Contains(err.Error(), "the client connection is closing") { sh.destroy() } if t.singleUse { From c922149a5a868b667d750eac0bccc3a1dc2347db Mon Sep 17 00:00:00 2001 From: Rahul Yadav Date: Tue, 20 Sep 2022 20:09:35 +0530 Subject: [PATCH 2/2] fix test --- spanner/session.go | 10 ++++++++++ spanner/transaction.go | 3 +-- 2 files changed, 11 insertions(+), 2 deletions(-) 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 63ea11a9e63..143aae1c4b0 100644 --- a/spanner/transaction.go +++ b/spanner/transaction.go @@ -18,7 +18,6 @@ package spanner import ( "context" - "strings" "sync" "sync/atomic" "time" @@ -780,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) || strings.Contains(err.Error(), "the client connection is closing") { + if isSessionNotFoundError(err) || isClientClosing(err) { sh.destroy() } if t.singleUse {