|
73 | 73 | import io.grpc.ServerCall; |
74 | 74 | import io.grpc.ServerCallHandler; |
75 | 75 | import io.grpc.ServerInterceptor; |
| 76 | +import io.grpc.Status; |
76 | 77 | import java.io.IOException; |
77 | 78 | import java.time.Duration; |
78 | 79 | import java.time.Instant; |
@@ -319,6 +320,89 @@ void awaitTerminatedReturnsTrueAfterSessionsDrain() |
319 | 320 | assertThat(sessionPool.awaitTerminated(Duration.ofSeconds(10))).isTrue(); |
320 | 321 | } |
321 | 322 |
|
| 323 | + @Test |
| 324 | + void closeCancelsInFlightVRpcAndDrains() throws InterruptedException { |
| 325 | + // Park the session-open response in the client interceptor so the vRPC cannot drain to a |
| 326 | + // ready session and sits in pendingRpcs. Then close the pool and verify (a) the queued |
| 327 | + // vRPC's listener gets a CANCELLED onClose with the "SessionPool closed:" description from |
| 328 | + // SessionPoolImpl.close()'s cancellation loop, (b) awaitTerminated returns true (the |
| 329 | + // OPENING session is driven to CLOSED by sessions.close(req), not parked until the |
| 330 | + // watchdog tick), and (c) no second onClose arrives once the delayed responses finally |
| 331 | + // flush past the interceptor. |
| 332 | + SessionPool<OpenFakeSessionRequest> testSessionPool = null; |
| 333 | + try (ChannelPool delayedPool = |
| 334 | + new SingleChannelPool( |
| 335 | + Suppliers.ofInstance( |
| 336 | + Grpc.newChannelBuilderForAddress( |
| 337 | + "localhost", server.getPort(), InsecureChannelCredentials.create()) |
| 338 | + .intercept(new DelayedClientInterceptor(Duration.ofMillis(100))) |
| 339 | + .build()))) { |
| 340 | + |
| 341 | + delayedPool.start(); |
| 342 | + |
| 343 | + testSessionPool = |
| 344 | + new SessionPoolImpl<>( |
| 345 | + metrics, |
| 346 | + FeatureFlags.getDefaultInstance(), |
| 347 | + CLIENT_INFO, |
| 348 | + configManager, |
| 349 | + delayedPool, |
| 350 | + CallOptions.DEFAULT, |
| 351 | + FakeDescriptor.FAKE_SESSION, |
| 352 | + "fake-pool-in-flight", |
| 353 | + testTimer); |
| 354 | + |
| 355 | + testSessionPool.start(OpenFakeSessionRequest.getDefaultInstance(), new Metadata()); |
| 356 | + |
| 357 | + VRpc<SessionFakeScriptedRequest, SessionFakeScriptedResponse> vrpc = |
| 358 | + testSessionPool.newCall(FakeDescriptor.SCRIPTED); |
| 359 | + CopyOnWriteArrayList<VRpcResult> closes = new CopyOnWriteArrayList<>(); |
| 360 | + CountDownLatch firstClose = new CountDownLatch(1); |
| 361 | + Duration deadline = Duration.ofSeconds(10); |
| 362 | + vrpc.start( |
| 363 | + SessionFakeScriptedRequest.getDefaultInstance(), |
| 364 | + VRpcCallContext.create( |
| 365 | + Deadline.after(deadline.toMillis(), TimeUnit.MILLISECONDS), true, vrpcTracer), |
| 366 | + new VRpc.VRpcListener<SessionFakeScriptedResponse>() { |
| 367 | + @Override |
| 368 | + public void onMessage(SessionFakeScriptedResponse msg) {} |
| 369 | + |
| 370 | + @Override |
| 371 | + public void onClose(VRpcResult result) { |
| 372 | + closes.add(result); |
| 373 | + firstClose.countDown(); |
| 374 | + } |
| 375 | + }); |
| 376 | + |
| 377 | + // vRPC is now queued in pendingRpcs behind the parked session-open. Close the pool. |
| 378 | + testSessionPool.close( |
| 379 | + CloseSessionRequest.newBuilder() |
| 380 | + .setReason(CloseSessionRequest.CloseSessionReason.CLOSE_SESSION_REASON_USER) |
| 381 | + .setDescription("close while in flight") |
| 382 | + .build()); |
| 383 | + |
| 384 | + // Cancellation hops through the vRPC's per-op SerializingExecutor — not the gRPC |
| 385 | + // listener thread — so it should arrive promptly even while the interceptor is asleep. |
| 386 | + assertThat(firstClose.await(2, TimeUnit.SECONDS)).isTrue(); |
| 387 | + assertThat(closes).hasSize(1); |
| 388 | + VRpcResult result = closes.get(0); |
| 389 | + assertThat(result).status().code().isEqualTo(Status.Code.CANCELLED); |
| 390 | + assertThat(result).status().description().contains("SessionPool closed:"); |
| 391 | + |
| 392 | + // OPENING session drains via sessions.close(req) once the delayed onMessages flush. |
| 393 | + assertThat(testSessionPool.awaitTerminated(Duration.ofSeconds(10))).isTrue(); |
| 394 | + |
| 395 | + // Regression guard: the delayed session-open responses arriving post-cancellation must |
| 396 | + // not produce a phantom second onClose on the cancelled vRPC's listener. |
| 397 | + Thread.sleep(200); |
| 398 | + assertThat(closes).hasSize(1); |
| 399 | + } finally { |
| 400 | + if (testSessionPool != null) { |
| 401 | + testSessionPool.close(CloseSessionRequest.getDefaultInstance()); |
| 402 | + } |
| 403 | + } |
| 404 | + } |
| 405 | + |
322 | 406 | @Test |
323 | 407 | void pendingVRpcOnClosedPoolDoesNotLeakDeadlineMonitor() throws InterruptedException { |
324 | 408 | // Regression: PendingVRpc.start used to arm the deadline timer before the pool-state |
|
0 commit comments