Skip to content

[Internal]Cosmos Tests - Add client and Netty ByteBuf leak detection#47200

Merged
FabianMeiswinkel merged 22 commits into
mainfrom
users/fabianm/TestImprovements
Nov 10, 2025
Merged

[Internal]Cosmos Tests - Add client and Netty ByteBuf leak detection#47200
FabianMeiswinkel merged 22 commits into
mainfrom
users/fabianm/TestImprovements

Conversation

@FabianMeiswinkel

@FabianMeiswinkel FabianMeiswinkel commented Nov 7, 2025

Copy link
Copy Markdown
Member

Description

Implements comprehensive resource leak detection infrastructure for the Azure Cosmos Java SDK, disabled by default and enabled via COSMOS.CLIENT_LEAK_DETECTION_ENABLED system property.

This PR is intentionally just adding the test infrastructure - not yet failing/blokcing tests when Netty buffer leaks or RxDocumentClientImpl leaks are detected in test code (or production code). Making any of those necessary changes in separate PRs intentionally to simplify reviews.

Changes

  • StackTraceUtil: New utility for capturing and formatting call stacks, used to track client creation locations
  • Client leak detection: Added tracking in RxDocumentClientImpl via addToActiveClients()/removeFromActiveClients() lifecycle hooks and getActiveClientsSnapshot() for diagnostics
  • Netty leak detection: Custom ResourceLeakDetectorFactory implementations in test modules that intercept all three leak report types (traced, untraced, instances) with consistent isLeakDetectionDisabled flag checking
  • Disable scope: createDisableLeakDetectionScope() provides AutoCloseable scope to temporarily suppress leak reporting in specific test scenarios
  • Test integration: Leak detection checks integrated into test base classes at test class granularity

Usage

// Enable via system property
System.setProperty("COSMOS.CLIENT_LEAK_DETECTION_ENABLED", "true");

// Check for active clients (indicates leaks)
Map<Integer, String> activeClients = RxDocumentClientImpl.getActiveClientsSnapshot();

// Temporarily disable leak detection
try (AutoCloseable scope = CosmosNettyLeakDetectorFactory.createDisableLeakDetectionScope()) {
    // Operations that intentionally leak or produce false positives
}

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

Copilot AI review requested due to automatic review settings November 7, 2025 20:51
@github-actions github-actions Bot added the Cosmos label Nov 7, 2025
@FabianMeiswinkel

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - tests

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@FabianMeiswinkel

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - tests

@FabianMeiswinkel

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - spark

@FabianMeiswinkel

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - kafka

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

2 similar comments
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request introduces client and Netty ByteBuf leak detection infrastructure for the Azure Cosmos Java SDK to help identify resource leaks during testing. The feature is disabled by default and can be enabled via the COSMOS.CLIENT_LEAK_DETECTION_ENABLED system property.

Key changes:

  • Added a new StackTraceUtil utility class for capturing call stacks
  • Implemented client leak detection tracking in RxDocumentClientImpl with methods to register/unregister active clients
  • Added comprehensive Netty leak detection using a custom ResourceLeakDetectorFactory implementation
  • Integrated leak detection checks into test base classes to detect leaks at the test class level

Reviewed Changes

Copilot reviewed 32 out of 32 changed files in this pull request and generated 15 comments.

Show a summary per file
File Description
sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/StackTraceUtil.java New utility class for capturing and formatting stack traces
sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java Added client leak detection infrastructure with static map to track active clients and their creation call stacks
sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/Configs.java Added configuration to enable/disable client leak detection via system property
sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/AsyncDocumentClient.java Added try-catch in client initialization to ensure proper cleanup on init failures
sdk/cosmos/azure-cosmos-tests/src/test/resources/*.xml Added CosmosNettyLeakDetectorFactory listener to all TestNG suite configuration files
sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/rx/TestSuiteBase.java Added before/after class methods to snapshot client state and report leaks
sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/TestSuiteBase.java Added before/after class methods to snapshot client state and report leaks
sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/CosmosNettyLeakDetectorFactory.java New custom Netty leak detector factory to intercept and log Netty ByteBuf leaks
sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/CosmosAsyncClientTest.java Added BeforeClass import to support leak detection setup
sdk/cosmos/azure-cosmos-encryption/src/test/java/com/azure/cosmos/encryption/TestSuiteBase.java Added before/after class methods to snapshot client state and report leaks for encryption tests
sdk/cosmos/azure-cosmos-encryption/src/test/java/com/azure/cosmos/encryption/CosmosNettyLeakDetectorFactory.java Duplicate of the main CosmosNettyLeakDetectorFactory for the encryption module

FabianMeiswinkel and others added 2 commits November 7, 2025 21:25
…mos/encryption/CosmosNettyLeakDetectorFactory.java

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@Azure Azure deleted a comment from Copilot AI Nov 7, 2025
@FabianMeiswinkel

Copy link
Copy Markdown
Member Author

@copilot please review the latest iteration again

Copilot AI commented Nov 7, 2025

Copy link
Copy Markdown
Contributor

@FabianMeiswinkel I've opened a new pull request, #47202, to work on those changes. Once the pull request is ready, I'll request review from you.

@FabianMeiswinkel

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - tests

@FabianMeiswinkel

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - spark

@FabianMeiswinkel

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - kafka

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

2 similar comments
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@FabianMeiswinkel

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - tests

@FabianMeiswinkel

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - kafka

@FabianMeiswinkel

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - spark

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

2 similar comments
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@tvaron3 tvaron3 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@FabianMeiswinkel

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - tests

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@FabianMeiswinkel

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - kafka

@FabianMeiswinkel

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - spark

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@FabianMeiswinkel FabianMeiswinkel merged commit 89dd0df into main Nov 10, 2025
123 checks passed
@FabianMeiswinkel FabianMeiswinkel deleted the users/fabianm/TestImprovements branch November 10, 2025 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants