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

Add special handling for URL-based variants of readValue() (ObjectMapper, ObjectReader) #3520

Closed
cowtowncoder opened this issue Jun 15, 2022 · 2 comments
Labels
good first issue Issue that seems easy to resolve and is likely a good candidate for contributors new to project

Comments

@cowtowncoder
Copy link
Member

(note: offshoot of #3455 -- read for background)

It looks like JDK's HttpURLConnection has unfortunate handling of some error cases, leading to leakage of not-fully-closed HTTP connections under some error conditions. See #3455 for details.

It seems like we might be able to handle some of those cases, specially readValue() variants of ObjectMapper / ObjectReader by using something like:

URLConnection connection = new URL(url).openConnection();
try (InputStream stream = connection.getInputStream()) {
  // parse JSON
} finally {
  // java.net.HttpURLConnection.HttpURLConnection
  if (connection instanceof HttpURLConnection) {
    ((HttpURLConnection) connection).disconnect();
  }
}

(as suggested by @fxha -- thanks!)

@cowtowncoder cowtowncoder added to-evaluate Issue that has been received but not yet evaluated good first issue Issue that seems easy to resolve and is likely a good candidate for contributors new to project 2.14 and removed to-evaluate Issue that has been received but not yet evaluated labels Jun 15, 2022
@cowtowncoder
Copy link
Member Author

Hmmh. Ok, this is not quite as easy to do as I thought. The problem being that the actual opening is within TokenStreamFactory (like JsonFactory) but that simply returns JsonParser instance.
Conversely ObjectMapper.readValue() etc do not have access to the connection being created etc.

But I think there is a way this could be achieved to some degree: if we implemented delegating InputStream that wraps underlying URL stream but overrides close() to use disconnect (in case where we have HttpURLConnection.
I will file an issue at jackson-core for this.

@cowtowncoder
Copy link
Member Author

Replaced by:

FasterXML/jackson-core#803

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Issue that seems easy to resolve and is likely a good candidate for contributors new to project
Projects
None yet
Development

No branches or pull requests

1 participant