From 9eb4567a10156552127aef84ffe29c8385125498 Mon Sep 17 00:00:00 2001 From: JackieTien97 Date: Sat, 7 Nov 2026 20:28:09 +0800 Subject: [PATCH] Fix race in SessionSeedNodeFallbackTest discovery buffer checks SessionPool schedules its first NodesSupplier refresh immediately on a real executor, but assertDiscoveryBufferThreshold() called run() and inspected the discovery connection right away. run() returns without waiting while another refresh holds refreshLock, so the test could see that background refresh half done: a null client, or the temporary 28-byte write buffer of an in-flight openSession frame. Both failures were seen on Windows CI. Hold refreshLock around the refresh and the buffer assertions. The lock is reentrant, so run() still refreshes on the test thread, while an in-flight background refresh finishes first and cannot use the connection during the assertions. --- .../session/SessionSeedNodeFallbackTest.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/iotdb-client/session/src/test/java/org/apache/iotdb/session/SessionSeedNodeFallbackTest.java b/iotdb-client/session/src/test/java/org/apache/iotdb/session/SessionSeedNodeFallbackTest.java index ad5767a07c60..bbcdda2abeda 100644 --- a/iotdb-client/session/src/test/java/org/apache/iotdb/session/SessionSeedNodeFallbackTest.java +++ b/iotdb-client/session/src/test/java/org/apache/iotdb/session/SessionSeedNodeFallbackTest.java @@ -72,6 +72,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.ReentrantLock; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -574,13 +575,24 @@ private void assertLargeRequestBuffer(Session session, int threshold) { } } - private void assertDiscoveryBufferThreshold(SessionPool pool, int threshold) { + private void assertDiscoveryBufferThreshold(SessionPool pool, int threshold) + throws InterruptedException { NodesSupplier current = Whitebox.getInternalState(pool, "availableNodes"); - current.run(); - ThriftConnection connection = Whitebox.getInternalState(current, "client"); - AutoScalingBufferWriteTransport output = assertTransportBufferThreshold(connection, threshold); - if (threshold == 0) { - assertEquals(32, output.getBuffer().length); + // The pool starts its first discovery refresh immediately, and run() returns at once while + // another refresh holds refreshLock. Holding the reentrant lock waits for that refresh and + // keeps the discovery connection idle while run() refreshes it and its buffers are checked. + ReentrantLock refreshLock = Whitebox.getInternalState(current, "refreshLock"); + assertTrue(refreshLock.tryLock(10, TimeUnit.SECONDS)); + try { + current.run(); + ThriftConnection connection = Whitebox.getInternalState(current, "client"); + AutoScalingBufferWriteTransport output = + assertTransportBufferThreshold(connection, threshold); + if (threshold == 0) { + assertEquals(32, output.getBuffer().length); + } + } finally { + refreshLock.unlock(); } }