Skip to content

Commit c8a3a65

Browse files
committed
UPSTREAM: 581: Fix deadlock in recursive metric locks
RecordMetrics() grabs a mutex and calls recordCancelMetric(), which wants to grab the same mutex. Go mutexes are not recursive, so recordCancelMetric blocks forever. recordCancelMetric should not grab the mutex, it can be sure that the caller did it already.
1 parent 4583660 commit c8a3a65

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

pkg/metrics/metrics.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,15 @@ func (opMgr *operationMetricsManager) RecordMetrics(opKey OperationKey, opStatus
231231
obj, exists := opMgr.cache[createKey]
232232
if exists {
233233
// record a cancel metric if found
234-
opMgr.recordCancelMetric(obj, createKey, operationDuration)
234+
opMgr.recordCancelMetricLocked(obj, createKey, operationDuration)
235235
}
236236

237237
// check if we have a CreateSnapshotAndReady operation pending for this
238238
createAndReadyKey := NewOperationKey(CreateSnapshotAndReadyOperationName, opKey.ResourceID)
239239
obj, exists = opMgr.cache[createAndReadyKey]
240240
if exists {
241241
// record a cancel metric if found
242-
opMgr.recordCancelMetric(obj, createAndReadyKey, operationDuration)
242+
opMgr.recordCancelMetricLocked(obj, createAndReadyKey, operationDuration)
243243
}
244244
}
245245

@@ -248,9 +248,8 @@ func (opMgr *operationMetricsManager) RecordMetrics(opKey OperationKey, opStatus
248248
}
249249

250250
// recordCancelMetric records a metric for a create operation that hasn't finished
251-
func (opMgr *operationMetricsManager) recordCancelMetric(val OperationValue, key OperationKey, duration float64) {
252-
opMgr.mu.Lock()
253-
defer opMgr.mu.Unlock()
251+
// This function must be called with opMgr mutex locked (to prevent recursive locks).
252+
func (opMgr *operationMetricsManager) recordCancelMetricLocked(val OperationValue, key OperationKey, duration float64) {
254253
// record a cancel metric if found
255254

256255
opMgr.opLatencyMetrics.WithLabelValues(

0 commit comments

Comments
 (0)