BewareMyPower opened a new pull request, #22279:
URL: https://github.com/apache/pulsar/pull/22279
### Motivation
The access on `readOnActiveConsumerTask` is not thread safe.
```java
if (readOnActiveConsumerTask != null) {
return;
}
readOnActiveConsumerTask = topic.getBrokerService().executor().schedule(()
-> {/* ... */});
```
There is a case that:
| Steps | Thread 1 | Thread 2 |
| :- | :- | :- |
| 1 | Read `readOnActiveConsumerTask`: null | |
| 2 | call `schedule` | |
| 3 | | Read `readOnActiveConsumerTask`: null |
| 4 | | call `schedule` |
| 5 | Write `readOnActiveConsumerTask` to the result of `schedule` | | | 6 |
| Write `readOnActiveConsumerTask` to the result of `schedule` |
Then `schedule()` will be called twice and only one result will be assigned
to `readOnActiveConsumerTask`.
### Modifications
Follow the double-checked locking when `readOnActiveConsumerTask` is null to
ensure `readOnActiveConsumerTask` cannot be called concurrently.
### Documentation
<!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
- [ ] `doc` <!-- Your PR contains doc changes. -->
- [ ] `doc-required` <!-- Your PR changes impact docs and you will update
later -->
- [x] `doc-not-needed` <!-- Your PR changes do not impact docs -->
- [ ] `doc-complete` <!-- Docs have been already added -->
### Matching PR in forked repository
PR in forked repository:
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]