This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch fix/CAMEL-24092 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 2563cc68af8f933c21e4d8556e0f3ccf46f24e3f Author: Claus Ibsen <[email protected]> AuthorDate: Wed Jul 15 20:44:48 2026 +0200 CAMEL-24092: camel-file - readLock=fileLock must return false on IOException Fix two bugs in FileLockExclusiveReadLockStrategy: 1. When IOException occurs (file deleted between poll and lock, permission error, Windows AV lock) with timeout > 0, the method fell through to return true with no OS lock held and the marker file already deleted by the finally block - enabling duplicate consumption by competing consumers. Now always returns false on IOException. 2. doReleaseExclusiveReadLock used the wrong property key (FILE_LOCK_EXCLUSIVE_LOCK instead of FILE_LOCK_RANDOM_ACCESS_FILE) to retrieve the RandomAccessFile, so it was always null. Co-Authored-By: Claude Opus 4.6 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../FileLockExclusiveReadLockStrategy.java | 23 ++---- .../FileLockExclusiveReadLockStrategyTest.java | 87 ++++++++++++++++++++++ 2 files changed, 94 insertions(+), 16 deletions(-) diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java b/components/camel-file/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java index 76df01ba2345..aadca129e214 100644 --- a/components/camel-file/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java +++ b/components/camel-file/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java @@ -116,9 +116,8 @@ public class FileLockExclusiveReadLockStrategy extends MarkerFileExclusiveReadLo // somehow hold a lock to a file // such as AntiVirus or MS Office that has special locks for it's // supported files - if (handleIOException(e)) { - return false; - } + handleIOException(e); + return false; } finally { // close channels if we did not grab the lock if (!exclusive) { @@ -139,19 +138,10 @@ public class FileLockExclusiveReadLockStrategy extends MarkerFileExclusiveReadLo return true; } - private boolean handleIOException(IOException e) { - if (timeout == 0) { - // if not using timeout, then we cant retry, so return false - return true; - } - LOG.debug("Cannot acquire read lock. Will try again.", e); - boolean interrupted = sleep(); - if (interrupted) { - // we were interrupted while sleeping, we are likely being - // shutdown so return false - return true; + private void handleIOException(IOException e) { + if (timeout > 0) { + LOG.debug("Cannot acquire read lock due to an IOException, will skip the file.", e); } - return false; } @Override @@ -162,7 +152,8 @@ public class FileLockExclusiveReadLockStrategy extends MarkerFileExclusiveReadLo FileLock lock = exchange.getProperty(asExclusiveReadLockKey(file, Exchange.FILE_LOCK_EXCLUSIVE_LOCK), FileLock.class); RandomAccessFile rac - = exchange.getProperty(asExclusiveReadLockKey(file, Exchange.FILE_LOCK_EXCLUSIVE_LOCK), RandomAccessFile.class); + = exchange.getProperty(asExclusiveReadLockKey(file, Exchange.FILE_LOCK_RANDOM_ACCESS_FILE), + RandomAccessFile.class); Channel channel = exchange.getProperty(asExclusiveReadLockKey(file, Exchange.FILE_LOCK_CHANNEL_FILE), FileChannel.class); diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategyTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategyTest.java new file mode 100644 index 000000000000..508748bc73ce --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategyTest.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.file.strategy; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; + +import org.apache.camel.Exchange; +import org.apache.camel.component.file.FileComponent; +import org.apache.camel.component.file.GenericFile; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.support.DefaultExchange; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.OS; +import org.junit.jupiter.api.io.TempDir; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@DisabledOnOs(OS.WINDOWS) +public class FileLockExclusiveReadLockStrategyTest { + + @TempDir + Path tempDir; + + private DefaultCamelContext camelContext; + + @BeforeEach + void setUp() throws Exception { + camelContext = new DefaultCamelContext(); + camelContext.start(); + } + + @AfterEach + void tearDown() { + if (camelContext != null) { + camelContext.stop(); + } + } + + @Test + void testIOExceptionReturnsFalseAndCleansMarker() throws Exception { + Path target = tempDir.resolve("testfile.txt"); + Files.writeString(target, "test content"); + + // make the file read-only so RandomAccessFile("rw") throws IOException + assertTrue(target.toFile().setWritable(false)); + + FileLockExclusiveReadLockStrategy strategy = new FileLockExclusiveReadLockStrategy(); + strategy.setTimeout(10000); + + GenericFile<File> genericFile = new GenericFile<>(); + genericFile.setFile(target.toFile()); + genericFile.setAbsoluteFilePath(target.toAbsolutePath().toString()); + genericFile.setFileName(target.getFileName().toString()); + + Exchange exchange = new DefaultExchange(camelContext); + + boolean acquired = strategy.acquireExclusiveReadLock(null, genericFile, exchange); + + assertFalse(acquired, "acquireExclusiveReadLock should return false when IOException occurs"); + + File markerFile = new File(target.toAbsolutePath() + FileComponent.DEFAULT_LOCK_FILE_POSTFIX); + assertFalse(markerFile.exists(), "marker file should be cleaned up after failed lock acquisition"); + + // restore writable so @TempDir cleanup succeeds + target.toFile().setWritable(true); + } +}
