You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "merlimat (via GitHub)" <gi...@apache.org> on 2026/03/18 20:57:20 UTC
[PR] [fix][broker] Fix race condition in ServerCnx producer/consumer async callbacks [pulsar]
merlimat opened a new pull request, #25352:
URL: https://github.com/apache/pulsar/pull/25352
## Flaky test failure
```
ServerCnxTest.testCreateProducerTimeout:2093->getResponse:2884->getResponse:2908 IO Failed to get response from socket within 10s
```
## Summary
Fix a race condition in `ServerCnx` where producer/consumer lifecycle callbacks could execute out of order, causing the create/close/create producer sequence to fail intermittently.
### Root Cause
The `ServerCnx` producers and consumers `ConcurrentLongHashMap` maps are designed with `concurrencyLevel=1`, assuming all accesses happen on the same Netty IO thread (`ctx.executor()`). However, most async callbacks used synchronous `CompletableFuture` variants (`thenAccept`, `thenCompose`, etc.) instead of async variants with `ctx.executor()`.
The issue is how `CompletableFuture` chaining works **without an explicit executor**:
- If the upstream future is **NOT yet completed** when `thenCompose(fn)` is called, `fn` is queued as a dependent and runs when the completing thread calls `complete()`. Multiple chained stages execute in registration order since the completing thread walks the chain sequentially.
- If the upstream future is **ALREADY completed** when `thenCompose(fn)` is called, `fn` runs **IMMEDIATELY on the calling thread**, right there in the `thenCompose` call. It skips the queue entirely.
This distinction causes the create/close/create producer race: both `createProducer1` and `createProducer2` call `getOrCreateTopic()` and chain `thenCompose(topic -> addProducer(...))`. When the topic future is not yet completed, both callbacks are queued as dependents and execute in order. But if the topic future is already completed when `createProducer2` chains on it, producer2's `addProducer()` runs immediately inline — potentially before producer1's cleanup from the close command has finished. Producer1 wins the race to register, producer2's `addProducer()` fails with "already connected", and the client never gets a success response.
### Fix
Using `thenComposeAsync(fn, ctx.executor())` forces `fn` to always be **submitted to the executor's task queue**, regardless of whether the future is already completed. This guarantees FIFO ordering — all stages go through the same queue, so the close handler runs before producer2's creation, and producer1's cleanup completes before producer2 tries to register.
### Changes
- `handleProducer` chain: `thenApplyAsync`, `thenComposeAsync`, `thenRunAsync`, `exceptionallyAsync` with `ctx.executor()`
- `buildProducerAndAddTopic`: `thenAcceptAsync`, `thenRunAsync`
- `handleSubscribe` chain: `thenApplyAsync`, `thenAcceptAsync`, `exceptionallyAsync`
- `handleCloseProducer`: `thenAcceptAsync`
- `safelyRemoveProducer/Consumer`: `whenCompleteAsync`
- `ServerCnxTest`: add `channel.runPendingTasks()` in `getResponse()` polling loop so `EmbeddedChannel` executor tasks are processed
## Documentation
- [x] `doc-not-needed`
(Your PR doesn't need any doc update)
## Matching PR in forked repository
_No response_
### Tip
Add the labels `ready-to-test` and `area/test` to trigger the CI.
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [fix][broker] Fix race condition in ServerCnx producer/consumer async callbacks [pulsar]
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #25352:
URL: https://github.com/apache/pulsar/pull/25352#issuecomment-4087767897
## [Codecov](https://app.codecov.io/gh/apache/pulsar/pull/25352?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 72.73%. Comparing base ([`7f6bc23`](https://app.codecov.io/gh/apache/pulsar/commit/7f6bc233dc37d0bd981f9bfe3ccc104069ac39a4?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`cc60065`](https://app.codecov.io/gh/apache/pulsar/commit/cc60065258b500ad429c3d68a99788b9ced6a574?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
:warning: Report is 8 commits behind head on master.
<details><summary>Additional details and impacted files</summary>
[](https://app.codecov.io/gh/apache/pulsar/pull/25352?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
```diff
@@ Coverage Diff @@
## master #25352 +/- ##
=============================================
+ Coverage 37.45% 72.73% +35.27%
+ Complexity 13161 2457 -10704
=============================================
Files 1897 1954 +57
Lines 150557 154768 +4211
Branches 17215 17718 +503
=============================================
+ Hits 56398 112572 +56174
+ Misses 86428 33146 -53282
- Partials 7731 9050 +1319
```
| [Flag](https://app.codecov.io/gh/apache/pulsar/pull/25352/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
|---|---|---|
| [inttests](https://app.codecov.io/gh/apache/pulsar/pull/25352/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `25.83% <100.00%> (-0.21%)` | :arrow_down: |
| [systests](https://app.codecov.io/gh/apache/pulsar/pull/25352/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `22.54% <100.00%> (+0.04%)` | :arrow_up: |
| [unittests](https://app.codecov.io/gh/apache/pulsar/pull/25352/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `73.71% <100.00%> (+39.54%)` | :arrow_up: |
Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
| [Files with missing lines](https://app.codecov.io/gh/apache/pulsar/pull/25352?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
|---|---|---|
| [...va/org/apache/pulsar/broker/service/ServerCnx.java](https://app.codecov.io/gh/apache/pulsar/pull/25352?src=pr&el=tree&filepath=pulsar-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fpulsar%2Fbroker%2Fservice%2FServerCnx.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci9zZXJ2aWNlL1NlcnZlckNueC5qYXZh) | `71.83% <100.00%> (+28.15%)` | :arrow_up: |
... and [1413 files with indirect coverage changes](https://app.codecov.io/gh/apache/pulsar/pull/25352/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
</details>
<details><summary> :rocket: New features to boost your workflow: </summary>
- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
- :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
</details>
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [fix][broker] Fix race condition in ServerCnx producer/consumer async callbacks [pulsar]
Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari merged PR #25352:
URL: https://github.com/apache/pulsar/pull/25352
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org