allthingssecurity opened a new pull request, #30312: URL: https://github.com/apache/camel/pull/30312
# Description [CAMEL-27087](https://issues.apache.org/jira/browse/CAMEL-27087) The observe consumer (`CoAPObserver`, `observe=true`) creates its client with `CoAPEndpoint.createCoapClient`, which gives the client its own Californium `CoapEndpoint`: a bound UDP socket (or a DTLS / TCP connector) with its receiver and sender threads. On stop it cancelled the observation and called `CoapClient.shutdown()`, which does not destroy that endpoint. So each stop of the route, or of the consumer, left one socket and its threads behind until the JVM ended. CAMEL-26049 (#28536) fixed the same leak in the producer and left the consumer out, because the endpoint carries the cancel of the observation (CAMEL-25742); in that review we agreed to do it in a separate JIRA. The endpoint must stay until the cancel has gone out, or the server keeps the stopped consumer as an observer. Fix, in `CoAPObserver`: - the stop cancels the observation and shuts the client down as before, under the lock; - then, outside the lock, it waits for the answer to the cancel and destroys the endpoint of the client. `ClientObserveRelation.proactiveCancel()` sends the cancel with the message observers of the observe request, so its answer comes to the handler of the observation: the cancel response (a GET answer without the observe option; a notification already on its way still has it), or an error. The wait is bounded (5 seconds, for a server that does not answer), and an interrupt ends it; - nothing is waited for when no cancel is sent: no relation, or a relation already cancelled (`isCanceled()`), such as for a resource that is not observable. Tests: `CoAPObserverStopDestroysEndpointTest` (as `CoAPProducerStopDestroysEndpointTest` for the producer): an observable resource and an observe route; the route is stopped and started 5 times and stopped again, and each stop must remove the observer on the server (the cancel still goes out). Then no `UDP-Receiver` thread started during the test may be alive. On main 10 are (two per client endpoint). The stops do not wait for the bound: the test takes about 1.4 seconds for 6 stops. With the change the camel-coap unit tests pass (93 run, 0 failures), the new test passes 40 times in one JVM, and `CoAPObserveRestartTest` (CAMEL-25742) passes in the module run. Run 30 times in one JVM, `CoAPObserveRestartTest` fails now and then both with and without this change (2 of 180 with it, 3 of 180 on main, the same tests: a notification not received in time), so that is not from this change. # Target - [x] I checked that the commit is targeting the correct branch (Camel 4 uses the `main` branch) # Tracking - [x] 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): [CAMEL-27087](https://issues.apache.org/jira/browse/CAMEL-27087). # Apache Camel coding standards and style - [x] I checked that each commit in the pull request has a meaningful subject line and body. - [ ] I have run `mvn clean install -DskipTests` locally from root folder and I have committed all auto-generated changes. (I built the core modules and camel-coap with the formatter and import-sort plugins; no option, doc or generated file changes. I did not run the full root build.) # AI-assisted contributions - [x] 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 PR was prepared with Claude Code (Claude Opus 5.5). The commit carries a `Co-Authored-By` trailer. _Claude Code on behalf of allthingssecurity_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- 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]
