A crash after Send troubleshooting report is uploaded with Never

Open
#23,796 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
android, kotlin
Domain
backend, mobile

Research direction

The issue identifies CrashReportService.sendReport and ACRA's crash handling as the relevant flow; start by reading those implementations and the regression test in AnkiDroid/src/test/java/com/ichi2/anki/analytics/ManualCrashReportCrashTest.kt. Check how the Never preference affects reporting after a manual report is sent, then verify that a later crash is not uploaded without approval.

Written by the indexing model from the issue text.

Description

code-scan

Assisted-by: Claude Fable 5.2 max

[EXISTING] A crash after Send troubleshooting report is uploaded with Never
Subject: [EXISTING] A crash after Send troubleshooting report is uploaded with Never

With reporting set to Never, sending a troubleshooting report from Help leaves ACRA enabled, so a later crash in that session is uploaded without asking.

⚠️ This issue existed before the PR and should likely be split off into a separate issue. The PR extends it to non-default profiles, whose approved reports the sender could not reach before.

---
diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/analytics/ManualCrashReportCrashTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/analytics/ManualCrashReportCrashTest.kt
new file mode 100644
index 0000000000..b3cda3b6a7
--- /dev/null
+++ b/AnkiDroid/src/test/java/com/ichi2/anki/analytics/ManualCrashReportCrashTest.kt
@@ -0,0 +1,102 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package com.ichi2.anki.analytics
+
+import android.app.Application
+import android.os.Bundle
+import androidx.core.content.edit
+import androidx.core.content.pm.PackageInfoCompat
+import androidx.test.core.app.ApplicationProvider
+import com.ichi2.anki.AnkiActivity
+import com.ichi2.anki.acraCoreConfigBuilder
+import com.ichi2.anki.common.analytics.Analytics
+import com.ichi2.anki.common.crashreporting.CrashReportService
+import com.ichi2.anki.common.crashreporting.CrashReporter.Companion.FEEDBACK_REPORT_KEY
+import com.ichi2.anki.common.crashreporting.CrashReporter.Companion.FEEDBACK_REPORT_NEVER
+import com.ichi2.anki.common.preferences.sharedPrefs
+import com.ichi2.anki.initializeAcraCrashReporter
+import com.ichi2.testutils.EmptyApplication
+import com.ichi2.testutils.common.OS
+import io.mockk.every
+import io.mockk.mockkConstructor
+import io.mockk.mockkObject
+import io.mockk.unmockkAll
+import io.mockk.verify
+import org.acra.ACRA
+import org.acra.ACRAConstants
+import org.acra.ReportField
+import org.acra.builder.ReportBuilder
+import org.acra.data.CrashReportData
+import org.acra.data.CrashReportDataFactory
+import org.acra.interaction.ToastInteraction
+import org.acra.scheduler.DefaultSenderScheduler
+import org.acra.sender.HttpSender
+import org.acra.sender.SendingConductor
+import org.junit.After
+import org.junit.Assume.assumeFalse
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.Robolectric
+import org.robolectric.RobolectricTestRunner
+import org.robolectric.annotation.Config
+import java.text.SimpleDateFormat
+import java.util.Date
+import java.util.Locale
+
+@RunWith(RobolectricTestRunner::class)
+@Config(application = EmptyApplication::class)
+class ManualCrashReportCrashTest {
+    private val originalHandler = Thread.getDefaultUncaughtExceptionHandler()
+
+    @After
+    fun tearDown() {
+        unmockkAll()
+        Thread.setDefaultUncaughtExceptionHandler(originalHandler)
+    }
+
+    @Test
+    fun `Never ignores a crash after a manual report`() {
+        assumeFalse("ACRA's report file names contain ':', which Windows doesn't allow", OS.WINDOWS.isRunning())
+        val application = ApplicationProvider.getApplicationContext<Application>()
+        val crash = IllegalStateException("crash after a manual report")
+        // Leave collection, approval and sender delivery real; replace only slow collectors and transports.
+        mockkConstructor(CrashReportDataFactory::class)
+        every { anyConstructed<CrashReportDataFactory>().createCrashData(any()) } answers {
+            reportData(application, firstArg<ReportBuilder>().exception)
+        }
+        every { anyConstructed<CrashReportDataFactory>().collectStartUp() } returns Unit
+        mockkConstructor(ToastInteraction::class)
+        every { anyConstructed<ToastInteraction>().performInteraction(any(), any(), any()) } returns true
+        mockkConstructor(DefaultSenderScheduler::class)
+        every { anyConstructed<DefaultSenderScheduler>().scheduleReportSending(any()) } returns Unit
+        mockkConstructor(HttpSender::class)
+        every { anyConstructed<HttpSender>().send(any(), any(), any()) } returns Unit
+        mockkObject(Analytics)
+        every { Analytics.sendAnalyticsException(any(), any()) } returns Unit
+        with(application) { initializeAcraCrashReporter() }
+        application.sharedPrefs().edit(commit = true) { putString(FEEDBACK_REPORT_KEY, FEEDBACK_REPORT_NEVER) }
+
+        CrashReportService.sendReport(Robolectric.buildActivity(AnkiActivity::class.java).get())
+        // This enters the same ReportExecutor as uncaughtException without terminating the JVM.
+        ACRA.errorReporter.handleException(crash)
+        val senderConfig = CrashReportService.acraCoreConfigBuilder.withSendReportsInDevMode(true).build()
+        SendingConductor(application, senderConfig).sendReports(false, Bundle())
+
+        verify(exactly = 0) {
+            anyConstructed<HttpSender>().send(any(), match { it.getString(ReportField.STACK_TRACE) == crash.toString() }, any())
+        }
+    }
+
+    private fun reportData(
+        application: Application,
+        exception: Throwable?,
+    ) = CrashReportData().apply {
+        put(ReportField.STACK_TRACE, exception.toString())
+        put(ReportField.USER_CRASH_DATE, SimpleDateFormat(ACRAConstants.DATE_TIME_FORMAT_STRING, Locale.ENGLISH).format(Date()))
+        put(ReportField.FILE_PATH, application.filesDir.absolutePath)
+        put(
+            ReportField.APP_VERSION_CODE,
+            PackageInfoCompat.getLongVersionCode(application.packageManager.getPackageInfo(application.packageName, 0)),
+        )
+    }
+}

Found while reviewing https://github.com/ankidroid/Anki-Android/pull/23337

Dominant language
Kotlin
Stars
12.3k
Forks
3.1k
Avg merge
2d 1h
Merged PRs (30d)
311

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ankidroid/Anki-Android

All issues in ankidroid/Anki-Android

Similar issues

More Kotlin issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.