Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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()
Expand Down
Loading