diff --git a/firebase-installations/CHANGELOG.md b/firebase-installations/CHANGELOG.md index 4ac6a598a53..cd52c408330 100644 --- a/firebase-installations/CHANGELOG.md +++ b/firebase-installations/CHANGELOG.md @@ -1,5 +1,8 @@ # Unreleased +- [fixed] Closed the response stream when deleting an installation to avoid a StrictMode + `LeakedClosableViolation`. + # 19.1.2 - [changed] Migrated from SharedPreferences to DataStore (#8355) diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/remote/FirebaseInstallationServiceClient.java b/firebase-installations/src/main/java/com/google/firebase/installations/remote/FirebaseInstallationServiceClient.java index a35b0ab64fa..9aaa5775420 100644 --- a/firebase-installations/src/main/java/com/google/firebase/installations/remote/FirebaseInstallationServiceClient.java +++ b/firebase-installations/src/main/java/com/google/firebase/installations/remote/FirebaseInstallationServiceClient.java @@ -326,6 +326,7 @@ public void deleteFirebaseInstallation( int httpResponseCode = httpURLConnection.getResponseCode(); if (httpResponseCode == 200 || httpResponseCode == 401 || httpResponseCode == 404) { + closeResponseStream(httpURLConnection, httpResponseCode); return; } @@ -453,6 +454,23 @@ private static boolean isSuccessfulResponseCode(int responseCode) { return responseCode >= 200 && responseCode < 300; } + // Close the response stream so decoders backing it (for example, the gzip Inflater on Android's + // HttpURLConnection) are released eagerly. disconnect() alone does not reliably release them, + // which triggers StrictMode LeakedClosableViolation when the finalizer runs. + private static void closeResponseStream(HttpURLConnection conn, int httpResponseCode) { + try { + InputStream stream = + isSuccessfulResponseCode(httpResponseCode) + ? conn.getInputStream() + : conn.getErrorStream(); + if (stream != null) { + stream.close(); + } + } catch (IOException ignored) { + + } + } + private static void logBadConfigError() { Log.e( FIS_TAG,