jackson-core
jackson-core copied to clipboard
Improve handling of URL-backed `InputStream`s, closing
(note: replaces https://github.com/FasterXML/jackson-databind/issues/3520)
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 https://github.com/FasterXML/jackson-databind/issues/3455 for details.
It seems like we might be able to handle some of those cases if we could do 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!)
But due to separation of concerns, this cannot (or should not) be done in databind. Instead we could wrap URL-backed InputStream
in these cases, delegated all calls except for close()
: and in close()
we would do disconnect.