Skip to content

Commit 03daa02

Browse files
authored
fix: small race fixes (#106)
* fix: protect providersCount with mutex to avoid race condition prevents concurrent access to shared counter when processing providers from DHT and IPNI * fix: respect context timeout in provider discovery loop adds context.Done() case to select statement to honor the timeout from URL parameters (5-300 seconds)
1 parent b68c284 commit 03daa02

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

daemon.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ func (d *daemon) runCidCheck(ctx context.Context, cidKey cid.Cid, ipniURL string
164164
var source string
165165

166166
select {
167+
case <-ctx.Done():
168+
// Respect the timeout from the URL/context
169+
done = true
170+
continue
167171
case provider, open = <-dhtProvsCh:
168172
if !open {
169173
dhtProvsCh = nil
@@ -183,10 +187,14 @@ func (d *daemon) runCidCheck(ctx context.Context, cidKey cid.Cid, ipniURL string
183187
}
184188
source = ipniSource
185189
}
190+
191+
// Protect providersCount with mutex to avoid race condition
192+
mu.Lock()
186193
providersCount++
187194
if providersCount == maxProvidersCount {
188195
done = true
189196
}
197+
mu.Unlock()
190198

191199
wg.Add(1)
192200
go func(provider peer.AddrInfo, src string) {

0 commit comments

Comments
 (0)