Skip to content

Commit a133d76

Browse files
authored
fix deadlock (#1152)
* fix deadlock Signed-off-by: Gregor Zeitlinger <[email protected]> * fix deadlock Signed-off-by: Gregor Zeitlinger <[email protected]> --------- Signed-off-by: Gregor Zeitlinger <[email protected]>
1 parent b8ff985 commit a133d76

File tree

1 file changed

+19
-16
lines changed
  • prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics

1 file changed

+19
-16
lines changed

prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Buffer.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ void reset() {
5757
reset = true;
5858
}
5959

60+
@SuppressWarnings("ThreadPriorityCheck")
6061
<T extends DataPointSnapshot> T run(
6162
Function<Long, Boolean> complete,
6263
Supplier<T> createResult,
@@ -70,24 +71,26 @@ <T extends DataPointSnapshot> T run(
7071
// Signal that the buffer is active.
7172
Long expectedCount = observationCount.getAndAdd(bufferActiveBit);
7273

73-
appendLock.lock();
74-
try {
75-
while (!complete.apply(expectedCount)) {
76-
// Wait until all in-flight threads have added their observations to the buffer.
77-
bufferFilled.await();
78-
}
79-
result = createResult.get();
74+
while (!complete.apply(expectedCount)) {
75+
// Wait until all in-flight threads have added their observations to the histogram
76+
// we can't use a condition here, because the other thread doesn't have a lock as it's on
77+
// the fast path.
78+
Thread.yield();
79+
}
80+
result = createResult.get();
8081

81-
// Signal that the buffer is inactive.
82-
int expectedBufferSize;
83-
if (reset) {
84-
expectedBufferSize =
85-
(int) ((observationCount.getAndSet(0) & ~bufferActiveBit) - expectedCount);
86-
reset = false;
87-
} else {
88-
expectedBufferSize = (int) (observationCount.addAndGet(bufferActiveBit) - expectedCount);
89-
}
82+
// Signal that the buffer is inactive.
83+
int expectedBufferSize;
84+
if (reset) {
85+
expectedBufferSize =
86+
(int) ((observationCount.getAndSet(0) & ~bufferActiveBit) - expectedCount);
87+
reset = false;
88+
} else {
89+
expectedBufferSize = (int) (observationCount.addAndGet(bufferActiveBit) - expectedCount);
90+
}
9091

92+
appendLock.lock();
93+
try {
9194
while (bufferPos < expectedBufferSize) {
9295
// Wait until all in-flight threads have added their observations to the buffer.
9396
bufferFilled.await();

0 commit comments

Comments
 (0)