Repository navigation
Close all streams in the Keycloak production code - #53852
Conversation
c90164a to
4361986
Compare
|
@ahus1 As Tomas writes, please, don't introduce 3rd party annotations into the code, also is it possible to remove SuppressWorning, related to checker only? This would also remove additional checker pom dependencies. |
|
@ahus1 did you consider codeql, which is already part of the CI? |
|
@ahus1 Besides that, static analysis makes sense, but only if managed by external config, not touching the codebase itself. So, partially agree with your proposal, thanks for this PR. 👍 |
| <groupId>org.checkerframework</groupId> | ||
| <artifactId>checker-qual</artifactId> | ||
| <version>${checkerframework.version}</version> | ||
| <scope>provided</scope> |
There was a problem hiding this comment.
This is incorrect in all pom.xml files.
provided means the dependency will be available at runtime, provided by a container or the JDK. That is not the case.
As the annotation is present at runtime, I would add the dependency to the distribution. I'm aware that the JVM loads the annotation class only if reflection is used, but I think it's better to be safe than to get a NoClassDefFoundError.
In any case, <scope>provided</scope> is not right. You probably want to make it optional.
There was a problem hiding this comment.
Ignore me, others already commented the same thing 😅
The
The tool should be self-contained. So far CodeQL didn't warn us about those problems, so we would need to write our own rules. Is that what you are suggeting?
In an ideal world yes. Unless you show me a tool that can do this, this is IMHO as good as it gets. |
|
I asked Copilot: Could this be done with CodeQL instead of the Checker Framework? And this is the answer: Partly, but not with the same strength. What CodeQL offers
What CodeQL does not do
Overrides and false positives with CodeQL
Recommendation
I (Copilot) have not run the CodeQL queries against this branch, so this is based on how the queries are designed rather than measured results. |
e58566c to
3759780
Compare
|
Picking up an idea from @ruchikajha95 to only focus on production code, and not on Test classes in this first PR. @pruivo shared a review with Claude, and I've incorporated those changes. There is in Please give it another view, and let me know if we should continue with this tool to check for closed streams (and have additional follow-up PRs to cover more production code and also tests), or rather look for a different tool. |
Thanks for the updates, @ahus1! Really appreciate you incorporating the feedback and adding the docs in building.md that should be very helpful . I personally feel this is a great addition. I personally feel We should move forward with this approach. |
| protected static final Logger log = Logger.getLogger(PushDeliveryService.class); | ||
|
|
||
| private final HttpClient httpClient; | ||
| private final CloseableHttpClient httpClient; |
There was a problem hiding this comment.
Shouldn't this client also be closed at some point?
There was a problem hiding this comment.
This client is retrieved from session.getProvider(HttpClientProvider.class).getHttpClient();, and it therefore must not be closed here. It is a shared client, and it will eventually be closed by the provider.
As the JavaDoc states:
Closing the HttpClient instance is responsibility of this provider. However, the objects created via the returned HttpClient need to be closed properly by the code that instantiated them.
As a follow-up, the interface will return a HttpClient, not a CloseableHttpClient. But that's a breaking change for the API, though it should have been like this all the time.
I slightly tune the annotations that this can stay a HttpClient, see my lastest push.
IMHO this resolved this.
There was a problem hiding this comment.
🔵 Needs a closer look
The compiler-profile changes affect reactor-wide annotation processing and module-specific processors, but the required dependency-backed clean JDK 21 reactor build is unavailable in this review environment.
No open issues 🎉
Review effort: Balanced
3dfdcd6 to
54bdbab
Compare
| ### Fixing findings | ||
|
|
||
| * Prefer try-with-resources for resources that are closed in the same method. | ||
| * Use `@Owning` on a parameter or field when the callee or the containing object becomes responsible for closing the resource. |
There was a problem hiding this comment.
This explanation doesn't make any sense to me; callee would mean the caller is responsible for closing the resource, while containing object means the called object is responsible.
Might be better to explain @Owning and @NotOwning` together, with the difference between it being used on an field or parameter (in this case @owning means the called object owns it), while on a method return it means the caller owns it.
There was a problem hiding this comment.
@stianst - I've restructured the guide, let me know if this helps. I also updated to the latest version of the Checker framework to remove some of the suppressions.
|
|
||
| * Prefer try-with-resources for resources that are closed in the same method. | ||
| * Use `@Owning` on a parameter or field when the callee or the containing object becomes responsible for closing the resource. | ||
| See [`SafeInputStream`](../server-spi-private/src/main/java/org/keycloak/connections/httpclient/SafeInputStream.java). |
There was a problem hiding this comment.
I would remove the See links. For one it is confusing as See suggests it's more documentation/explanation, not an example. Link to the checker framework docs instead perhaps? Or, provide some simple examples in the readme?
| See [`SafeInputStream`](../server-spi-private/src/main/java/org/keycloak/connections/httpclient/SafeInputStream.java). | ||
| * Use `@NotOwning` on a method that returns a resource managed by someone else. | ||
| See [`HttpClientProvider`](../server-spi-private/src/main/java/org/keycloak/connections/httpclient/HttpClientProvider.java). | ||
| * Use `@MustCallAlias` for wrappers, where closing the wrapper closes the wrapped resource, and `@EnsuresCalledMethods` on the `close()` method that closes owned fields. |
There was a problem hiding this comment.
Same as above really, this explanation doesn't help me, just confuses me.
| * Use `@NotOwning` on a method that returns a resource managed by someone else. | ||
| See [`HttpClientProvider`](../server-spi-private/src/main/java/org/keycloak/connections/httpclient/HttpClientProvider.java). | ||
| * Use `@MustCallAlias` for wrappers, where closing the wrapper closes the wrapped resource, and `@EnsuresCalledMethods` on the `close()` method that closes owned fields. | ||
| * Contracts for third-party APIs are declared in `.mvn/checkerframework/jdk.astub`. |
There was a problem hiding this comment.
What about @MustCall that is an important one and should be explained?
Closes keycloak#53845 Signed-off-by: Alexander Schwartz <alexander.schwartz@ibm.com>
Signed-off-by: Alexander Schwartz <alexander.schwartz@ibm.com>
54bdbab to
028b7ad
Compare
Closes #53845