Skip to content

Commit b345596

Browse files
authored
Fix context cancelled during clean-up GOODBYE (#640)
* Make sure that the pool's asynchronous connection closure (`GOODBYE`) does not get cancelled when the acquisition of return of a connection that triggered the clean-up ends (and thus cancels the parent context). * Lower log level from error to debug on errors occurring during connection closure or when the connection is already dead.
1 parent 551a395 commit b345596

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

neo4j/internal/bolt/bolt4.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ func (b *bolt4) setError(err error, fatal bool) {
189189
return
190190
}
191191

192+
wasDead := b.state == bolt5Dead
192193
// No previous error
193194
if b.err == nil {
194195
b.err = err
@@ -213,6 +214,8 @@ func (b *bolt4) setError(err error, fatal bool) {
213214
neo4jErr, casted := err.(*db.Neo4jError)
214215
if casted && neo4jErr.Classification() == "ClientError" {
215216
b.log.Debugf(log.Bolt4, b.logId, "%s", err)
217+
} else if wasDead {
218+
b.log.Debugf(log.Bolt5, b.logId, "Already broken connection: %s", err)
216219
} else {
217220
b.log.Error(log.Bolt4, b.logId, err)
218221
}

neo4j/internal/bolt/bolt5.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ func (b *bolt5) setError(err error, fatal bool) {
200200
return
201201
}
202202

203+
wasDead := b.state == bolt5Dead
203204
// No previous error
204205
if b.err == nil {
205206
b.err = err
@@ -224,6 +225,8 @@ func (b *bolt5) setError(err error, fatal bool) {
224225
neo4jErr, casted := err.(*db.Neo4jError)
225226
if casted && neo4jErr.Classification() == "ClientError" {
226227
b.log.Debugf(log.Bolt5, b.logId, "%s", err)
228+
} else if wasDead {
229+
b.log.Debugf(log.Bolt5, b.logId, "Already broken connection: %s", err)
227230
} else {
228231
b.log.Error(log.Bolt5, b.logId, err)
229232
}

neo4j/internal/pool/pool.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,20 @@ func (p *Pool) removeIdleOlderThanLocked(ctx context.Context, s *server, now tim
411411
func (p *Pool) closeConnection(ctx context.Context, c idb.Connection) {
412412
p.ssrTracker.removeConnection(c)
413413
// Close connection in another thread to avoid potential long blocking operation during close.
414-
go c.Close(ctx)
414+
go func() {
415+
ctx, cancelFunc := context.WithTimeout(context.Background(), 30*time.Second)
416+
defer cancelFunc()
417+
c.Close(ctx)
418+
if ctxErr := ctx.Err(); ctxErr != nil {
419+
p.log.Debugf(
420+
log.Pool,
421+
p.logId,
422+
"Connection %s timed out during graceful shutdown: %s",
423+
c.ConnId(),
424+
ctxErr,
425+
)
426+
}
427+
}()
415428
}
416429

417430
func (p *Pool) Return(ctx context.Context, c idb.Connection) {

0 commit comments

Comments
 (0)