Skip to content

Commit

Permalink
Guard Executor/Scheduler unwrapping against implementations returning…
Browse files Browse the repository at this point in the history
… themselves.

If unwrap isn't able to unwrap then it would return itself causing a cast exception.

[resolves #220]

Signed-off-by: Mark Paluch <[email protected]>
  • Loading branch information
mp911de committed Nov 28, 2024
1 parent bf6540e commit 55f8f30
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 151 deletions.
35 changes: 22 additions & 13 deletions src/main/java/io/r2dbc/pool/ConnectionPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,10 @@ public ConnectionPool(ConnectionPoolConfiguration configuration) {

Connection connection = ref.poolable();
Scheduler scheduler = null;
Executor executor = null;
Mono<Connection> conn;

if (connection instanceof Wrapped<?>) {

Wrapped<?> wrapped = (Wrapped<?>) connection;

scheduler = wrapped.unwrap(Scheduler.class);

if (scheduler == null) {
executor = wrapped.unwrap(Executor.class);
}

if (executor != null) {
scheduler = Schedulers.fromExecutor(executor);
}
scheduler = findScheduler((Wrapped<?>) connection);
}

if (scheduler != null) {
Expand Down Expand Up @@ -167,6 +155,27 @@ public ConnectionPool(ConnectionPoolConfiguration configuration) {
this.create = configuration.getAcquireRetry() > 0 ? create.retry(configuration.getAcquireRetry()) : create;
}

@Nullable
@SuppressWarnings("DataFlowIssue")
private static Scheduler findScheduler(Wrapped<?> connection) {

Object unwrapped = connection.unwrap(Scheduler.class);

if (!(unwrapped instanceof Scheduler)) {
unwrapped = connection.unwrap(Executor.class);
}

if (unwrapped instanceof Executor) {
unwrapped = Schedulers.fromExecutor((Executor) unwrapped);
}

if (unwrapped instanceof Scheduler) {
return (Scheduler) unwrapped;
}

return null;
}

private Mono<Connection> prepareConnection(ConnectionPoolConfiguration configuration, PooledRef<Connection> ref, Connection connection, Function<Connection, Mono<Void>> allocateValidation) {

Mono<Void> prepare = null;
Expand Down
Loading

0 comments on commit 55f8f30

Please sign in to comment.