Fix create resource concurrently with Stat call race
https://github.com/jackc/pgx/issues/1148#issuecomment-1304898720
This commit is contained in:
@@ -427,8 +427,12 @@ func (p *Pool[T]) initResourceValue(ctx context.Context, res *Resource[T]) (*Res
|
||||
return
|
||||
}
|
||||
|
||||
// The resource is already in p.allResources where it might be read. So we need to acquire the lock to update its
|
||||
// status.
|
||||
p.mux.Lock()
|
||||
res.value = value
|
||||
res.status = resourceStatusAcquired
|
||||
p.mux.Unlock()
|
||||
|
||||
// This select works because the channel is unbuffered.
|
||||
select {
|
||||
|
||||
@@ -985,6 +985,11 @@ func TestStress(t *testing.T) {
|
||||
res.Release()
|
||||
}
|
||||
},
|
||||
// Stat
|
||||
func() {
|
||||
stat := pool.Stat()
|
||||
assert.NotNil(t, stat)
|
||||
},
|
||||
}
|
||||
|
||||
workerCount := int(poolSize) * 2
|
||||
|
||||
Reference in New Issue
Block a user