shashank created CAMEL-27567:
--------------------------------
Summary: camel-file - FileLockClusterView: a lock file open that
timed out leaves a file open that releases the leader's lock when the garbage
collector closes it, so a member in another JVM can become a second leader
Key: CAMEL-27567
URL: https://issues.apache.org/jira/browse/CAMEL-27567
Project: Camel
Issue Type: Bug
Components: camel-file
Reporter: shashank
Assignee: shashank
h3. What happens
{{FileLockClusterView.acquireLock}} opens the lock file through the cluster
task executor. When an attempt does not finish within
{{clusterDataTaskTimeout}} (a slow network mount),
{{FileLockClusterTaskExecutor}} calls {{cancel(true)}} on the
{{CompletableFuture}} and starts the next attempt. Cancelling a
{{CompletableFuture}} does not interrupt the task, so the first attempt keeps
running. The retry opens the file, the view locks it and becomes LEADER. The
first attempt then returns a second {{RandomAccessFile}} on the lock file that
nothing references.
The file locks are locks of the process (POSIX fcntl on Linux and macOS):
closing any channel of the file releases all the locks of the process on it
(see the {{FileLock}} javadoc and CAMEL-27322). After a garbage collection the
JDK closes the unreferenced file, and the lock of the leader is gone at OS
level. The leader does not notice: {{lockIsValid}} checks
{{FileLock.isValid()}}, which stays true, and that the file exists. From then
on a member in another JVM that finds the leader's heartbeat stale (a GC pause,
the scheduler thread busy with the retries of another view, a slow I/O call)
takes the lock and becomes LEADER too. The lock is meant to keep it out in
exactly that case.
h3. How to reproduce
A view in the test JVM and a member in a child JVM on one local directory, task
timeout 300 ms. The first attempt to open the lock file in the test JVM waits
past the timeout and then opens the file. The view becomes LEADER with the
retry, the late attempt opens the file, and after one {{System.gc()}} an fcntl
probe from another process gets the lock while the view still reports LEADER.
Stalling the view's checks for 4 seconds then makes the child JVM LEADER too.
Found with a TLA+ model of the file lock cluster view (an attempt that keeps
running after its timeout, the close of its file by the garbage collector), and
reproduced against the real code.
h3. Fix
The view never leaves a file on the lock file open for the garbage collector.
Each attempt to open the lock file is tracked: the files of attempts that
finished during the call, other than the one returned, are closed before the
lock is tried. While an attempt that timed out is still running, the view does
not try the lock and keeps the lock file registered in the JVM-wide registry of
CAMEL-27322, so no other view of the JVM tries it either. The late attempt
closes its file as soon as it has opened it, which releases nothing because no
view of the JVM holds the lock then. Closing the late file only when it arrives
while the view holds the lock would release the lock just the same, and a check
of the lock from inside the JVM cannot see the OS lock ({{FileLock.isValid()}}
stays true and {{tryLock}} of the same JVM throws
{{OverlappingFileLockException}}), so the view waits instead. The start of such
a wait is logged at WARN, naming the lock file.
h3. Trade-off
If an open of the lock file never returns (a hung mount), no view of that JVM
tries the lock again until the JVM restarts: the count of running opens and the
JVM-wide registry entry both survive a stop and start of the service or of the
CamelContext. The view reports FOLLOWER meanwhile, and a member in another JVM
on a healthy mount takes the leadership, but a single-member cluster stays
without a leader. In exactly that case the current code is safe and live: no
late file ever arrives, so nothing is left to the garbage collector, and the
retry takes the lock. So the fix trades liveness on a mount where one open
hangs for good (and a later one returns) for never having a second leader on a
slow mount. Keeping the late file open while the view holds the lock and
closing it when the lock is released would keep the member live in that case,
but it needs the hand-over of the late file on every release path (lost lock,
stop, a try that is not published), changes the paths the fix was checked
against, and a new CamelContext of the same JVM would still have to wait for
the hung open, as closing its file would release that context's lock.
h3. Depends on CAMEL-27566
The fix contains the one-line change of CAMEL-27566 (the backoff of a member
that failed to take the leadership back lasts one check), with the same text,
so it does not depend on the merge order. Without it, a member that lost the
leadership and whose open of the lock file then timed out (acquireLock returns
false while the open runs) keeps its own id as the previous observation and
stays in that backoff for good: a single-member cluster never has a leader
again. A backport to 4.18.x or 4.14.x needs CAMEL-27322 and CAMEL-27566 too.
_Filed with Claude Code on behalf of allthingssecurity._
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)