gnodet opened a new pull request, #30432:
URL: https://github.com/apache/camel/pull/30432

   ## Summary
   
   Fix two race conditions in `KubernetesInformerLoopTest` that caused 4 hard 
failures and 20 flaky runs on `main` CI since the test was introduced.
   
   ## Root Cause
   
   **Race 1 — `awaitOpenWatches(2)` timeout:**
   
   `WatchControlDispatcher.awaitOpenWatches(int count)` used 
`Semaphore.tryAcquire(count, 20, SECONDS)`. This acquires exactly `count` 
permits atomically and then re-releases them, encoding a cumulative watch count 
in the semaphore permit pool. While technically correct, the semantics were 
fragile: `tryAcquire(2)` can only succeed once 2 permits are simultaneously 
available, so if `awaitOpenWatches(1)` was called first (acquiring and 
re-releasing 1 permit), the subsequent `awaitOpenWatches(2)` needed 2 permits 
at once. Under CI load — where fabric8's internal retry backoff combined with 
sequential handler dispatch could delay the second watch opening — the 20 s 
window was occasionally missed.
   
   **Race 2 — `server.destroy()` TimeoutException:**
   
   `tearDown()` closed the Kubernetes client *after* the loop thread was joined:
   
   ```
   service.stop() → loop.stop() → thread.join() → client.close() → 
server.destroy()
   ```
   
   When the informer received the invalid-object error, fabric8's internal 
executor scheduled a relist+rewatch. After the handler finished 
(`release.countDown()`), the reconnect opened watch #2. During teardown, 
`loop.stop()` stopped the informer, but fabric8's internal executor — which is 
shut down lazily by `client.close()` — could schedule yet another reconnect 
attempt between `loop.stop()` returning and `client.close()` executing. The new 
WebSocket connection reached the mock server while `server.destroy()` was 
already in `MockWebServer.await()`, causing a `TimeoutException`.
   
   ## Fix
   
   **Race 1 — Replace Semaphore with Lock + Condition:**
   
   Replace the two Semaphore fields (`openWatches`, `closedWatches`) with 
monotonically increasing counters (`totalOpenWatches`, `totalClosedWatches`) 
guarded by a `ReentrantLock` and two `Condition` variables. 
`awaitOpenWatches(N)` now loops on `totalOpenWatches < N` and uses a 
deadline-based `Condition.await(remaining, NANOSECONDS)`. This correctly 
handles:
   - Watch N opening before `awaitOpenWatches(N)` is called (counter already ≥ 
N → returns immediately)
   - Spurious wakeups (re-checks the condition after each wakeup)
   - The cumulative "at least N watches opened" semantics used by the test
   
   **Race 2 — Fix tearDown() ordering:**
   
   Change the teardown sequence to:
   
   ```
   service.stop() → loop.stop() → client.close() → thread.join() → 
server.destroy()
   ```
   
   `loop.stop()` signals the loop to stop and calls `informer.stop()` to tell 
fabric8 to stop the informer. `client.close()` immediately terminates all 
active WebSocket connections at the OkHttp level, ensuring no connection is 
live when `server.destroy()` runs. The loop thread is then joined (it exits 
quickly because `isRunAllowed()` is false and the informer was stopped), and 
only then is the server destroyed.
   
   ## Tests
   
   All 7 tests in `KubernetesInformerLoopTest` pass cleanly across multiple 
runs. The specific test that was flaky 
(`testUpdateQueuedWhileTheHandlerIsBusyIsReportedAfterAWatchError`) no longer 
shows the `awaitOpenWatches` timeout or `server.destroy()` failure.
   
   Fixes CAMEL-27240
   
   ---
   _Hermes Agent (Claude Sonnet 4.6) on behalf of Guillaume Nodet_
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to