Skip to content

Prevent unregistering actionReceiver twice and handle weird lifecycle#6913

Merged
TimoPtr merged 2 commits into
mainfrom
feature/catch_alreadyunregistered
May 30, 2026
Merged

Prevent unregistering actionReceiver twice and handle weird lifecycle#6913
TimoPtr merged 2 commits into
mainfrom
feature/catch_alreadyunregistered

Conversation

@TimoPtr
Copy link
Copy Markdown
Member

@TimoPtr TimoPtr commented May 27, 2026

Summary

We have 1k users impacted by a crash of the AssistVoiceInteractionService while onShutdown is invoked, this PR catch the exception. It seems to happen because the system already unregistered the actionReceiver.
image

This PR also handle some weirdness of the lifecycle of the service here the documentation of onReady

    /**
     * Called during service initialization to tell you when the system is ready
     * to receive interaction from it. You should generally do initialization here
     * rather than in {@link #onCreate}. Methods such as {@link #showSession} will
     * not be operational until this point.
     *
     * <p>Note: Under some circumstances, it is possible for {@link #onReady} to be called after
     * {@link #onDestroy} has been called. This can happen in cases where the system is in the
     * process of shutting down the service, but the {@code IVoiceInteractionManagerService}
     * still sends a final "ready" signal. Therefore, implementations must be prepared for
     * {@link #onReady} to be called even after {@link #onDestroy}, and should not rely on a
     * fixed lifecycle order.
     */

and onShutdown

    /**
     * Called during service de-initialization to tell you when the system is shutting the
     * service down.
     * At this point this service may no longer be the active {@link VoiceInteractionService}.
     *
     * <p>Note: After this method is called, it is possible for other methods on this service
     * to be called, including {@link #onReady}. Implementations should be prepared to handle
     * this case gracefully.
     */

Checklist

  • New or updated tests have been added to cover the changes following the testing guidelines.
  • The code follows the project's code style and best_practices.
  • The changes have been thoroughly tested, and edge cases have been considered.
  • Changes are backward compatible whenever feasible. Any breaking changes are documented in the changelog for users and/or in the code for developers depending on the relevance.

Copilot AI review requested due to automatic review settings May 27, 2026 15:17
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens AssistVoiceInteractionService against unusual platform lifecycle ordering, especially late onReady() calls and receiver unregister crashes during shutdown.

Changes:

  • Adds a teardown latch to ignore onReady() after shutdown or destroy.
  • Defensively handles already-unregistered action receivers in onShutdown().
  • Adds Robolectric tests for late lifecycle callbacks and duplicate unregister behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
app/src/main/kotlin/io/homeassistant/companion/android/assist/service/AssistVoiceInteractionService.kt Adds lifecycle teardown guarding and defensive receiver unregister handling.
app/src/test/kotlin/io/homeassistant/companion/android/assist/service/AssistVoiceInteractionServiceTest.kt Adds tests covering late onReady() and already-unregistered receiver scenarios.

Copy link
Copy Markdown
Member

@jpelgrom jpelgrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that we have so many users with the issue but didn't encounter it during testing 🤔

…service/AssistVoiceInteractionService.kt

Co-authored-by: Joris Pelgröm <jpelgrom@users.noreply.github.com>
@TimoPtr TimoPtr enabled auto-merge (squash) May 30, 2026 17:45
@TimoPtr TimoPtr disabled auto-merge May 30, 2026 17:45
@TimoPtr TimoPtr enabled auto-merge (squash) May 30, 2026 17:45
@TimoPtr TimoPtr merged commit 123eeb1 into main May 30, 2026
52 of 54 checks passed
@TimoPtr TimoPtr deleted the feature/catch_alreadyunregistered branch May 30, 2026 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants