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

Enable Error Prone check: CheckedExceptionNotThrown #8227

Conversation

ksobolew
Copy link
Contributor

@ksobolew ksobolew commented Jun 8, 2021

@kokosing keeps wishing that this was on.

The issue with this is that:

  • This is an experimental checker
  • It has a huge number of false negatives

Regarding the second point, it may be that making it more accurate would make it unviably slow (the equivalent inspection in IntelliJ IDEA takes a lot of time, has to look at the whole code base - it's definitely not local - and has to be done repeatedly until nothing more is found). But perhaps it's still useful.

@@ -40,7 +40,6 @@ public LazyConnectionFactory(@ForLazyConnectionFactory ConnectionFactory delegat

@Override
public Connection openConnection(ConnectorSession session)
throws SQLException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping this helps update the code in the future, would i want to throw a checked exception (as super/interface allows me to)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you only call this method via the interface, you should be safe

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i mean a future self who will be changing the implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then your future self will add this exception back? It doesn't affect the interface, or its callers, only the override.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, except

  • except the method is not allowed to throw a checked exception (i need to consult parent for that, which i may not do), so i may e.g. wrap IOException in Runtime unnecessarily and reviewer may miss that either
  • someone can call the impl directly (not via interface), so such legit reintroduction of a checked exception may still be a breaking change for callers

Copy link
Member

@kokosing kokosing Jun 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we suppress it here, as other cases where throws is removed seem valid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppressed the error in this method

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this considered an errorprone bug and reported upstream?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm personally still on the fence on this. On the one hand I'd love it to lead to removal of dead code more (if none of the implementations throw this, we can remove the throws from the interface declaration!), but on the other I guess you're right that it's better to be conservative here.

I can create an issue upstream to see what they think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ksobolew ksobolew force-pushed the ksob/enable-error-prone-check-CheckedExceptionNotThrown branch from 1cde497 to 200412a Compare July 13, 2022 13:08
@github-actions github-actions bot added jdbc Relates to Trino JDBC driver tests:hive labels Jul 13, 2022
@ksobolew
Copy link
Contributor Author

Refreshing this old PR. It looks like this check is getting more precise (and thus useful), though it still misses lots of cases.

@ksobolew ksobolew force-pushed the ksob/enable-error-prone-check-CheckedExceptionNotThrown branch from 200412a to 3764d0f Compare July 13, 2022 13:15
@ksobolew
Copy link
Contributor Author

Weird:

2022-07-13T08:46:40.993-0500	INFO	pool-53-thread-1	stderr	FATAL: io.trino.testng.services.LogTestDurationListener: onBeforeClass: 
java.lang.IllegalStateException: There already is a start record for test: io.trino.execution.TestSqlTask
	at com.google.common.base.Preconditions.checkState(Preconditions.java:590)
	at io.trino.testng.services.LogTestDurationListener.beginTest(LogTestDurationListener.java:245)
	at io.trino.testng.services.LogTestDurationListener.onBeforeClass(LogTestDurationListener.java:179)
	at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:167)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:829)
2022-07-13T08:46:40.993-0500	INFO	pool-53-thread-1	stderr	JVM will be terminated

@ksobolew ksobolew force-pushed the ksob/enable-error-prone-check-CheckedExceptionNotThrown branch from 3764d0f to ee022be Compare July 14, 2022 13:42
@ksobolew
Copy link
Contributor Author

The Pinot failure is #13165.

For Redis:

Trying to resolve the latest version from remote
Error: socket hang up

Looks like a hiccup in the environment.

There are some code changes in the PR, but they should not affect any tests anyway.

@kokosing
Copy link
Member

Re started failed jobs. Please report flaky failures as github issues.

@ksobolew
Copy link
Contributor Author

The build is green, thanks. Not sure if there's anything to report if it was some instability in the build infra?

@@ -39,6 +39,7 @@ public LazyConnectionFactory(@ForLazyConnectionFactory ConnectionFactory delegat
}

