This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new e89e24cb2e62 CAMEL-27091: camel-mongodb-gridfs - a stopped consumer no
longer leaves a thread waiting for its polls for ever (#30307)
e89e24cb2e62 is described below
commit e89e24cb2e62d868f4851360f0c5343792e2c95b
Author: allthingssecurity <[email protected]>
AuthorDate: Wed Nov 18 17:50:47 2026 +0530
CAMEL-27091: camel-mongodb-gridfs - a stopped consumer no longer leaves a
thread waiting for its polls for ever (#30307)
The consumer starts its polls from a thread of its own executor: run()
builds a
background task with an unlimited duration and runs it, which schedules the
polls on the task executor and waits until the task ends. A poll never ends
the task (it returns false whatever it found, also when it fails), so only
an
exception thrown out of a poll released that thread.
The stop shuts the task executor down, which cancels the scheduled polls,
and
then shuts down the executor without interrupting it. Nothing released the
thread in run(): each stop of the consumer (a route stop and start, a
context
reload) left one thread waiting for ever, and the task stayed in the task
registry.
run() now schedules the polls instead of running the task and waiting for
it,
and keeps the task, which the stop cancels without interrupting a poll that
runs (it routes the files it found on its thread). A run() that sets the
task
after the stop looked for it sees the stop and does not schedule the polls.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../component/mongodb/gridfs/GridFsConsumer.java | 38 ++++++++++++++--------
.../gridfs/GridFsConsumerTimestampTest.java | 24 ++++++++++++++
2 files changed, 48 insertions(+), 14 deletions(-)
diff --git
a/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/mongodb/gridfs/GridFsConsumer.java
b/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/mongodb/gridfs/GridFsConsumer.java
index 896c6366324b..f1a897b30bff 100644
---
a/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/mongodb/gridfs/GridFsConsumer.java
+++
b/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/mongodb/gridfs/GridFsConsumer.java
@@ -21,7 +21,6 @@ import java.time.Duration;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
-import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import com.mongodb.BasicDBObject;
@@ -40,7 +39,7 @@ import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.support.DefaultConsumer;
import org.apache.camel.support.SynchronizationAdapter;
-import org.apache.camel.support.task.BlockingTask;
+import org.apache.camel.support.task.BackgroundTask;
import org.apache.camel.support.task.Tasks;
import org.apache.camel.support.task.budget.Budgets;
import org.apache.camel.util.IOHelper;
@@ -62,13 +61,12 @@ public class GridFsConsumer extends DefaultConsumer
implements Runnable {
private final GridFsEndpoint endpoint;
private volatile ScheduledExecutorService taskExecutor;
- private volatile ExecutorService executor;
// the polls ask for the files uploaded after this date. With the
TimeStamp and PersistentTimestamp strategies it
// is the upload date of the newest file that was routed, kept between the
polls. The strategies with a file
// attribute keep the start date, because the attribute already makes sure
that a file is routed once, and a file
// whose upload date is older than one that was routed already (another
uploader, clock skew) is still found.
- // Two threads use it: run() sets the start value on the thread of the
executor, before it schedules the polls;
- // the polls then read and move it on the thread of the taskExecutor.
+ // Two threads use it: run() sets the start value on the thread that
starts the consumer, before it schedules the
+ // polls; the polls then read and move it on the thread of the
taskExecutor.
private volatile Date fromDate;
// the collection and the document of the persistent timestamp, read by
the first poll that can reach MongoDB.
// run() resets them before it schedules the polls, the polls then use
them on the thread of the taskExecutor
@@ -80,6 +78,9 @@ public class GridFsConsumer extends DefaultConsumer
implements Runnable {
// failure is logged at DEBUG instead of being reported again, so a file
that fails at every poll is reported once,
// as for the failures of the polls. It holds the files of one poll only,
so it does not grow with the bucket
private volatile Set<BsonValue> lastFailedFiles = Set.of();
+ // the task that runs the polls, which a stop cancels: once taskExecutor
is shut down no poll runs, and a poll
+ // never ends the task on its own
+ private volatile BackgroundTask pollTask;
public GridFsConsumer(GridFsEndpoint endpoint, Processor processor) {
super(endpoint, processor);
@@ -89,14 +90,17 @@ public class GridFsConsumer extends DefaultConsumer
implements Runnable {
@Override
protected void doStop() throws Exception {
super.doStop();
+ BackgroundTask task = pollTask;
+ if (task != null) {
+ // without interrupt: a poll that is running finishes the file it
is routing, then stops, as its loop
+ // checks isStarted()
+ task.cancel(false);
+ pollTask = null;
+ }
if (taskExecutor != null) {
endpoint.getCamelContext().getExecutorServiceManager().shutdown(taskExecutor);
taskExecutor = null;
}
- if (executor != null) {
-
endpoint.getCamelContext().getExecutorServiceManager().shutdown(executor);
- executor = null;
- }
}
@Override
@@ -104,9 +108,8 @@ public class GridFsConsumer extends DefaultConsumer
implements Runnable {
super.doStart();
taskExecutor = endpoint.getCamelContext().getExecutorServiceManager()
.newSingleThreadScheduledExecutor(this, "GridFsPollingTask");
- executor =
endpoint.getCamelContext().getExecutorServiceManager().newFixedThreadPool(this,
endpoint.getEndpointUri(),
- 1);
- executor.execute(this);
+ // run() only schedules the polls and returns, so it runs on this
thread
+ run();
}
@Override
@@ -132,7 +135,7 @@ public class GridFsConsumer extends DefaultConsumer
implements Runnable {
fromDate = startDate;
}
- BlockingTask task = Tasks.backgroundTask()
+ BackgroundTask task = Tasks.backgroundTask()
.withBudget(Budgets.iterationTimeBudget()
// maxIterations defaults to Integer.MAX_VALUE
(effectively unlimited)
.withInterval(Duration.ofMillis(endpoint.getDelay()))
@@ -143,7 +146,14 @@ public class GridFsConsumer extends DefaultConsumer
implements Runnable {
.withName("GridFsPolling")
.build();
- task.run(endpoint.getCamelContext(), () -> poll(usesTimestamp,
persistsTimestamp, usesAttribute, startDate));
+ pollTask = task;
+ if (!isRunAllowed()) {
+ // stopped before the task was set, so the stop could not cancel it
+ return;
+ }
+ // scheduled, not run: run() would wait on this thread for the end of
the polls, which never comes
+ task.schedule(endpoint.getCamelContext(),
+ () -> poll(usesTimestamp, persistsTimestamp, usesAttribute,
startDate));
}
/**
diff --git
a/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/GridFsConsumerTimestampTest.java
b/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/GridFsConsumerTimestampTest.java
index 58f4300dc6a3..13895dfb54c7 100644
---
a/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/GridFsConsumerTimestampTest.java
+++
b/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/GridFsConsumerTimestampTest.java
@@ -48,6 +48,8 @@ import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.ExceptionHandler;
+import org.apache.camel.support.PluginHelper;
+import org.apache.camel.support.task.TaskManagerRegistry;
import org.bson.BsonArray;
import org.bson.BsonDocument;
import org.bson.BsonObjectId;
@@ -339,6 +341,28 @@ class GridFsConsumerTimestampTest {
assertEquals(List.of(), reported, "no poll follows, the failure must
not be reported as one that is retried");
}
+ @Test
+ void stopCancelsThePolls() throws Exception {
+ TaskManagerRegistry registry =
PluginHelper.getTaskManagerRegistry(context.getCamelContextExtension());
+ int tasksBefore = registry.getSize();
+ AtomicInteger pollCount = new AtomicInteger();
+ onPoll = pollCount::incrementAndGet;
+ // polls far apart, so that the stop comes between two polls
+ startConsumer(QueryStrategy.TimeStamp, 500);
+ awaitFullPolls(1);
+ assertEquals(tasksBefore + 1, registry.getSize(), "the polls run as a
task of the registry");
+
+ consumer.stop();
+
+ // a task that is not cancelled stays in the registry, as only one of
its runs takes it out
+ assertEquals(tasksBefore, registry.getSize(), "the stopped consumer
left its task in the registry");
+ int pollsAtStop = pollCount.get();
+ CountDownLatch nextPoll = new CountDownLatch(1);
+ polls = nextPoll;
+ assertFalse(nextPoll.await(2, TimeUnit.SECONDS), "the stopped consumer
polled again");
+ assertEquals(pollsAtStop, pollCount.get(), "the stopped consumer
polled again");
+ }
+
@ParameterizedTest
@EnumSource(value = QueryStrategy.class,
names = { "FileAttribute", "TimeStampAndFileAttribute",
"PersistentTimestampAndFileAttribute" })