Repository navigation
Commit d9a8eef
authored
fix(spanner): prevent statement cancellation race in AbstractBaseUnitOfWork (#14283)
In `AbstractBaseUnitOfWork.executeStatementAsync`, statements were
submitted asynchronously to `statementExecutor` before storing the
resulting future in `currentlyRunningStatementFuture`. Under thread
contention, the query RPC can reach the mock server and trigger
`cancel()` before the future is assigned, causing `cancel()` to silently
drop the cancellation request and resulting in statement timeouts in CI
tests such as
`StatementTimeoutTest#testCancelReadWriteAutocommitMultipleStatements`.
Furthermore, submitting statements while holding the unit-of-work
monitor lock causes lock contention when
`StatementExecutorType.DIRECT_EXECUTOR` is used (such as in Spanner JDBC
or PGAdapter), because direct executors execute synchronously on the
calling thread.
This change:
- Pre-registers a `SettableApiFuture` as
`currentlyRunningStatementFuture` under synchronization before
submitting to the executor, ensuring any immediate `cancel()` invocation
is captured without racing.
- Submits the statement task to `statementExecutor` outside the
synchronized block to avoid holding the unit-of-work lock during direct
synchronous execution.
- Attaches callbacks to forward results/exceptions to the
`SettableApiFuture`, and registers a listener on the `SettableApiFuture`
to propagate cancellation to the underlying future and clear
`currentlyRunningStatementFuture`.1 parent d086ba7 commit d9a8eef
1 file changed
Lines changed: 46 additions & 15 deletions
File tree
- java-spanner/google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
| 22 | + | |
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| |||
401 | 403 | | |
402 | 404 | | |
403 | 405 | | |
404 | | - | |
405 | 406 | | |
406 | 407 | | |
407 | 408 | | |
408 | 409 | | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
409 | 429 | | |
410 | 430 | | |
411 | 431 | | |
| |||
417 | 437 | | |
418 | 438 | | |
419 | 439 | | |
420 | | - | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
425 | 443 | | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
434 | 462 | | |
435 | 463 | | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
436 | 467 | | |
437 | 468 | | |
438 | | - | |
| 469 | + | |
439 | 470 | | |
440 | 471 | | |
441 | 472 | | |
| |||
0 commit comments