-
Notifications
You must be signed in to change notification settings - Fork 170
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
SNOW-872545: Thread.interrupt() does not work #1482
Comments
Similar issue: #1461 |
It works for me like this. In this program, after interrupting the thread, I throw an exception. import java.sql.*;
public class ThreadTest {
static volatile SnowflakeJDBCExample snowflakeJDBCExample = new SnowflakeJDBCExample();
public static void main(String[] args) throws SQLException, InterruptedException {
Thread t1 = new Thread(snowflakeJDBCExample, "thread1");
t1.start();
try {
t1.interrupt();
} catch(Exception e) {
System.out.println("Exception handled "+e);
}
}
}
class SnowflakeJDBCExample implements Runnable {
static String SNOWFLAKE_URL = "jdbc:snowflake://XXXX.snowflakecomputing.com/?&tracing=ALL";
static String USERNAME = "XXXX";
static String PASS = "XXXX";
@Override
public void run() {
try{
Connection connection = DriverManager.getConnection(SNOWFLAKE_URL, USERNAME, PASS);
System.out.println("Connection Established");
ResultSet resultSet;
System.out.println("Current running thread: " + Thread.currentThread().getName());
try {
Thread.sleep(10000);
resultSet = connection.getMetaData().getTables(null, "%", "%", new String[]{"TABLE", "VIEW", "ALIAS", "SYNONYM"});
System.out.println("Meta fetch completed");
} catch (InterruptedException e) {
throw new RuntimeException("Thread interrupted " + e);
}
} catch (Exception e) {
System.out.println("Exception occurred while performing IO operations: "+ e);
StackTraceElement[] stackTrace = e.getStackTrace();
System.out.println("---------------------------------");
if (stackTrace != null) {
for (StackTraceElement element : stackTrace) {
System.out.println(element.toString());
}
}
System.out.println("---------------------------------");
} finally {
System.out.println("Executing finally block");
ThreadTest.snowflakeJDBCExample = null;
}
}
} When I run it: $ java -cp .:snowflake-jdbc-3.14.2.jar ThreadTest.java
Connection Established
Current running thread: thread1
Exception occurred while performing IO operations: java.lang.RuntimeException: Thread interrupted java.lang.InterruptedException: sleep interrupted
---------------------------------
SnowflakeJDBCExample.run(ThreadTest.java:38)
java.base/java.lang.Thread.run(Thread.java:1589)
---------------------------------
Executing finally block Keep in mind that In your case, you interrupt the thread but you don't do anything with it, therefore no exception is thrown. You never get into this block cc: @sfc-gh-wfateem FYI for your issue 1461 |
@sfc-gh-spanaite - Your program throws an InterruptedException when your thread is sleeping, not while executing In my case, the thread does not interrupt even if I make a |
Hi @MohamedKamarudeen that is correct, but with most database drivers, instead of using Thread's interrupt() method, the preferred method would be to invoke cancel() on the Statement that's being executed. On the other hand, why do you need to use thread interrupt when running JDBC statements? |
Hi @sfc-gh-spanaite, Let's assume that It's important to note that we're not using a Statement object here; we're using |
If it's only for |
@sfc-gh-spanaite - I feel that
This exception does not occur in versions 3.13.29 and 3.13.33. As a result, the thread does not get interrupted, and it continues to run continuously without responding to the interrupt call. So, I'm considering this a bug. |
Give me a bit more time to have another look at the differences between 3.13.30 and 3.13.33 to see why an exception would be thrown in 3.13.32. I'll get back with more information. |
sorry for leaving this unanswered. Does the same issue occur with latest driver version 3.15.1 ? |
The issue here is that you're catching all Exceptions with a very generic catch clause and then you're not handling that exception, so you essentially have an infinite loop in your class implementing Runnable:
You should be checking if the thread is interrupted from within the thread itself. So for example, changing the while loop in the SnowflakeJDBCExample class would react to the interrupt call you're making from the main method:
Running the application code will result in the following:
I'm going to close off this issue, if you still have any questions or concerns then please feel free to open it again. |
@sfc-gh-wfateem - Thanks for taking look at this. You need to interrupt the thread when |
@MohamedKamarudeen I'm not sure I fully understand, so I'm going to walk you through my understanding of what you were trying to demonstrate as the problem based on the code snippet you provided. This is your problem statement:
The thread in question that you're trying to interrupt is implemented here:
In your
After you call
Your code then puts the I'm assuming the objective of that is to just ensure that you're calling the Once your main method wakes up again, you attempt to interrupt your Your expectation is that the
That goes on until 10 seconds, since you put the
That continues on forever, so based on your problem statement, you're saying that this should have stopped especially that the output of your If everything I'm saying here makes sense and I'm understanding your problem correctly, then the problem is mainly here in this block:
The JDK docs indicate that calling In your case, you never catch an exception when you run your code, so calling Unless you do something about the
The output we get from the new application code is this:
Again, this goes on for 10 seconds until the
If there's something I'm still missing here then can you help clarify that for me, please? What does the following expectation mean for you?
What does a proper interruption mean or look like for you? |
@sfc-gh-wfateem - My objective with this program is to interrupt the thread during the processing of the method call Reproduction Steps:
The issue is that Snowflake JDBC neither returns nor throws any exceptions when the thread interrupt call is invoked during the processing of I expect the thread interrupt call to either complete or throw an exception. As i mentioned in the comment , this issue(interrupt call waits indefinitely) does not happen in 3.13.30. |
@MohamedKamarudeen when you execute the following method in your environment on its own, how long does that take to return? |
What version of JDBC driver are you using?
3.13.29, 3.13.33
What operating system and processor architecture are you using?
MacOS Monterey and Intel Core i7
What version of Java are you using?
Java 8
What did you do?
Tried to interrupt the thread (using Thread.interrupt()) while performing JDBC calls, but the thread did not interrupt properly. However, thread.isInterrupted() returns true.
Expected the interrupt to happen properly, but it did not.
I tried the same scenario with JDBC version 3.13.30, and it throws a below exception. So, the execution is stopped
However, this above exception does not occur in versions 3.13.29 and 3.13.33. As a result, the thread does not get interrupted, and it continues to run continuously without responding to the interruption call.
The text was updated successfully, but these errors were encountered: