fixes #22711; Check atomicArc for atomic destroy race condition (#22788)

fixes #22711

Per @elcritch's awesome solution
This commit is contained in:
ringabout
2023-10-05 01:41:39 +08:00
committed by GitHub
parent f4a623dadf
commit f2f0b3e25d

View File

@@ -202,15 +202,22 @@ proc nimDecRefIsLast(p: pointer): bool {.compilerRtl, inl.} =
writeStackTrace()
cfprintf(cstderr, "[DecRef] %p %ld\n", p, cell.count)
if cell.count == 0:
result = true
when traceCollector:
cprintf("[ABOUT TO DESTROY] %p\n", cell)
when defined(gcAtomicArc) and hasThreadSupport:
# `atomicDec` returns the new value
if atomicDec(cell.rc, rcIncrement) == -1:
result = true
when traceCollector:
cprintf("[ABOUT TO DESTROY] %p\n", cell)
else:
decrement cell
# According to Lins it's correct to do nothing else here.
when traceCollector:
cprintf("[DECREF] %p\n", cell)
if cell.count == 0:
result = true
when traceCollector:
cprintf("[ABOUT TO DESTROY] %p\n", cell)
else:
decrement cell
# According to Lins it's correct to do nothing else here.
when traceCollector:
cprintf("[DECREF] %p\n", cell)
proc GC_unref*[T](x: ref T) =
## New runtime only supports this operation for 'ref T'.