Skip to content

Fix S2095 resource leaks in camel-test-infra-openai-mock#24359

Closed
gnodet wants to merge 4 commits into
apache:mainfrom
gnodet:sonar/s2095-openai-mock
Closed

Fix S2095 resource leaks in camel-test-infra-openai-mock#24359
gnodet wants to merge 4 commits into
apache:mainfrom
gnodet:sonar/s2095-openai-mock

Conversation

@gnodet

@gnodet gnodet commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Claude Code on behalf of Guillaume Nodet

Fix SonarCloud S2095 (Resources should be closed) in the camel-langchain4j-chat component.

Real resource leak fixes

  • AudioTranscriptionRequestHandler.java: Close InputStream from HttpExchange.getRequestBody() using try-with-resources
  • EmbeddingRequestHandler.java: Close InputStream from HttpExchange.getRequestBody() using try-with-resources
  • RequestHandler.java: Close InputStream from HttpExchange.getRequestBody() using try-with-resources

HttpClient false positives (marked Won't Fix in SonarCloud)

HttpClient does not implement AutoCloseable before Java 21 (JDK-8304165). On the project's Java 17 target, there is nothing to close. These 17 issues across 8 test files are marked as Won't Fix directly in SonarCloud instead of suppressing in code.

Test plan

  • CI passes
  • SonarCloud S2095 count decreases after merge

🤖 Generated with Claude Code

Close InputStream from HttpExchange.getRequestBody() using
try-with-resources in AudioTranscriptionRequestHandler,
EmbeddingRequestHandler, and RequestHandler.

Close HttpClient instances in test classes using a finally block
with an instanceof AutoCloseable guard (compatible with --release 17
while correctly closing on Java 21+ runtimes where HttpClient
implements AutoCloseable).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gnodet gnodet requested review from Croway and gzurowski July 1, 2026 09:54
@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

🌟 Thank you for your contribution to the Apache Camel project! 🌟
🤖 CI automation will test this PR automatically.

🐫 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 have access to the summary.

⚠️ Be careful when sharing logs. Review their contents before sharing them publicly.

@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

🧪 CI tested the following changed modules:

  • test-infra/camel-test-infra-openai-mock
All tested modules (13 modules)
  • Camel :: AI :: LangChain4j :: Agent
  • Camel :: AI :: LangChain4j :: Tools
  • Camel :: AI :: OpenAI
  • Camel :: JBang :: MCP
  • Camel :: JBang :: Plugin :: MCP
  • Camel :: JBang :: Plugin :: Route Parser
  • Camel :: JBang :: Plugin :: TUI
  • Camel :: JBang :: Plugin :: Validate
  • Camel :: Launcher :: Container
  • Camel :: Test Infra :: All test services
  • Camel :: Test Infra :: OpenAI Mock
  • Camel :: YAML DSL :: Validator
  • Camel :: YAML DSL :: Validator Maven Plugin

⚙️ View full build and test results

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gnodet gnodet marked this pull request as ready for review July 1, 2026 18:30
@gnodet gnodet requested a review from Copilot July 1, 2026 19:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses SonarCloud S2095 “resources should be closed” findings in camel-test-infra-openai-mock by fixing real HttpExchange request-body InputStream leaks in mock handlers and suppressing known-false-positive HttpClient warnings in the module’s test classes (Java 17 target).

Changes:

  • Close HttpExchange.getRequestBody() InputStreams via try-with-resources in the OpenAI mock request handlers.
  • Add method-level @SuppressWarnings("java:S2095") (with rationale comments) for HttpClient.newHttpClient() usage in tests where HttpClient is not closeable on the project’s Java target.
  • Apply minor formatting adjustments to keep code style consistent after the additions.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.

Show a summary per file
File Description
test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/RequestHandler.java Wraps exchange.getRequestBody() in try-with-resources to ensure the request stream is closed.
test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/EmbeddingRequestHandler.java Wraps exchange.getRequestBody() in try-with-resources to close the embedding request stream.
test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/AudioTranscriptionRequestHandler.java Wraps request-body consumption in try-with-resources to avoid leaking the request stream.
test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/OpenAIMockTest.java Suppresses S2095 false positives for HttpClient construction in tests (method-level).
test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/OpenAIMockSimpleAssertionTest.java Suppresses S2095 false positives for HttpClient construction in the test method.
test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/OpenAIMockReplyWithToolContentTest.java Suppresses S2095 false positives for HttpClient construction; keeps formatting tidy.
test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/OpenAIMockReplyWithAfterToolTest.java Suppresses S2095 false positives for HttpClient construction in the test method.
test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/OpenAIMockMultipleToolsTest.java Suppresses S2095 false positives for HttpClient construction and adjusts line wrapping.
test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/OpenAIMockFailuresTest.java Suppresses S2095 false positives for HttpClient construction in failure-path tests.
test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/OpenAIMockEmbeddingTest.java Suppresses S2095 false positives for HttpClient construction across embedding tests; minor formatting.
test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/OpenAIMockConversationHistoryTest.java Suppresses S2095 false positives for HttpClient construction in the history test.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

gnodet and others added 2 commits July 2, 2026 05:57
…loud

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gnodet

gnodet commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

Claude Code on behalf of Guillaume Nodet — Changes incorporated into #24358.

@gnodet gnodet closed this Jul 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants