2
0

Fix create resource concurrently with Stat call race

https://github.com/jackc/pgx/issues/1148#issuecomment-1304898720
This commit is contained in:
Jack Christensen
2022-11-11 18:55:27 -06:00
parent eca38babce
commit 2f17b3e259
2 changed files with 9 additions and 0 deletions
+4
View File
@@ -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 {
+5
View File
@@ -985,6 +985,11 @@ func TestStress(t *testing.T) {
res.Release()
}
},
// Stat
func() {
stat := pool.Stat()
assert.NotNil(t, stat)
},
}
workerCount := int(poolSize) * 2