Skip to content

fix: safe unregister SystemEventsBroadcastReceiver#5106

Open
kollesnica1337 wants to merge 4 commits intogetsentry:mainfrom
kollesnica1337:fix/safe-reciever-unregister
Open

fix: safe unregister SystemEventsBroadcastReceiver#5106
kollesnica1337 wants to merge 4 commits intogetsentry:mainfrom
kollesnica1337:fix/safe-reciever-unregister

Conversation

@kollesnica1337
Copy link

@kollesnica1337 kollesnica1337 commented Feb 20, 2026

📜 Description

  • Add try-catch block in SystemEventsBreadcrumbsIntegration.unregisterReceiver() method
  • Add test for this case

💡 Motivation and Context

I'm struggling with a crash in production that is quite tricky to reproduce:

java.lang.IllegalArgumentException: Receiver not registered: io.sentry.android.core.SystemEventsBreadcrumbsIntegration$b@f292929
    at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:1770)
    at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1899)
    at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:829)
    at androidx.lifecycle.DefaultLifecycleObserverAdapter.onStateChanged(DefaultLifecycleObserverAdapter.kt:0)
    at androidx.lifecycle.DefaultLifecycleObserverAdapter.onStateChanged(DefaultLifecycleObserverAdapter.kt:28)
    at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.jvm.kt:0)
    at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.jvm.kt:320)
    at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.jvm.kt:0)
    at androidx.lifecycle.LifecycleRegistry.backwardPass(LifecycleRegistry.jvm.kt:272)
    at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.jvm.kt:289)
    at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.jvm.kt:0)
    at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.jvm.kt:142)
    at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.jvm.kt:124)
    at androidx.lifecycle.ProcessLifecycleOwner.activityStopped$lifecycle_process_release(ProcessLifecycleOwner.kt:0)
    at androidx.lifecycle.ProcessLifecycleOwner.dispatchStopIfNeeded$lifecycle_process_release(ProcessLifecycleOwner.kt:135)
    at androidx.lifecycle.ProcessLifecycleOwner.activityStopped$lifecycle_process_release(ProcessLifecycleOwner.kt:123)
    at androidx.lifecycle.ProcessLifecycleOwner$attach$1.onActivityStopped(ProcessLifecycleOwner.kt:183)
    at android.app.Application.dispatchActivityStopped(Application.java:510)
    at android.app.Activity.dispatchActivityStopped(Activity.java:1548)
    at android.app.Activity.onStop(Activity.java:2679)
    at androidx.fragment.app.FragmentActivity.onStop(FragmentActivity.java:0)
    at androidx.appcompat.app.AppCompatActivity.onStop(AppCompatActivity.java:0)
    at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1551)
    at android.app.Activity.performStop(Activity.java:8829)
    at android.app.ActivityThread.callActivityOnStop(ActivityThread.java:5835)
    at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:5809)
    at android.app.ActivityThread.handleStopActivity(ActivityThread.java:5880)
    at android.app.servertransaction.StopActivityItem.execute(StopActivityItem.java:43)
    at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2574)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:226)
    at android.os.Looper.loop(Looper.java:313)
    at android.app.ActivityThread.main(ActivityThread.java:8762)
    at java.lang.reflect.Method.invoke(Method.java)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)

It seems like in certain cases Android can kill SystemEventsBroadcastReceiver before SystemEventsBreadcrumbsIntegration calls unregisterReceiver() after app going background.

💚 How did you test it?

Added test case for this problem.

📝 Checklist

  • I added GH Issue ID & Linear ID
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments