diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/internal/util/ScreenshotUtils.java b/sentry-android-core/src/main/java/io/sentry/android/core/internal/util/ScreenshotUtils.java index 1b17f28d5e..28d032acfd 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/internal/util/ScreenshotUtils.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/internal/util/ScreenshotUtils.java @@ -15,6 +15,7 @@ import io.sentry.SentryLevel; import io.sentry.android.core.BuildInfoProvider; import io.sentry.android.core.SentryAndroidOptions; +import io.sentry.util.ExceptionUtils; import io.sentry.util.thread.IThreadChecker; import java.io.ByteArrayOutputStream; import java.util.concurrent.CountDownLatch; @@ -155,8 +156,15 @@ public final class ScreenshotUtils { } try { - final Bitmap bitmap = - Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.RGB_565); + final Bitmap bitmap; + try { + bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.RGB_565); + } catch (OutOfMemoryError e) { + // A bitmap of the whole window is one large allocation, which can fail while the rest of + // the heap is healthy. The screenshot is skipped instead of crashing the app. + logger.log(SentryLevel.WARNING, "Not enough memory for the screenshot bitmap.", e); + return null; + } final @NotNull CountDownLatch latch = new CountDownLatch(1); @@ -183,7 +191,9 @@ public final class ScreenshotUtils { final boolean completed = latch.await(CAPTURE_TIMEOUT_MS, TimeUnit.MILLISECONDS); success = completed && copyResultCode.get() == PixelCopy.SUCCESS; } catch (Throwable e) { - // ignored + // PixelCopy reads the window's surface through the system compositor, so we can't know + // what it throws. No screenshot is attached. + ExceptionUtils.rethrowIfFatal(e); logger.log(SentryLevel.ERROR, "Taking screenshot using PixelCopy failed.", e); } finally { thread.quit(); @@ -206,6 +216,9 @@ public final class ScreenshotUtils { try { view.draw(canvas); } catch (Throwable e) { + // draw() runs the app's own views, so we can't know what it throws. No screenshot is + // attached. + ExceptionUtils.rethrowIfFatal(e); logger.log(SentryLevel.ERROR, "Taking screenshot failed (view.draw).", e); } finally { latch.countDown(); @@ -219,6 +232,9 @@ public final class ScreenshotUtils { } return bitmap; } catch (Throwable e) { + // On the main thread, draw() runs the app's own views, so we can't know what it throws. No + // screenshot is attached. + ExceptionUtils.rethrowIfFatal(e); logger.log(SentryLevel.ERROR, "Taking screenshot failed.", e); } return null; diff --git a/sentry-android-replay/src/main/java/io/sentry/android/replay/ReplayIntegration.kt b/sentry-android-replay/src/main/java/io/sentry/android/replay/ReplayIntegration.kt index 6bdca6f748..c2b4537505 100644 --- a/sentry-android-replay/src/main/java/io/sentry/android/replay/ReplayIntegration.kt +++ b/sentry-android-replay/src/main/java/io/sentry/android/replay/ReplayIntegration.kt @@ -21,6 +21,7 @@ import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryLevel.DEBUG import io.sentry.SentryLevel.ERROR import io.sentry.SentryLevel.INFO +import io.sentry.SentryLevel.WARNING import io.sentry.SentryOptions import io.sentry.SentryReplayEvent import io.sentry.TypeCheckHint @@ -480,7 +481,15 @@ public class ReplayIntegration( state.get().captureStrategy?.onScreenshotRecorded(bitmap) { frameTimeStamp -> val observer = options.sessionReplay.frameObserver if (observer != null) { - val copy = bitmap.copy(bitmap.config!!, false) + val copy = + try { + bitmap.copy(bitmap.config!!, false) + } catch (e: OutOfMemoryError) { + // A copy of the whole frame is one large allocation, which can fail while the rest of + // the heap is healthy. The observer doesn't get this frame, but it is still recorded. + options.logger.log(WARNING, "Not enough memory to copy the frame for the observer", e) + null + } if (copy != null) { try { val hint = Hint() @@ -503,7 +512,16 @@ public class ReplayIntegration( state.get().captureStrategy?.onScreenshotRecorded { _ -> val observer = options.sessionReplay.frameObserver if (observer != null) { - val bitmap = BitmapFactory.decodeFile(screenshot.absolutePath) + val bitmap = + try { + BitmapFactory.decodeFile(screenshot.absolutePath) + } catch (e: OutOfMemoryError) { + // A bitmap of the whole frame is one large allocation, which can fail while the rest + // of the heap is healthy. The observer doesn't get this frame, but it is still + // recorded. + options.logger.log(WARNING, "Not enough memory to decode the frame for the observer", e) + null + } if (bitmap != null) { try { val hint = Hint() diff --git a/sentry-android-replay/src/main/java/io/sentry/android/replay/ScreenshotRecorder.kt b/sentry-android-replay/src/main/java/io/sentry/android/replay/ScreenshotRecorder.kt index ce987c24ce..bbbdf3e146 100644 --- a/sentry-android-replay/src/main/java/io/sentry/android/replay/ScreenshotRecorder.kt +++ b/sentry-android-replay/src/main/java/io/sentry/android/replay/ScreenshotRecorder.kt @@ -17,6 +17,7 @@ import io.sentry.android.replay.screenshot.ScreenshotStrategy import io.sentry.android.replay.util.DebugOverlayDrawable import io.sentry.android.replay.util.addOnDrawListenerSafe import io.sentry.android.replay.util.removeOnDrawListenerSafe +import io.sentry.util.ExceptionUtils import java.io.File import java.lang.ref.WeakReference import java.util.concurrent.atomic.AtomicBoolean @@ -92,6 +93,9 @@ internal class ScreenshotRecorder( contentChanged.set(false) screenshotStrategy.capture(root) } catch (e: Throwable) { + // Capturing draws the app's own views and goes through the device's graphics stack, so we + // can't know what it throws. The frame is skipped. + ExceptionUtils.rethrowIfFatal(e) options.logger.log(WARNING, "Failed to capture replay recording", e) } } diff --git a/sentry-android-replay/src/main/java/io/sentry/android/replay/WindowRecorder.kt b/sentry-android-replay/src/main/java/io/sentry/android/replay/WindowRecorder.kt index 8ee0041f8b..fc052b4e73 100644 --- a/sentry-android-replay/src/main/java/io/sentry/android/replay/WindowRecorder.kt +++ b/sentry-android-replay/src/main/java/io/sentry/android/replay/WindowRecorder.kt @@ -16,6 +16,7 @@ import io.sentry.android.replay.util.addOnPreDrawListenerSafe import io.sentry.android.replay.util.hasSize import io.sentry.android.replay.util.removeOnPreDrawListenerSafe import io.sentry.util.AutoClosableReentrantLock +import io.sentry.util.ExceptionUtils import java.lang.ref.WeakReference import java.util.WeakHashMap import java.util.concurrent.ScheduledExecutorService @@ -96,6 +97,9 @@ internal class WindowRecorder( } recorder?.capture() } catch (e: Throwable) { + // Capturing draws the app's own views and goes through the device's graphics stack, so we + // can't know what it throws. This frame is skipped, and the next one is still scheduled. + ExceptionUtils.rethrowIfFatal(e) options.logger.log(ERROR, "Failed to capture a frame", e) } @@ -229,7 +233,16 @@ internal class WindowRecorder( } capturer?.config = config - capturer?.recorder = ScreenshotRecorder(config, options, this, screenshotRecorderCallback) + capturer?.recorder = + try { + ScreenshotRecorder(config, options, this, screenshotRecorderCallback) + } catch (e: OutOfMemoryError) { + // The PixelCopy strategy allocates a bitmap of the whole recording, one large allocation + // that can fail while the rest of the heap is healthy. No frames are captured until the + // next configuration change. + options.logger.log(ERROR, "Not enough memory to record replay frames", e) + null + } val newRoot = rootViews.lastOrNull()?.get() if (newRoot != null) { diff --git a/sentry-android-replay/src/main/java/io/sentry/android/replay/screenshot/CanvasStrategy.kt b/sentry-android-replay/src/main/java/io/sentry/android/replay/screenshot/CanvasStrategy.kt index dfb8ec0f26..186ab77b26 100644 --- a/sentry-android-replay/src/main/java/io/sentry/android/replay/screenshot/CanvasStrategy.kt +++ b/sentry-android-replay/src/main/java/io/sentry/android/replay/screenshot/CanvasStrategy.kt @@ -36,6 +36,7 @@ import io.sentry.android.replay.ScreenshotRecorderCallback import io.sentry.android.replay.ScreenshotRecorderConfig import io.sentry.android.replay.util.ReplayRunnable import io.sentry.util.AutoClosableReentrantLock +import io.sentry.util.ExceptionUtils import io.sentry.util.IntegrationUtils import java.util.WeakHashMap import java.util.concurrent.atomic.AtomicBoolean @@ -96,14 +97,25 @@ internal class CanvasStrategy( screenshotLock.acquire().use { if (screenshot == null) { screenshot = - Bitmap.createBitmap( - config.recordingWidth, - config.recordingHeight, - Bitmap.Config.RGB_565, - ) + try { + Bitmap.createBitmap( + config.recordingWidth, + config.recordingHeight, + Bitmap.Config.RGB_565, + ) + } catch (e: OutOfMemoryError) { + // A bitmap of the whole recording is one large allocation, which can fail while the + // rest of the heap is healthy. This frame is skipped, and the next one tries again. + options.logger.log(SentryLevel.WARNING, "Not enough memory for the replay frame", e) + null + } } } } + if (screenshot == null) { + lastCaptureSuccessful.set(false) + return@Runnable + } if (isClosed.get()) { options.logger.log( @@ -144,6 +156,9 @@ internal class CanvasStrategy( executor.getBackgroundHandler(), ) } catch (t: Throwable) { + // Drawing into the hardware surface and PixelCopy go through the device's graphics stack, + // so we can't know what they throw. The frame is skipped. + ExceptionUtils.rethrowIfFatal(t) // if the request itself threw, no callback will clear the flag pixelCopyInFlight = false options.logger.log(SentryLevel.ERROR, "Canvas Strategy: picture render failed", t) diff --git a/sentry-android-replay/src/main/java/io/sentry/android/replay/screenshot/PixelCopyStrategy.kt b/sentry-android-replay/src/main/java/io/sentry/android/replay/screenshot/PixelCopyStrategy.kt index 72c355a5a7..004ee78592 100644 --- a/sentry-android-replay/src/main/java/io/sentry/android/replay/screenshot/PixelCopyStrategy.kt +++ b/sentry-android-replay/src/main/java/io/sentry/android/replay/screenshot/PixelCopyStrategy.kt @@ -24,6 +24,7 @@ import io.sentry.android.replay.util.MaskRenderer import io.sentry.android.replay.util.ReplayRunnable import io.sentry.android.replay.util.traverse import io.sentry.android.replay.viewhierarchy.ViewHierarchyNode +import io.sentry.util.ExceptionUtils import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicInteger import kotlin.LazyThreadSafetyMode.NONE @@ -177,6 +178,9 @@ internal class PixelCopyStrategy( mainLooperHandler.handler, ) } catch (e: Throwable) { + // PixelCopy goes through the device's graphics stack, so we can't know what it throws. The + // frame is skipped, and the frame gate is released for the next one. + ExceptionUtils.rethrowIfFatal(e) options.logger.log(WARNING, "Failed to capture replay recording", e) unstableCaptures.set(0) lastCaptureSuccessful.set(false) @@ -261,9 +265,17 @@ internal class PixelCopyStrategy( var svBitmap: Bitmap? = null try { - svBitmap = - Bitmap.createBitmap(surfaceView.width, surfaceView.height, Bitmap.Config.ARGB_8888) - val bitmapToCapture = svBitmap + val bitmapToCapture: Bitmap = + try { + Bitmap.createBitmap(surfaceView.width, surfaceView.height, Bitmap.Config.ARGB_8888) + } catch (e: OutOfMemoryError) { + // A bitmap of the whole SurfaceView is one large allocation, which can fail while the + // rest of the heap is healthy. This SurfaceView is left out of the frame. + options.logger.log(WARNING, "Not enough memory to capture the SurfaceView", e) + onCaptureComplete() + continue + } + svBitmap = bitmapToCapture surfaceView.getLocationOnScreen(svLocation) val capturedX = svLocation[0] @@ -294,6 +306,9 @@ internal class PixelCopyStrategy( // double-recycle if the recycle paths above already ran. svBitmap = null } catch (e: Throwable) { + // PixelCopy goes through the device's graphics stack, so we can't know what it throws. + // This SurfaceView is left out of the frame. + ExceptionUtils.rethrowIfFatal(e) options.logger.log(WARNING, "Failed to capture SurfaceView", e) svBitmap?.recycle() onCaptureComplete()