Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clear interrupted flag after cancel #1880

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.spanner.jdbc;

import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.Options;
import com.google.cloud.spanner.Options.QueryOption;
import com.google.cloud.spanner.ReadContext.QueryAnalyzeMode;
Expand All @@ -34,6 +35,7 @@
import java.time.Duration;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;
Expand All @@ -47,6 +49,7 @@ abstract class AbstractJdbcStatement extends AbstractJdbcWrapper implements Stat
final AbstractStatementParser parser;
private final Lock executingLock;
private volatile Thread executingThread;
private final AtomicBoolean cancelled = new AtomicBoolean();
private boolean closed;
private boolean closeOnCompletion;
private boolean poolable;
Expand Down Expand Up @@ -259,10 +262,18 @@ private <T> T doWithStatementTimeout(
connection.recordClientLibLatencyMetric(executionDuration.toMillis());
return result;
} catch (SpannerException spannerException) {
if (this.cancelled.get()
&& spannerException.getErrorCode() == ErrorCode.CANCELLED
&& this.executingLock != null) {
// Clear the interrupted flag of the thread.
//noinspection ResultOfMethodCallIgnored
Thread.interrupted();
}
throw JdbcSqlExceptionFactory.of(spannerException);
} finally {
if (this.executingLock != null) {
this.executingThread = null;
this.cancelled.set(false);
this.executingLock.unlock();
}
if (shouldResetTimeout.apply(result)) {
Expand Down Expand Up @@ -374,8 +385,14 @@ public void cancel() throws SQLException {
// This is a best-effort operation. It could be that the executing thread is set to null
// between the if-check and the actual execution. Just ignore if that happens.
try {
this.cancelled.set(true);
this.executingThread.interrupt();
} catch (NullPointerException ignore) {
// ignore, this just means that the execution finished before we got to the point where we
// could interrupt the thread.
} catch (SecurityException securityException) {
throw JdbcSqlExceptionFactory.of(
securityException.getMessage(), Code.PERMISSION_DENIED, securityException);
}
} else {
connection.getSpannerConnection().cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ public void testCancel() throws Exception {
message instanceof ExecuteSqlRequest
&& ((ExecuteSqlRequest) message).getSql().equals(sql),
5000L);
System.out.println("Cancelling statement");
statement.cancel();
return null;
});
Expand Down
Loading