releaseRunRef decrements the reference count for runID and cleans up shared state when the last reference is released. The mutex ensures no concurrent trackRunRef can increment between the zero check and the map deletions.
(runID uuid.UUID)
| 126 | // no concurrent trackRunRef can increment between the zero check and |
| 127 | // the map deletions. |
| 128 | func releaseRunRef(runID uuid.UUID) { |
| 129 | refCountMu.Lock() |
| 130 | defer refCountMu.Unlock() |
| 131 | val, ok := runRefCounts.Load(runID) |
| 132 | if !ok { |
| 133 | return |
| 134 | } |
| 135 | counter, ok := val.(*atomic.Int32) |
| 136 | if !ok { |
| 137 | panic("chatdebug: runRefCounts contains non-*atomic.Int32 value") |
| 138 | } |
| 139 | if counter.Add(-1) <= 0 { |
| 140 | runRefCounts.Delete(runID) |
| 141 | stepCounters.Delete(runID) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | func nextStepNumber(runID uuid.UUID) int32 { |
| 146 | val, _ := stepCounters.LoadOrStore(runID, &atomic.Int32{}) |