Skip to content

Commit 5d903b5

Browse files
committed
Enforce start happens-before stop
Closes gh-13133
1 parent 0b6e84b commit 5d903b5

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

web/src/main/java/org/springframework/security/web/server/ObservationWebFilterChainDecorator.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.ListIterator;
2222
import java.util.concurrent.atomic.AtomicInteger;
2323
import java.util.concurrent.atomic.AtomicReference;
24+
import java.util.concurrent.locks.Lock;
25+
import java.util.concurrent.locks.ReentrantLock;
2426

2527
import io.micrometer.common.KeyValue;
2628
import io.micrometer.common.KeyValues;
@@ -382,6 +384,8 @@ private static final class ObservationReference {
382384

383385
private static final ObservationReference NOOP = new ObservationReference(Observation.NOOP);
384386

387+
private final Lock lock = new ReentrantLock();
388+
385389
private final AtomicInteger state = new AtomicInteger(0);
386390

387391
private final Observation observation;
@@ -391,20 +395,38 @@ private ObservationReference(Observation observation) {
391395
}
392396

393397
private void start() {
394-
if (this.state.compareAndSet(0, 1)) {
395-
this.observation.start();
398+
try {
399+
this.lock.lock();
400+
if (this.state.compareAndSet(0, 1)) {
401+
this.observation.start();
402+
}
403+
}
404+
finally {
405+
this.lock.unlock();
396406
}
397407
}
398408

399409
private void error(Throwable ex) {
400-
if (this.state.get() == 1) {
401-
this.observation.error(ex);
410+
try {
411+
this.lock.lock();
412+
if (this.state.get() == 1) {
413+
this.observation.error(ex);
414+
}
415+
}
416+
finally {
417+
this.lock.unlock();
402418
}
403419
}
404420

405421
private void stop() {
406-
if (this.state.compareAndSet(1, 2)) {
407-
this.observation.stop();
422+
try {
423+
this.lock.lock();
424+
if (this.state.compareAndSet(1, 2)) {
425+
this.observation.stop();
426+
}
427+
}
428+
finally {
429+
this.lock.unlock();
408430
}
409431
}
410432

0 commit comments

Comments
 (0)