@Override
@SuppressWarnings("CheckedExceptionNotThrown") // throws in the interface declaration; keep it to prevent false sense of security
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't understand the comment message

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW this seems not violate the check

public interface AnInterface
{
    void foo()
            throws Exception;
}

public class AnImplementationThatCurrentlyDoesNotThrow
        implements AnInterface
{
    @Override
    public void foo()
            throws Exception
    {}
}

so why do we need suppression here?
is it a bug in error-prone?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's difficult to tell why it's not reported. Maybe there's an exception for Exception (but there are some places where it reports Exception, so likely not). I don't know what logic it uses to find those cases, but I'm pretty sure it doesn't find everything.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out that this check does trigger in this case if the class is final, otherwise it does not. See this comment: google/error-prone#3425 (comment)

@@ -52,13 +52,11 @@
private final CassandraSession session;

public TestingScyllaServer()
throws Exception
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is test code. In tests is often more convenient to just declare throws Exception
and keep it, so that you won't need to add it ever again

Can we exclude the check on test resources?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's possible per-check. Maybe we could create a separate configuration in maven-compiler-plugin for tests?

@martint
Copy link
Member

martint commented Jul 19, 2022

Forcing the removal of a checked exception declaration from a method is a source-level backward incompatible change. Any callers that are calling such methods within a try-catch block will have to rewrite their code before they can compile. This is not good for the SPI, and also not good in keeping code changes localized.

For example, given:

class A
{
  public static void main(String... args)
  {
    try {
      B.run();
    }
    catch (java.io.IOException e) {
    }
}

class B
{
  public static void run()
    throws java.io.IOException
  {
    ...
  }
}

Forcing the removal of throws java.io.IOException from B will cause A to no longer compile.

@ksobolew
Copy link
Contributor Author

Forcing the removal of a checked exception declaration from a method is a source-level backward incompatible change. Any callers that are calling such methods within a try-catch block will have to rewrite their code before they can compile. This is not good for the SPI, and also not good in keeping code changes localized.

I agree that it is bad for interfaces and SPI, but other than that I would consider that an advantage (except for the bad change locality). If we know that some piece of code cannot throw a declared checked exception, this allows us to remove some potentially flaky error handling code, which was dead anyway. There is still an issue of unchecked exceptions, though.

@ksobolew ksobolew force-pushed the ksob/enable-error-prone-check-CheckedExceptionNotThrown branch from ee022be to 8e7c166 Compare July 20, 2022 10:39
The issue with this is that:

* This is an experimental checker
* It has a huge number of false negatives

Regarding the second point, it may be that making it more accurate would
make it unviably slow (the equivalent inspection in IntelliJ IDEA takes
a lot of time, has to look at the whole code base - it's definitely not
local - and has to be done repeatedly until nothing more is found). But
perhaps it's still useful.
@ksobolew ksobolew force-pushed the ksob/enable-error-prone-check-CheckedExceptionNotThrown branch from 8e7c166 to 9d1c7a6 Compare August 30, 2022 08:30
@colebow
Copy link
Member

colebow commented Oct 27, 2022

👋 @ksobolew - this PR has become inactive. If you're still interested in working on it, please let us know, and we can try to get reviewers to help with that.

We're working on closing out old and inactive PRs, so if you're too busy or this has too many merge conflicts to be worth picking back up, we'll be making another pass to close it out in a few weeks.

@ksobolew
Copy link
Contributor Author

Hi @colebow, I think this change still has merit, but it looks like the Error Prone checker is still not precise enough to satisfy the community :) I'm going to close it, at least for now.

@ksobolew ksobolew closed this Oct 27, 2022
@ksobolew ksobolew deleted the ksob/enable-error-prone-check-CheckedExceptionNotThrown branch October 27, 2022 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed jdbc Relates to Trino JDBC driver
Development

Successfully merging this pull request may close these issues.

6 participants