Skip to content

Fix: Add context.mounted checks across async gaps to prevent crashes#23

Merged
insign merged 2 commits intomainfrom
fix-use-build-context-synchronously-75362650297974914
Apr 5, 2026
Merged

Fix: Add context.mounted checks across async gaps to prevent crashes#23
insign merged 2 commits intomainfrom
fix-use-build-context-synchronously-75362650297974914

Conversation

@insign
Copy link
Copy Markdown
Contributor

@insign insign commented Apr 5, 2026

This PR adds missing context.mounted checks after asynchronous operations to prevent potential runtime crashes caused by unmounted widgets. By ensuring we do not operate on invalid contexts, this change improves application stability and resolves use_build_context_synchronously analyzer warnings.

Changes:

  • lib/presentation/pages/chat_page/chat_page_workspace_controller.dart: Added if (!mounted) return; check in _closeProjectContext.
  • lib/presentation/pages/settings/sections/about_settings_section.dart: Replaced if (!mounted) with if (!context.mounted) to properly guard passed BuildContexts in _triggerOnboardingTour and _confirmResetApp.
  • lib/presentation/widgets/chat_message/chat_message_text_part.dart: Added if (!context.mounted) return; before calling _showLinkOpenFeedback after await launchUrl(...).

The make check suite confirms that tests pass and the number of analyzer issues has dropped as a result of these fixes.


PR created automatically by Jules for task 75362650297974914 started by @insign

Adds missing context.mounted checks across async gaps in several
files to prevent potential runtime crashes when widgets are unmounted
while async operations are pending. This improves the overall
reliability of the application and clears analyzer warnings.

Co-authored-by: insign <1113045+insign@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0e37c0723e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

if (!changed || !wasActiveProject) {
return;
}
if (!mounted) return;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Keep chat scope sync even if page unmounts during close

_closeProjectContext now returns when mounted is false before calling ChatProvider.onProjectScopeChanged(). If the async closeProject call completes after the chat page is disposed (for example, user closes a project then quickly navigates away), the project context changes but chat state is never switched, leaving ChatProvider on the previous scope until some later manual refresh. This regression is specific to the new guard and can cause stale sessions/models after closing the active project.

Useful? React with 👍 / 👎.

@insign
Copy link
Copy Markdown
Contributor Author

insign commented Apr 5, 2026

_closeProjectContext now returns when mounted is false before calling ChatProvider.onProjectScopeChanged(). If the async closeProject call completes after the chat page is disposed (for example, user closes a project then quickly navigates away), the project context changes but chat state is never switched, leaving ChatProvider on the previous scope until some later manual refresh. This regression is specific to the new guard and can cause stale sessions/models after closing the active project.

@google-labs-jules
Copy link
Copy Markdown
Contributor

_closeProjectContext now returns when mounted is false before calling ChatProvider.onProjectScopeChanged(). If the async closeProject call completes after the chat page is disposed (for example, user closes a project then quickly navigates away), the project context changes but chat state is never switched, leaving ChatProvider on the previous scope until some later manual refresh. This regression is specific to the new guard and can cause stale sessions/models after closing the active project.

You're absolutely right, returning early on !mounted skipped the critical state sync. I've updated the fix to lookup and cache the ChatProvider before the async gap so the state still updates reliably even if the widget unmounts. Thanks for catching that regression.

Refactors the async gap fix in `_closeProjectContext` to capture the
`ChatProvider` reference prior to the async call. This resolves the
issue where skipping state synchronization when unmounted caused stale
chat state if the user closed a project and navigated away quickly.

Co-authored-by: insign <1113045+insign@users.noreply.github.com>
@insign insign merged commit 8eb08d5 into main Apr 5, 2026
13 checks passed
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