-
-
Notifications
You must be signed in to change notification settings - Fork 289
Open
Labels
Description
Bug Description
In se.michaelthelin.spotify.SpotifyApi, the static field SIMPLE_DATE_FORMAT utilizes a ThreadLocal<SimpleDateFormat> to handle date parsing and formatting. However, there is no mechanism to call ThreadLocal.remove() to clean up the resource.
Reproduction Steps
This issue triggers when the SDK is integrated into server-side gateway applications or any environment utilizing long-lived thread pools (e.g., Spring Boot default executors, Tomcat).
Minimal reproduction concept:
ExecutorService pool = Executors.newFixedThreadPool(200);
for (int i = 0; i < 10000; i++) {
pool.submit(() -> {
try {
// Each thread in the pool initializes its own SimpleDateFormat
SpotifyApi.parseDefaultDate("2023-01-01T12:00:00");
} catch (Exception e) {
e.printStackTrace();
}
});
}
// The ThreadLocalMap of all 200 core threads will permanently hold a SimpleDateFormat instance, as .remove() is never invoked.
Impact
This Unmanaged ThreadLocal (UTL) usage leads to memory leaks. In high-concurrency environments with large thread pools, the uncollected SimpleDateFormat instances will gradually inflate the heap memory, potentially leading to performance degradation or OutOfMemoryError over time.
Suggested Fix
Since the project likely uses Java 8+, the most robust solution is to replace ThreadLocal<SimpleDateFormat> with java.time.format.DateTimeFormatter. DateTimeFormatter is immutable and inherently thread-safe, eliminating the need for ThreadLocal entirely.Reactions are currently unavailable