Skip to content

fix(memory-leaks): Add JVM shutdown hooks, fix resource leaks, fix race conditions#7

Open
maheshkumargangula wants to merge 2 commits into
developfrom
fix/memory-leaks
Open

fix(memory-leaks): Add JVM shutdown hooks, fix resource leaks, fix race conditions#7
maheshkumargangula wants to merge 2 commits into
developfrom
fix/memory-leaks

Conversation

@maheshkumargangula

Copy link
Copy Markdown
Collaborator

PR Title

fix(memory-leaks): Add JVM shutdown hooks, fix resource leaks, fix race conditions

Base Branch

develop

Summary

  • Redis connection never closed: Added JVM shutdown hook to RedisConnectionManager — gracefully calls client.shutdown() on JVM termination (like Cassandra/ES already do)
  • Kafka connections never closed: Added JVM shutdown hook to KafkaClient — flushes producer then closes both producer and consumer
  • XSSFWorkbook memory/file handle leak: Refactored ExcelFileUtil.writeToFile() to use try-with-resources for both XSSFWorkbook and FileOutputStream
  • Cassandra session race condition: Replaced non-atomic get() + put() in CassandraConnectionManagerImpl.getSession() with ConcurrentHashMap.computeIfAbsent()
  • Unbounded ES cache: Replaced unlimited HashMap for indexMap/typeMap in ElasticSearchHelper with bounded LRU LinkedHashMap (max 500 entries)
  • ObjectMapper per-call creation: Added public static final ObjectMapper MAPPER singleton to JsonUtil; updated 8 service classes to reuse it

Files Changed

  • core/sunbird-redis-utils/.../RedisConnectionManager.java
  • core/sunbird-redis-utils/.../RedisCache.java
  • core/sunbird-platform-common/.../KafkaClient.java
  • core/sunbird-platform-common/.../ExcelFileUtil.java
  • core/sunbird-platform-common/.../JsonUtil.java
  • core/sunbird-cassandra-utils/.../CassandraConnectionManagerImpl.java
  • core/sunbird-es-utils/.../ElasticSearchHelper.java
  • modules/userorg/service/... (ObjectMapper singleton adoption)

Implements Tasks

ML-01, ML-02, ML-03, ML-04, ML-06, ML-07

Test Plan

  • Build all modules: mvn clean install -DskipTests
  • Verify no new ObjectMapper() in method bodies of updated files
  • Test graceful shutdown by sending SIGTERM to the running service
  • Load test for 30 minutes and verify no memory growth in ES/Cassandra cache

…onditions

- Add JVM shutdown hook for Redis client graceful cleanup (ML-03)
- Add JVM shutdown hook for Kafka producer/consumer cleanup (ML-02)
- Fix XSSFWorkbook resource leak using try-with-resources in ExcelFileUtil (ML-04)
- Fix Cassandra session race condition using ConcurrentHashMap.computeIfAbsent (ML-07)
- Replace unbounded static HashMap with bounded LRU LinkedHashMap in ElasticSearchHelper (ML-01)
- Add shared ObjectMapper singleton to JsonUtil, replace 8 per-call instantiations (ML-06)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants