Summarized from: https://discord.com/channels/973324189794697286/1526567789803081728/1527371027628298461

Follow up discussions: https://discord.com/channels/973324189794697286/1526567789803081728/1527683425874481272

Related PRs:

https://github.com/vacp2p/nim-libp2p/pull/2847

https://github.com/vacp2p/nim-libp2p/pull/2845

Condition triggering the issue:

Stack trace:

[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
__memcpy_avx_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:266
#0  __memcpy_avx_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:266
#1  0x00005bcf6cdad278 in genericSeqAssign ()
#2  0x00005bcf6d358dda in toAdvertisementKey__pkgZlibp2pZprotocolsZservice95discoveryZtypes_u191 ()
#3  0x00005bcf6d362e06 in pruneAdsForService__pkgZlibp2pZprotocolsZservice95discoveryZregistrar_u291 ()
#4  0x00005bcf6d36395c in pruneExpiredAds__pkgZlibp2pZprotocolsZservice95discoveryZregistrar_u3693 ()
#5  0x00005bcf6d379b22 in maintainRegistrar__pkgZlibp2pZprotocolsZservice95discovery_u911 ()
#6  0x00005bcf6ce578ee in futureContinue__pkgZchronosZinternalZasyncfutures_u570 ()
#7  0x00005bcf6ce52392 in poll__pkgZchronosZinternalZasyncengine_u8673 ()
#8  0x00005bcf6ce75f5d in waitFor__pkgZchronosZinternalZasyncfutures_u723 ()
#9  0x00005bcf6d395100 in NimMainInner ()
#10 0x00005bcf6d39f5ca in NimMain ()
#11 0x00005bcf6ca867a1 in main ()
[Inferior 1 (process 1444549) detached]

We were facing an infinite loop in this part of the code:

proc pruneAdsForService(
    registrar: Registrar,
    serviceId: ServiceId,
    ads: var seq[Advertisement],
    now: Moment,
    advertExpiry: Duration,
    expiredCount: var int,
) =
  var i = 0
  while i < ads.len:
    let ad = ads[i]
    let key = ad.toAdvertisementKey()
    registrar.cacheTimestamps.withValue(key, ts):
      if isExpired(now, ts[], advertExpiry):
        registrar.ipTree.removeAd(ad)
        registrar.cacheTimestamps.del(key)
        ads.delete(i)
        inc(expiredCount)
      else:
        inc(i)

Specifically this line registrar.cacheTimestamps.withValue(key, ts):. This conditional statement has no else. The code behaves like:

if key in registrar.cacheTimestamps:
  let ts = registrar.cacheTimestamps[key]

  if isExpired(now, ts, advertExpiry):
    registrar.ipTree.removeAd(ad)
    registrar.cacheTimestamps.del(key)
    ads.delete(i)
    inc(expiredCount)
  else:
    inc(i)

# no else here
# if key is missing, nothing happens

What it is happening is that the body does not delete ads[i]and does not inc(i). The next iteration sees the exact same ads[i], recomputes toAdvertisementKey(), misses the timestamp again, and repeats forever.

Why we are facing the condition

From what I have seen, the registrar has 2 separate indexes for the same advertisement, ie:

Registrar* = ref object
cache*: OrderedTable[ServiceId, seq[Advertisement]]
cacheTimestamps*: Table[AdvertisementKey, Moment]

Where