You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by "apupier (via GitHub)" <gi...@apache.org> on 2026/10/14 07:52:24 UTC
[PR] CAMEL-25487: Fix flaky GrpcProducerStreamingTest by eliminating strea… [camel]
apupier opened a new pull request, #27806:
URL: https://github.com/apache/camel/pull/27806
…m TOCTOU race
GrpcStreamingExchangeForwarder had a TOCTOU (Time-Of-Check to Time-Of-Use) race: the fast-path lock-free read of currentStream allowed a Camel thread to obtain a live stream reference, which could then be closed by the gRPC response thread (via doCloseStream()) before onNext() was called on it, silently dropping the message.
Fix: replace the bare volatile StreamObserver field with a StreamHolder (stream + volatile boolean closed flag). closeCurrentHolder() sets holder.closed = true under the lock when the server terminates the call (onError / onCompleted). getOrCreateStream() always acquires the lock and checks currentHolder.closed, so any forward() call that races with a stream teardown will detect the closed flag and open a fresh stream instead of sending on the dead one.
The test is also made deterministic: a dedicated recovery route is added with forwardOnError=true. The test waits for messageGroupCount+1 messages (normal replies + the forwarded onError event) before sending the second batch. Because closeCurrentHolder() runs before streamObserver.onError() in the production code, MockEndpoint assertIsSatisfied() provides a causal guarantee that the stream has been fully torn down on the client side — no timing-dependent sleeps or server-side await proxies needed.
History of the modified lines (no prior intent is broken):
- CAMEL-16893 (5d2c4dce): added null guard in shutdown() to prevent NPE when no stream was ever opened. Preserved as-is.
- CAMEL-20277 (dae5983b): added GRPC_EVENT_TYPE header dispatch in forward() to support proxy routes driving all stream event types. The dispatch logic is untouched by this fix.
- CAMEL-20199 (8466fd64): replaced synchronized(this) with ReentrantLock for virtual-thread compatibility (avoid carrier-thread pinning). This commit kept the pre-existing double-checked locking fast path, which inadvertently preserved the TOCTOU race. Our fix keeps the ReentrantLock and eliminates the race; virtual-thread friendliness is maintained.
- chore 2e2684610b / CAMEL-22539 685e1a4e: two successive test-side attempts to reduce flakiness (Awaitility, longer timeouts, re-enable on CI). Neither addressed the root cause. The server-side await before the second batch is removed as it is no longer needed.
# Description
<!--
- Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
-->
# Target
- [ ] I checked that the commit is targeting the correct branch (Camel 4 uses the `main` branch)
# Tracking
- [ ] If this is a large change, bug fix, or code improvement, I checked there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for the change (usually before you start working on it).
<!--
# *Note*: trivial changes like, typos, minor documentation fixes and other small items do not require a JIRA issue. In this case your pull request should address just this issue, without pulling in other changes.
-->
# Apache Camel coding standards and style
- [ ] I checked that each commit in the pull request has a meaningful subject line and body.
<!--
If you're unsure, you can format the pull request title like `[CAMEL-XXX] Fixes bug in camel-file component`, where you replace `CAMEL-XXX` with the appropriate JIRA issue.
-->
- [ ] I have run `mvn clean install -DskipTests` locally from root folder and I have committed all auto-generated changes.
<!--
You can run the aforementioned command in your module so that the build auto-formats your code. This will also be verified as part of the checks and your PR may be rejected if if there are uncommited changes after running `mvn clean install -DskipTests`.
You can learn more about the contribution guidelines at https://github.com/apache/camel/blob/main/CONTRIBUTING.md
-->
# AI-assisted contributions
- [ ] If this PR includes AI-generated code, commits have proper co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR description identifies the AI tool used.
--
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@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] CAMEL-25487: Fix flaky GrpcProducerStreamingTest by eliminating strea… [camel]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #27806:
URL: https://github.com/apache/camel/pull/27806#issuecomment-6166034995
<!-- ci-tested-modules -->
:test_tube: **CI tested the following changed modules:**
- `components/camel-grpc`
---
<details><summary>:microscope: Scalpel shadow comparison — Scalpel: 8 of 703 tested, 27 compile-only — current: 8 all tested</summary>
[Maveniverse Scalpel](https://github.com/maveniverse/scalpel) detected **8 affected modules** (current approach: 8).
**Skip-tests mode would test 8 modules** (1 direct + 7 downstream), **skip tests for 27** (generated code, meta-modules)
<details><summary>Modules Scalpel would test (8)</summary>
- `camel-grpc` ← components/camel-grpc/src/main/java/org/apache/camel/component/grpc/client/GrpcStreamingExchangeForwarder.java, components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcProducerStreamingTest.java
- `camel-jbang-mcp` ← downstream of org.apache.camel:camel-catalog
- `camel-jbang-plugin-mcp` ← downstream of org.apache.camel:camel-jbang-core
- `camel-jbang-plugin-tui` ← downstream of org.apache.camel:camel-catalog
- `camel-jbang-plugin-validate` ← downstream of org.apache.camel:camel-yaml-dsl-validator
- `camel-launcher-container` ← downstream of org.apache.camel:camel-launcher
- `camel-yaml-dsl-validator` ← downstream of org.apache.camel:camel-catalog
- `camel-yaml-dsl-validator-maven-plugin` ← downstream of org.apache.camel:camel-yaml-dsl-validator
</details>
<details><summary>Modules with tests skipped (27)</summary>
- `apache-camel`
- `camel-allcomponents`
- `camel-catalog`
- `camel-catalog-console`
- `camel-catalog-maven`
- `camel-catalog-suggest`
- `camel-componentdsl`
- `camel-endpointdsl`
- `camel-endpointdsl-support`
- `camel-itest`
- `camel-jbang-core`
- `camel-jbang-it`
- `camel-jbang-main`
- `camel-jbang-plugin-edit`
- `camel-jbang-plugin-generate`
- `camel-jbang-plugin-kubernetes`
- `camel-jbang-plugin-test`
- `camel-kamelet-main`
- `camel-launcher`
- `camel-report-maven-plugin`
- `camel-route-parser`
- `camel-yaml-dsl`
- `camel-yaml-dsl-deserializers`
- `camel-yaml-dsl-maven-plugin`
- `coverage`
- `docs`
- `dummy-component`
</details>
> :information_source: Shadow mode — Scalpel observes but does not affect test execution. [Learn more](https://github.com/maveniverse/scalpel)
</details>
<details><summary>All tested modules (35 modules, 5m 43s total)</summary>
**Total reactor time:** 5m 43s
| Module | Duration | Status |
| --- | --- | --- |
| Camel :: Launcher | 49.4s | SUCCESS |
| Camel :: JBang :: MCP | 43.0s | SUCCESS |
| Camel :: JBang :: Plugin :: Kubernetes | 28.0s | SUCCESS |
| Camel :: Catalog :: Camel Catalog | 24.8s | SUCCESS |
| Camel :: Component DSL | 23.6s | SUCCESS |
| Camel :: YAML DSL :: Validator | 22.3s | SUCCESS |
| Camel :: YAML DSL | 22.0s | SUCCESS |
| Camel :: gRPC | 19.2s | SUCCESS |
| Camel :: JBang :: Plugin :: Validate | 17.0s | SUCCESS |
| Camel :: Docs | 16.1s | SUCCESS |
| Camel :: Kamelet Main | 12.9s | SUCCESS |
| Camel :: JBang :: Plugin :: Testing | 8.5s | SUCCESS |
| Camel :: YAML DSL :: Deserializers | 8.4s | SUCCESS |
| Camel :: Catalog :: Camel Route Parser | 8.2s | SUCCESS |
| Camel :: Catalog :: Camel Report Maven Plugin | 7.2s | SUCCESS |
| Camel :: All Components Sync point | 5.8s | SUCCESS |
| Camel :: YAML DSL :: Validator Maven Plugin | 5.0s | SUCCESS |
| Camel :: YAML DSL :: Maven Plugins | 3.1s | SUCCESS |
| Camel :: Catalog :: Maven | 3.0s | SUCCESS |
| Camel :: Catalog :: Suggest (deprecated) | 2.7s | SUCCESS |
| Camel :: Catalog :: Dummy Component | 2.0s | SUCCESS |
| Camel :: Assembly | 1.7s | SUCCESS |
| Camel :: Coverage | 1.6s | SUCCESS |
| Camel :: JBang :: Plugin :: Edit | 1.3s | SUCCESS |
| Camel :: JBang :: Plugin :: MCP | 1.3s | SUCCESS |
| Camel :: JBang :: Integration tests | 1.2s | SUCCESS |
| Camel :: JBang :: Plugin :: Generate | 1.1s | SUCCESS |
| Camel :: Catalog :: Console | 0.8s | SUCCESS |
| Camel :: Endpoint DSL :: Support | 0.8s | SUCCESS |
| Camel :: Launcher :: Container | 0.7s | SUCCESS |
| Camel :: JBang :: Main | 0.7s | SUCCESS |
| Camel :: Endpoint DSL | n/a | |
| Camel :: Integration Tests | n/a | |
| Camel :: JBang :: Core | n/a | |
| Camel :: JBang :: Plugin :: TUI | n/a | |
**Top 20 slowest modules:**
- `Camel :: Launcher` (49.4s)
- `Camel :: JBang :: MCP` (43.0s)
- `Camel :: JBang :: Plugin :: Kubernetes` (28.0s)
- `Camel :: Catalog :: Camel Catalog` (24.8s)
- `Camel :: Component DSL` (23.6s)
- `Camel :: YAML DSL :: Validator` (22.3s)
- `Camel :: YAML DSL` (22.0s)
- `Camel :: gRPC` (19.2s)
- `Camel :: JBang :: Plugin :: Validate` (17.0s)
- `Camel :: Docs` (16.1s)
- `Camel :: Kamelet Main` (12.9s)
- `Camel :: JBang :: Plugin :: Testing` (8.5s)
- `Camel :: YAML DSL :: Deserializers` (8.4s)
- `Camel :: Catalog :: Camel Route Parser` (8.2s)
- `Camel :: Catalog :: Camel Report Maven Plugin` (7.2s)
- `Camel :: All Components Sync point` (5.8s)
- `Camel :: YAML DSL :: Validator Maven Plugin` (5.0s)
- `Camel :: YAML DSL :: Maven Plugins` (3.1s)
- `Camel :: Catalog :: Maven` (3.0s)
- `Camel :: Catalog :: Suggest (deprecated)` (2.7s)
</details>
---
:gear: [View full build and test results](https://github.com/apache/camel/actions/runs/38531205007)
--
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@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] CAMEL-25487: Fix flaky GrpcProducerStreamingTest by eliminating strea… [camel]
Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus commented on code in PR #27806:
URL: https://github.com/apache/camel/pull/27806#discussion_r4275133981
##########
components/camel-grpc/src/main/java/org/apache/camel/component/grpc/client/GrpcStreamingExchangeForwarder.java:
##########
@@ -37,9 +37,15 @@ class GrpcStreamingExchangeForwarder implements GrpcExchangeForwarder {
private final Object grpcStub;
private final Lock lock = new ReentrantLock();
- private volatile StreamObserver<Object> currentStream;
- private volatile StreamObserver<Object> currentResponseObserver;
+ /**
+ * Holds the current open stream together with a closed flag. The holder is replaced atomically (under lock) each
+ * time the stream is recreated. The closed flag is set by the gRPC response thread when the server terminates the
+ * call, signalling that any concurrent or subsequent forward() must open a fresh stream.
+ */
+ private StreamHolder currentHolder;
+
+ private StreamObserver<Object> currentResponseObserver;
Review Comment:
This used to be `volatile`. `shutdown()` (line 82) reads it without the lock, from the producer stop thread, so that thread can see a stale `null` and skip `onCompleted()`. If the class is not reverted, please keep the field volatile:
```suggestion
private volatile StreamObserver<Object> currentResponseObserver;
```
##########
components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcProducerStreamingTest.java:
##########
@@ -82,20 +82,25 @@ public void testPingAsyncAsync() throws Exception {
public void testPingAsyncAsyncRecovery() throws Exception {
int messageGroupCount = 5;
for (int i = 1; i <= messageGroupCount; i++) {
- template.sendBody("direct:grpc-stream-async-async-route",
+ template.sendBody("direct:grpc-stream-async-async-recovery-route",
PingRequest.newBuilder().setPingName(String.valueOf(i)).build());
}
- template.sendBody("direct:grpc-stream-async-async-route", PingRequest.newBuilder().setPingName("error").build());
+ template.sendBody("direct:grpc-stream-async-async-recovery-route",
+ PingRequest.newBuilder().setPingName("error").build());
MockEndpoint replies = getMockEndpoint("mock:grpc-replies");
- replies.expectedMessageCount(messageGroupCount);
+ // 5 normal replies + 1 forwarded onError event (forwardOnError=true on this route)
+ replies.expectedMessageCount(messageGroupCount + 1);
replies.assertIsSatisfied();
- await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> assertListSize(pingPongServer.getLastStreamRequests(), 1));
-
+ // At this point closeCurrentHolder() has already completed on the client side:
+ // closeCurrentHolder() runs *before* streamObserver.onError() is called, and
+ // streamObserver.onError() is what ultimately delivers the onError event to this mock.
+ // MockEndpoint.assertIsSatisfied() above confirms that delivery has occurred, so the
+ // next batch is guaranteed to open a fresh stream — no timing-dependent wait needed.
for (int i = messageGroupCount + 1; i <= 2 * messageGroupCount; i++) {
- template.sendBody("direct:grpc-stream-async-async-route",
+ template.sendBody("direct:grpc-stream-async-async-recovery-route",
PingRequest.newBuilder().setPingName(String.valueOf(i)).build());
}
Review Comment:
`replies.reset()` and `replies.expectedMessageCount(messageGroupCount)` (lines 107-108) run after this loop. The replies to 6..10 arrive asynchronously on the gRPC thread and can reach the mock before `reset()`, which drops them from the count. The test then fails with `Expected: <5> but was: <n>`, the same symptom as in the JIRA. Please move the reset and the expectation above this loop.
##########
components/camel-grpc/src/main/java/org/apache/camel/component/grpc/client/GrpcStreamingExchangeForwarder.java:
##########
@@ -74,38 +80,44 @@ public void forward(Exchange exchange) {
@Override
public void shutdown() {
if (this.currentResponseObserver != null) {
- checkAndRecreateStreamObserver(this.currentResponseObserver).onCompleted();
+ getOrCreateStream(this.currentResponseObserver).onCompleted();
}
- doCloseStream();
+ closeCurrentHolder();
}
- private StreamObserver<Object> checkAndRecreateStreamObserver(StreamObserver<Object> responseObserver) {
- StreamObserver<Object> curStream = this.currentStream;
- if (curStream == null) {
- lock.lock();
- try {
- if (this.currentStream == null) {
- this.currentResponseObserver = responseObserver;
- this.currentStream = doCreateStream(responseObserver);
- }
-
- curStream = this.currentStream;
- } finally {
- lock.unlock();
+ /**
+ * Returns the current open stream, creating a new one if there is none or if the current one has been closed by the
+ * server (onError / onCompleted). Always called under the lock so that a concurrent doCloseHolder() cannot sneak in
+ * between the null-check and the return.
+ */
+ private StreamObserver<Object> getOrCreateStream(StreamObserver<Object> responseObserver) {
+ lock.lock();
+ try {
+ if (this.currentHolder == null || this.currentHolder.closed) {
+ this.currentResponseObserver = responseObserver;
+ this.currentHolder = new StreamHolder(doCreateStream(responseObserver));
}
- }
- StreamObserver<Object> curResponseObserver = this.currentResponseObserver;
- if (curResponseObserver != null && !curResponseObserver.equals(responseObserver)) {
- throw new IllegalArgumentException("This forwarder must always use the same response observer");
+ StreamObserver<Object> curResponseObserver = this.currentResponseObserver;
+ if (curResponseObserver != null && !curResponseObserver.equals(responseObserver)) {
+ throw new IllegalArgumentException("This forwarder must always use the same response observer");
+ }
+ return this.currentHolder.stream;
+ } finally {
+ lock.unlock();
}
Review Comment:
The check is done under the lock, but the stream is used after `unlock()`: `forward()` calls `streamObserver.onNext(...)` at line 69 without holding the lock. `closeCurrentHolder()` on the gRPC response thread can still run between this return and that `onNext`, which is the same window as the old volatile fast path. So this does not remove the TOCTOU, and it adds a lock acquisition per message. The test change already makes the test deterministic without this (the old `doCloseStream()` also ran before `streamObserver.onError`). Could this file be reverted?
--
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@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] CAMEL-25487: Fix flaky GrpcProducerStreamingTest recovery test [camel]
Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus merged PR #27806:
URL: https://github.com/apache/camel/pull/27806
--
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@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] CAMEL-25487: Fix flaky GrpcProducerStreamingTest recovery test [camel]
Posted by "gnodet-bot (via GitHub)" <gi...@apache.org>.
gnodet-bot commented on code in PR #27806:
URL: https://github.com/apache/camel/pull/27806#discussion_r4280145168
##########
components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcProducerStreamingTest.java:
##########
@@ -82,31 +82,37 @@ public void testPingAsyncAsync() throws Exception {
public void testPingAsyncAsyncRecovery() throws Exception {
int messageGroupCount = 5;
for (int i = 1; i <= messageGroupCount; i++) {
- template.sendBody("direct:grpc-stream-async-async-route",
+ template.sendBody("direct:grpc-stream-async-async-recovery-route",
PingRequest.newBuilder().setPingName(String.valueOf(i)).build());
}
- template.sendBody("direct:grpc-stream-async-async-route", PingRequest.newBuilder().setPingName("error").build());
+ template.sendBody("direct:grpc-stream-async-async-recovery-route",
+ PingRequest.newBuilder().setPingName("error").build());
MockEndpoint replies = getMockEndpoint("mock:grpc-replies");
- replies.expectedMessageCount(messageGroupCount);
+ // 5 normal replies + 1 forwarded onError event (forwardOnError=true on this route)
+ replies.expectedMessageCount(messageGroupCount + 1);
Review Comment:
Note on the forwarded `onError` event: the body of this exchange will be the `Throwable` (a `RuntimeCamelException` wrapping the gRPC status error), not a `PongResponse`. The assertion only checks message count, which is fine — just worth knowing when debugging failures on this assertion.
--
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@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] CAMEL-25487: Fix flaky GrpcProducerStreamingTest by eliminating strea… [camel]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #27806:
URL: https://github.com/apache/camel/pull/27806#issuecomment-6163678341
:star2: Thank you for your contribution to the Apache Camel project! :star2:
:robot: CI automation will test this PR automatically.
:camel: Apache Camel Committers, please review the following items:
* First-time contributors **require MANUAL approval** for the GitHub Actions to run
* You can use the command `/component-test (camel-)component-name1 (camel-)component-name2..` to request a test from the test bot although they are normally detected and executed by CI.
* You can label PRs using `skip-tests` and `test-dependents` to fine-tune the checks executed by this PR.
* Build and test logs are available in the summary page. **Only** [Apache Camel committers](https://camel.apache.org/community/team/#committers) have access to the summary.
:warning: Be careful when sharing logs. Review their contents before sharing them publicly.
--
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@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org