[Bug] Potential OOM in FileSource due to unbounded LinkedBlockingQueue (UBSCQ)
Search before reporting
- [x] I searched in the issues and found nothing similar.
Read release policy
- [x] I understand that unsupported versions don't get bug fixes. I will attempt to reproduce the issue on a supported version of Pulsar client and Pulsar broker.
User environment
Broker version: Master Java version: Java8
Issue Description
The FileSource connector (org.apache.pulsar.io.file.FileSource) initializes internal blocking queues (workQueue, inProcess, recentlyProcessed) using the default new LinkedBlockingQueue<>() constructor.
Internal queues should have a bounded capacity to provide backpressure. This prevents memory exhaustion when the producer (file listing) creates tasks faster than the consumer (file processing) can handle them.
The default constructor sets the capacity to Integer.MAX_VALUE. If the FileListingThread scans files significantly faster than the FileConsumerThread can process them (e.g., large files or slow I/O), the workQueue will grow indefinitely, eventually leading to a java.lang.OutOfMemoryError.
This is a classic "Unbounded Size Cache Queue" (UBSCQ) pattern. It lacks protection against traffic spikes or slow consumption rates.
Error messages
java.lang.OutOfMemoryError: Java heap space
at java.util.concurrent.LinkedBlockingQueue.offer(LinkedBlockingQueue.java:416)
at org.apache.pulsar.io.file.FileListingThread.run(FileListingThread.java:...)
Reproducing the issue
-
Configure a FileSource connector to monitor a directory.
-
Generate a scenario where the source directory contains a massive number of files (high production rate).
-
Ensure the file processing logic is slower than the disk listing speed (simulation of backpressure accumulation).
-
Monitor the JVM Heap memory; the workQueue object size will grow until OOM occurs.
Relevant code: Location: pulsar-io/file/src/main/java/org/apache/pulsar/io/file/FileSource.java
// Line 39-41: Queues are unbounded
private final BlockingQueue
// Line 49: ThreadPool is also unbounded executor = Executors.newFixedThreadPool(fileConfig.getNumWorkers() + 2);
Additional information
Suggestion for fix: Replace the unbounded initialization with a configurable or fixed capacity to enable blocking behavior (backpressure) when the queue is full.
// Example fix
private final BlockingQueue
This ensures the FileListingThread blocks when the consumer cannot keep up, preventing OOM.
Are you willing to submit a PR?
- [x] I'm willing to submit a PR!
I’d like to take this up!
The OutOfMemoryError is clearly due to the lack of backpressure in the internal queues. I plan to:
Replace the unbounded LinkedBlockingQueue instances with bounded versions. Update FileSourceConfig to allow users to configure the queue capacity (with a sensible default). Ensure the FileListingThread correctly blocks/pauses when the queues are full.
I'll submit a PR for this shortly