From dcb31bee698655f6c03557c9c147987e24d13f6e Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Wed, 18 Nov 2026 17:17:15 +0100 Subject: [PATCH 1/4] fix: Stop swallowing errors in three places the Throwable audit found QueueFile deleted its file when reading it ran out of memory, as if the file were corrupt. It now resets the file only for the IndexOutOfBoundsException that a corrupt file causes. Installation wrapped an OutOfMemoryError in a RuntimeException. It now wraps only the IOException from reading or writing the installation file. The Ktor client integration swallowed coroutine cancellation while reading the response body for a failed-request event. It now rethrows CancellationException and fatal errors. Refs #5513 Refs JAVA-552 Co-Authored-By: Claude Opus 5.5 --- .../main/java/io/sentry/android/core/Installation.java | 2 +- .../java/io/sentry/ktorClient/SentryKtorClientUtils.kt | 9 ++++++++- sentry/src/main/java/io/sentry/cache/tape/QueueFile.java | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/Installation.java b/sentry-android-core/src/main/java/io/sentry/android/core/Installation.java index fcd4406a5f8..34a5da4f57e 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/Installation.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/Installation.java @@ -43,7 +43,7 @@ public static String id(final @NotNull Context context) throws RuntimeException return deviceId; } deviceId = readInstallationFile(installation); - } catch (Throwable e) { + } catch (IOException e) { throw new RuntimeException(e); } } diff --git a/sentry-ktor-client/src/main/java/io/sentry/ktorClient/SentryKtorClientUtils.kt b/sentry-ktor-client/src/main/java/io/sentry/ktorClient/SentryKtorClientUtils.kt index 4ab9310d6b1..09f40e9234c 100644 --- a/sentry-ktor-client/src/main/java/io/sentry/ktorClient/SentryKtorClientUtils.kt +++ b/sentry-ktor-client/src/main/java/io/sentry/ktorClient/SentryKtorClientUtils.kt @@ -17,9 +17,11 @@ import io.sentry.exception.ExceptionMechanismException import io.sentry.exception.SentryHttpClientException import io.sentry.protocol.Mechanism import io.sentry.util.CookieUtils +import io.sentry.util.ExceptionUtils import io.sentry.util.HttpUtils import io.sentry.util.UrlUtils import java.util.concurrent.TimeUnit +import kotlin.coroutines.cancellation.CancellationException internal object SentryKtorClientUtils { internal suspend fun captureClientError( @@ -52,7 +54,12 @@ internal object SentryKtorClientUtils { statusCode = response.status.value try { bodySize = response.bodyAsBytes().size.toLong() - } catch (_: Throwable) {} + } catch (e: CancellationException) { + throw e + } catch (@Suppress("TooGenericExceptionCaught") e: Throwable) { + // The HTTP engine can throw anything while reading the body. The body size is optional. + ExceptionUtils.rethrowIfFatal(e) + } } event.request = sentryRequest diff --git a/sentry/src/main/java/io/sentry/cache/tape/QueueFile.java b/sentry/src/main/java/io/sentry/cache/tape/QueueFile.java index 137f1cd9156..c93d19ebf75 100644 --- a/sentry/src/main/java/io/sentry/cache/tape/QueueFile.java +++ b/sentry/src/main/java/io/sentry/cache/tape/QueueFile.java @@ -379,7 +379,7 @@ boolean ringRead(long position, byte[] buffer, int offset, int count) throws IOE resetFile(); } catch (IOException e) { throw e; - } catch (Throwable e) { + } catch (IndexOutOfBoundsException e) { // most likely the file is corrupt, so we delete it and recreate, accepting data loss resetFile(); } From ad5b0c32e2413d124c8a902a3867361c2637b0be Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Wed, 18 Nov 2026 17:32:37 +0100 Subject: [PATCH 2/4] changelog Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index be7899e2dd6..6c84e524bca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -96,6 +96,8 @@ ### Fixes - Set Apollo 3, 4, and 5 HTTP spans to `CANCELLED` instead of `INTERNAL_ERROR` when the request is cancelled ([#6284](https://github.com/getsentry/sentry-java/pull/6284)) +- The offline cache no longer deletes its queue file when it runs out of memory while reading it ([#6670](https://github.com/getsentry/sentry-java/pull/6670)) +- The Ktor client integration no longer swallows coroutine cancellation while reading the response body of a failed request ([#6670](https://github.com/getsentry/sentry-java/pull/6670)) - Preserve independent Netty read and write timeout events when exception singletons are reused. These exceptions now bypass event deduplication by default, so applications with multiple capture integrations may report the same timeout more than once. Use `SentryOptions.setIgnoredErrorsForDeduplication(...)` or `ignored-errors-for-deduplication` to customize or clear the default exclusions. ([#6465](https://github.com/getsentry/sentry-java/pull/6465)) - Prevent changes to reused tracing options from affecting spans and transactions after creation ([#6417](https://github.com/getsentry/sentry-java/pull/6417)) - Session Replay: Send Ktor client network request start and end times in milliseconds, so the requests line up with the replay ([#6619](https://github.com/getsentry/sentry-java/pull/6619)) From e828ee13821fb5008c276c6f41d6e964d3eba75c Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Wed, 18 Nov 2026 18:35:23 +0100 Subject: [PATCH 3/4] changelog: Move the entry to #6665 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c84e524bca..be7899e2dd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -96,8 +96,6 @@ ### Fixes - Set Apollo 3, 4, and 5 HTTP spans to `CANCELLED` instead of `INTERNAL_ERROR` when the request is cancelled ([#6284](https://github.com/getsentry/sentry-java/pull/6284)) -- The offline cache no longer deletes its queue file when it runs out of memory while reading it ([#6670](https://github.com/getsentry/sentry-java/pull/6670)) -- The Ktor client integration no longer swallows coroutine cancellation while reading the response body of a failed request ([#6670](https://github.com/getsentry/sentry-java/pull/6670)) - Preserve independent Netty read and write timeout events when exception singletons are reused. These exceptions now bypass event deduplication by default, so applications with multiple capture integrations may report the same timeout more than once. Use `SentryOptions.setIgnoredErrorsForDeduplication(...)` or `ignored-errors-for-deduplication` to customize or clear the default exclusions. ([#6465](https://github.com/getsentry/sentry-java/pull/6465)) - Prevent changes to reused tracing options from affecting spans and transactions after creation ([#6417](https://github.com/getsentry/sentry-java/pull/6417)) - Session Replay: Send Ktor client network request start and end times in milliseconds, so the requests line up with the replay ([#6619](https://github.com/getsentry/sentry-java/pull/6619)) From 68265f7b09022dd1947fb8de5d22fbe57a925c79 Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Thu, 19 Nov 2026 09:55:21 +0100 Subject: [PATCH 4/4] fix: Bound the installation id read and the Ktor failed-request body size Installation read the whole installation file into memory, so an oversized file could cause an OutOfMemoryError. It now reads at most 1 KB, enough for the UUID it holds, and treats a larger or unreadable file as corrupt and writes a new id. The Ktor client integration copied the whole response body to measure it for a failed-request event. It now takes the size from Content-Length, so it no longer reads the body at all, and can no longer swallow coroutine cancellation while doing so. Refs #5513 Refs JAVA-552 Co-Authored-By: Claude Opus 5.5 --- .../io/sentry/android/core/Installation.java | 20 +++++++++++++++++-- .../ktorClient/SentryKtorClientUtils.kt | 15 +++----------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/Installation.java b/sentry-android-core/src/main/java/io/sentry/android/core/Installation.java index 34a5da4f57e..63cd25ffcab 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/Installation.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/Installation.java @@ -21,6 +21,9 @@ final class Installation { private static final Charset UTF_8 = Charset.forName("UTF-8"); + // The id is a UUID, so a valid file is far below this + private static final int MAX_INSTALLATION_FILE_SIZE = 1024; + static final @NotNull AutoClosableReentrantLock staticLock = new AutoClosableReentrantLock(); private Installation() {} @@ -43,6 +46,9 @@ public static String id(final @NotNull Context context) throws RuntimeException return deviceId; } deviceId = readInstallationFile(installation); + if (deviceId == null) { + deviceId = writeInstallationFile(installation); + } } catch (IOException e) { throw new RuntimeException(e); } @@ -51,12 +57,22 @@ public static String id(final @NotNull Context context) throws RuntimeException } } + /** + * Returns null when the file is larger than an id can be, or can't be read, so the caller treats + * it as corrupt. The size check keeps an oversized file from being read into memory. + */ @TestOnly - static @NotNull String readInstallationFile(final @NotNull File installation) throws IOException { + static @Nullable String readInstallationFile(final @NotNull File installation) { try (final RandomAccessFile f = new RandomAccessFile(installation, "r")) { - final byte[] bytes = new byte[(int) f.length()]; + final long length = f.length(); + if (length > MAX_INSTALLATION_FILE_SIZE) { + return null; + } + final byte[] bytes = new byte[(int) length]; f.readFully(bytes); return new String(bytes, UTF_8); + } catch (IOException e) { + return null; } } diff --git a/sentry-ktor-client/src/main/java/io/sentry/ktorClient/SentryKtorClientUtils.kt b/sentry-ktor-client/src/main/java/io/sentry/ktorClient/SentryKtorClientUtils.kt index 09f40e9234c..55e5d2ca6bb 100644 --- a/sentry-ktor-client/src/main/java/io/sentry/ktorClient/SentryKtorClientUtils.kt +++ b/sentry-ktor-client/src/main/java/io/sentry/ktorClient/SentryKtorClientUtils.kt @@ -2,7 +2,6 @@ package io.sentry.ktorClient import io.ktor.client.request.HttpRequest import io.ktor.client.statement.HttpResponse -import io.ktor.client.statement.bodyAsBytes import io.ktor.http.Headers import io.ktor.http.contentLength import io.ktor.util.toMap @@ -17,14 +16,12 @@ import io.sentry.exception.ExceptionMechanismException import io.sentry.exception.SentryHttpClientException import io.sentry.protocol.Mechanism import io.sentry.util.CookieUtils -import io.sentry.util.ExceptionUtils import io.sentry.util.HttpUtils import io.sentry.util.UrlUtils import java.util.concurrent.TimeUnit -import kotlin.coroutines.cancellation.CancellationException internal object SentryKtorClientUtils { - internal suspend fun captureClientError( + internal fun captureClientError( scopes: IScopes, request: HttpRequest, response: HttpResponse, @@ -52,14 +49,8 @@ internal object SentryKtorClientUtils { cookies = CookieUtils.filterSetCookie(response.headers["Set-Cookie"], scopes.options) headers = getResponseHeaders(scopes, response.headers) statusCode = response.status.value - try { - bodySize = response.bodyAsBytes().size.toLong() - } catch (e: CancellationException) { - throw e - } catch (@Suppress("TooGenericExceptionCaught") e: Throwable) { - // The HTTP engine can throw anything while reading the body. The body size is optional. - ExceptionUtils.rethrowIfFatal(e) - } + // Content-Length rather than reading the body, which would copy all of it into memory + bodySize = response.contentLength() } event.request = sentryRequest