lightzhao created KAFKA-19874:
---------------------------------

             Summary: Fix resource leak in TopicBasedRemoteLogMetadataManager 
initialization
                 Key: KAFKA-19874
                 URL: https://issues.apache.org/jira/browse/KAFKA-19874
             Project: Kafka
          Issue Type: Bug
          Components: core
    Affects Versions: 4.1.0
            Reporter: lightzhao


## Problem
There is a resource leak in the TopicBasedRemoteLogMetadataManager 
initialization 
process. If ProducerManager is created successfully but ConsumerManager 
creation 
fails, the ProducerManager instance is not properly closed, leading to resource 
leaks (e.g., unclosed Kafka producer, threads, network connections).

## Current Code (Problematic)
lock.writeLock().lock();
try {
    producerManager = new ProducerManager(rlmmConfig, partitioner);
    consumerManager = new ConsumerManager(rlmmConfig, 
remotePartitionMetadataStore, partitioner, time);
    consumerManager.startConsumerThread();
    // If exception occurs here, producerManager is not closed
} catch (Exception e) {
    log.error("Encountered error while initializing producer/consumer", e);
    initializationFailed = true;
} finally {
    lock.writeLock().unlock();
}## Solution
Use temporary variables to hold manager instances and only assign to instance 
variables after successful initialization. Clean up temporary resources in the 
catch block if any exception occurs.

## Impact
- Prevents resource leaks during initialization failures
- Ensures proper cleanup of Kafka clients
- Improves system stability and resource management



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to