Skip to content

bug: pool.closeItem cancels context before calling closeItem() #1835

Open
@kprokopenko

Description

@kprokopenko

Bug Report

YDB GO SDK version:

3.112.0

Environment

go 1.24.5 MacOS

Current behavior:

If closeTimeout != 0 (default), context canceled before closeItem was called.

Expected behavior:

closeItem() executed with non-canceled context.

Steps to reproduce:

Related code:

func (p *Pool[PT, T]) closeItem(ctx context.Context, item PT, opts ...closeItemOption) {
	// ...

	if t := p.config.closeTimeout; t > 0 {
		var cancel context.CancelFunc
		ctx, cancel = context.WithTimeout(ctx, t)
		defer cancel() // executed on exit
	}

	if options.wg != nil {
		options.wg.Add(1)

                 // wg.Wait() in the separate method, so current goroutine exited, and cancel() was invoked
		go func() {
			defer options.wg.Done()

			p.config.closeItemFunc(ctx, item)
		}()
	} else {
		p.config.closeItemFunc(ctx, item)
	}
}

if t := p.config.closeTimeout; t > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, t)
defer cancel()
}
if options.wg != nil {
options.wg.Add(1)
go func() {
defer options.wg.Done()
p.config.closeItemFunc(ctx, item)
}()
} else {
p.config.closeItemFunc(ctx, item)
}
}

Other information:

We missed this bug because we weren't checking for errors on session close.

closeItemFunc: func(ctx context.Context, item PT) {
_ = item.Close(ctx)
},

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions