graphql-java / graphql-java/java-dataloader
[Bug] Memory Leak and Stats Pollution via unmanaged ThreadLocal in ThreadLocalStatisticsCollector
Nessuno ha ancora preso questa issue.
- Lingua principale
- Java
- Stelle
- 525
- Fork
- 101
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
Describe the bug
There is a potential Unintentional ThreadLocal Leak (UTL) in ThreadLocalStatisticsCollector. The class relies on consumers manually calling resetThread() at "request boundaries" to clear the ThreadLocal<SimpleStatisticsCollector>.
However, in typical GraphQL environments (reactive streams, async gateways, or standard thread pools), threads are heavily reused. If an unhandled exception occurs or a developer forgets to call resetThread(), the SimpleStatisticsCollector object is permanently retained by the worker thread.
Impact:
- Data Pollution: Subsequent requests processed by the dirty thread will inherit the accumulated statistics of previous requests, leading to completely distorted metrics.
- Memory Leak (Type I UTL): As more threads in the pool retain these un-cleared statistics objects over time, the heap usage will grow linearly, potentially leading to an
OutOfMemoryErrorin high-throughput applications.
To Reproduce
Here is a simplified code example demonstrating the data pollution in a thread-pool environment when resetThread() is missed (e.g., bypassed due to an exception):
import org.dataloader.stats.ThreadLocalStatisticsCollector;
import java.util.concurrent.*;
public class UTLReproduction {
public static void main(String[] args) throws Exception {
ThreadLocalStatisticsCollector collector = new ThreadLocalStatisticsCollector();
// Simulate a web server with a reusable thread pool
ExecutorService threadPool = Executors.newFixedThreadPool(1);
// Request 1: Execution completes but resetThread() is missed (e.g. exception thrown)
threadPool.submit(() -> {
collector.incrementLoadCount();
// Developer forgets to put collector.resetThread() in a finally block
}).get();
// Request 2: A new incoming request reuses the same dirty thread
threadPool.submit(() -> {
long loadCount = collector.getStatistics().getLoadCount();
// BUG: loadCount is 1 instead of 0! The new request is polluted by Request 1.
System.out.println("New Request Load Count (Expected 0): " + loadCount);
}).get();
threadPool.shutdown();
}
}
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia leggendo ThreadLocalStatisticsCollector, in particolare il suo utilizzo di ThreadLocal, getStatistics() e resetThread(). Esegui la riproduzione fornita con un fixed-thread-pool per verificare se le statistiche persistono tra le requests quando resetThread() viene omesso. Done deve includere un modo deciso per prevenire la contaminazione e la ritenzione tra le requests, con test che coprano lo scenario riprodotto.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- java
- Ambito
- backend, observability
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Tranquilla
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 35/100