|
36 | 36 | import java.lang.reflect.InvocationTargetException; |
37 | 37 | import java.lang.reflect.Method; |
38 | 38 | import java.util.concurrent.TimeUnit; |
| 39 | +import java.util.concurrent.atomic.AtomicBoolean; |
39 | 40 | import java.util.concurrent.locks.Condition; |
40 | 41 | import java.util.concurrent.locks.ReentrantLock; |
41 | 42 |
|
@@ -175,6 +176,108 @@ public void testShutdownRacingDispatchMustNotStrandCaller() throws Exception { |
175 | 176 | unexpectedF.get(queryImpl)); |
176 | 177 | } |
177 | 178 |
|
| 179 | + /** |
| 180 | + * Busy-worker variant of the shutdown-drop race fixed in df6f7ca |
| 181 | + * ({@code while (!shuttingDown)} -> {@code while (true)} in |
| 182 | + * {@link QueryWorker}'s run loop). Unlike |
| 183 | + * {@link #testShutdownRacingDispatchMustNotStrandCaller()} -- which only |
| 184 | + * drives the PARKED-worker branch (worker blocked in |
| 185 | + * {@code awaitUninterruptibly} before {@code shuttingDown} flips) and stays |
| 186 | + * green even with the fix reverted -- this test forces the worker THROUGH a |
| 187 | + * job's {@code runOn()} and then, on the worker thread at the exact instant |
| 188 | + * that job returns, reproduces a reused lease re-dispatching |
| 189 | + * ({@code current = q2}) racing a shutdown ({@code shuttingDown = true}), |
| 190 | + * both set before the loop re-enters the strand check. |
| 191 | + * <p> |
| 192 | + * With the fix the loop re-enters the {@code signalLock} block, observes |
| 193 | + * {@code shuttingDown}, and strands q2 (signalling its caller). With the bug |
| 194 | + * the loop exits at the top without re-reading {@code current}, so q2 is |
| 195 | + * dropped -- never run, never signalled -- and its caller's |
| 196 | + * {@code Completion.await()} would hang forever. The assertion on |
| 197 | + * {@code q2.done} fails if the fix is reverted. |
| 198 | + * <p> |
| 199 | + * The interleaving is made deterministic with a test-only worker-thread |
| 200 | + * barrier ({@code QueryWorker.busyWorkerTestHook}) instead of a sleep: |
| 201 | + * {@link QueryWorker} and {@code QueryImpl} are final and |
| 202 | + * {@code QwpQueryClient} has no test seam, so pausing between |
| 203 | + * {@code runOn()} and the loop check is the only race-free reproduction. |
| 204 | + * {@code client}/{@code pool} are null -- {@code q1.runOn(null)} throws an |
| 205 | + * NPE that {@code runLoop} catches and turns into q1's terminal signal, a |
| 206 | + * fast stand-in for a real job returning from {@code runOn()}. |
| 207 | + */ |
| 208 | + @Test(timeout = 30_000) |
| 209 | + public void testBusyWorkerShutdownStrandsReDispatchedCurrent() throws Exception { |
| 210 | + Class<?> queryImplClass = Class.forName("io.questdb.client.impl.QueryImpl"); |
| 211 | + |
| 212 | + Field lockF = QueryWorker.class.getDeclaredField("signalLock"); |
| 213 | + Field currentF = QueryWorker.class.getDeclaredField("current"); |
| 214 | + Field shuttingF = QueryWorker.class.getDeclaredField("shuttingDown"); |
| 215 | + Field threadF = QueryWorker.class.getDeclaredField("thread"); |
| 216 | + Field hookF = QueryWorker.class.getDeclaredField("busyWorkerTestHook"); |
| 217 | + for (Field f : new Field[]{lockF, currentF, shuttingF, threadF, hookF}) { |
| 218 | + f.setAccessible(true); |
| 219 | + } |
| 220 | + |
| 221 | + Field doneF = queryImplClass.getDeclaredField("done"); |
| 222 | + Field unexpectedF = queryImplClass.getDeclaredField("unexpectedError"); |
| 223 | + doneF.setAccessible(true); |
| 224 | + unexpectedF.setAccessible(true); |
| 225 | + |
| 226 | + // client == null: q1.runOn(null) throws NPE, which runLoop catches and |
| 227 | + // turns into q1's terminal signal -- a fast, deterministic stand-in for |
| 228 | + // a real job returning from runOn(). pool == null is never touched here. |
| 229 | + QueryWorker worker = new QueryWorker(null, null, 0); |
| 230 | + |
| 231 | + Constructor<?> ctor = queryImplClass.getDeclaredConstructor(QueryWorker.class); |
| 232 | + ctor.setAccessible(true); |
| 233 | + Object q1 = ctor.newInstance(new Object[]{worker}); |
| 234 | + Object q2 = ctor.newInstance(new Object[]{worker}); |
| 235 | + doneF.setBoolean(q1, false); |
| 236 | + doneF.setBoolean(q2, false); |
| 237 | + |
| 238 | + ReentrantLock lock = (ReentrantLock) lockF.get(worker); |
| 239 | + AtomicBoolean fired = new AtomicBoolean(false); |
| 240 | + |
| 241 | + // The busy-worker barrier: the FIRST time the worker returns from a |
| 242 | + // job's runOn(), simulate submit() -> dispatch() re-arming current with |
| 243 | + // q2 while shutdown() flips shuttingDown -- both set, under signalLock, |
| 244 | + // before the loop re-checks. Runs on the worker thread. |
| 245 | + Runnable hook = () -> { |
| 246 | + if (fired.compareAndSet(false, true)) { |
| 247 | + lock.lock(); |
| 248 | + try { |
| 249 | + currentF.set(worker, q2); |
| 250 | + shuttingF.setBoolean(worker, true); |
| 251 | + } catch (IllegalAccessException e) { |
| 252 | + throw new RuntimeException(e); |
| 253 | + } finally { |
| 254 | + lock.unlock(); |
| 255 | + } |
| 256 | + } |
| 257 | + }; |
| 258 | + hookF.set(worker, hook); |
| 259 | + |
| 260 | + // Pre-arm current with q1 so the worker consumes it immediately on |
| 261 | + // start (no need to wait for the await park); start() establishes the |
| 262 | + // happens-before that publishes current and the hook to the worker. |
| 263 | + currentF.set(worker, q1); |
| 264 | + |
| 265 | + Thread t = (Thread) threadF.get(worker); |
| 266 | + t.start(); |
| 267 | + |
| 268 | + t.join(5_000); |
| 269 | + Assert.assertFalse("worker thread must exit after shuttingDown=true", t.isAlive()); |
| 270 | + |
| 271 | + Assert.assertTrue( |
| 272 | + "BUG (df6f7ca regressed): the busy worker returned from runOn() with a " |
| 273 | + + "re-dispatched current!=null and shuttingDown=true, then exited the loop " |
| 274 | + + "without stranding it. q2 was never signalled; its caller's await() hangs " |
| 275 | + + "forever.", |
| 276 | + doneF.getBoolean(q2)); |
| 277 | + Assert.assertNotNull("the stranded busy-path job must record the closed-handle error", |
| 278 | + unexpectedF.get(q2)); |
| 279 | + } |
| 280 | + |
178 | 281 | /** |
179 | 282 | * Result handlers (onBatch/onEnd/onError) run inline on the worker's |
180 | 283 | * dispatch thread. The blocking lease ops -- {@code close()} and the two |
|
0 commit comments