Disconnect HttpURLConnection in AbstractFileResolvingResource#36748
Open
SebTardif wants to merge 1 commit intospring-projects:mainfrom
Open
Disconnect HttpURLConnection in AbstractFileResolvingResource#36748SebTardif wants to merge 1 commit intospring-projects:mainfrom
SebTardif wants to merge 1 commit intospring-projects:mainfrom
Conversation
The exists(), checkReadable(), contentLength(), and lastModified() methods open URLConnection instances that are never disconnected on most code paths. On the 405 (Method Not Allowed) retry paths, the original connection reference is silently overwritten when a new connection is opened, leaking the original. Wrap HTTP connection usage in try-finally blocks to ensure disconnect() is always called, and explicitly disconnect the original connection before opening a retry connection. Signed-off-by: Sebastien Tardif <SebTardif@ncf.ca>
996a33b to
f5522ae
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Ensure
HttpURLConnectioninstances are always disconnected inAbstractFileResolvingResource'sexists(),checkReadable(),contentLength(), andlastModified()methods.Problem
These four methods open
URLConnectioninstances viaurl.openConnection()that are never disconnected on most code paths. This leaks underlying sockets when resources are accessed over HTTP.Additionally, on the 405 (Method Not Allowed) retry paths, the original connection reference is silently overwritten when a new connection is opened — the original connection is leaked since no reference to it remains.
This class is the base for
ClassPathResource,UrlResource, and all classpath/URL resources in Spring, so any code checking resource existence, readability, content length, or last-modified over HTTP is affected.Fix
disconnect()is always called regardless of which code path is takenhttpCon = nullwhen a retry yields a non-HTTP connection to avoid disconnecting a stale referenceNo behavioral changes — only resource cleanup is added.