Skip to content

Cherry pick PR #9798: Cobalt: Fix application lifecycle and focus reporting issues#10011

Merged
jellefoks merged 1 commit intomainfrom
cherry-pick-main-9798
Apr 15, 2026
Merged

Cherry pick PR #9798: Cobalt: Fix application lifecycle and focus reporting issues#10011
jellefoks merged 1 commit intomainfrom
cherry-pick-main-9798

Conversation

@cobalt-github-releaser-bot
Copy link
Copy Markdown
Collaborator

Refer to the original PR: #9798

This PR significantly hardens the Cobalt application lifecycle, specifically addressing synchronous blocking requirements during downward state transitions and improving the robustness of the message pump for nested loops.

Key Improvements

  • MessagePumpUIStarboard nested loop support: Implemented the previously missing Run() method (which formerly threw a NOTREACHED()) to support a full Chromium-style message loop. This is critical for enabling nested base::RunLoop calls, allowing the main OS thread to block during teardown while the UI thread continues to process asynchronous tasks (e.g., surface destruction). The loop blocks highly efficiently using base::WaitableEvent::TimedWait().
  • Synchronous State Transitions: Refactored AppEventDelegate to explicitly block the Starboard OS thread during downward transitions (Blur, Conceal, Freeze) until the target state is reached and all asynchronous side effects are completed. This uses a nested RunLoop on the UI thread and dynamically instantiated WaitableEvents on non-UI threads. Added a 5-second safety timeout (kTransitionTimeout) to prevent permanent Starboard thread hangs.
  • Stability & Thread Safety:
    • Deadlock Prevention: The final kStopped state transition (which destroys the SequenceManager) is now safely executed synchronously outside of the nested RunLoop. Attempting to tear down the task queue from within one of its own tasks would cause a fatal deadlock.
    • Replaced ContentMain with RunContentProcess using a process-lifetime static ContentMainRunner to resolve SIGSEGV issues during shutdown.
    • Introduced base::Lock and std::atomic state variables in AppEventDelegate and AppEventRunner to serialize lifecycle event processing and ensure thread safety across multiple threads without lock contention.
    • Improved MessagePumpUIStarboard destruction (calling CancelAll() and AfterRun()) to prevent DCHECK crashes in SequenceManager during teardown.
  • Visibility & Focus Reporting:
    • Added explicit calls to web_contents()->WasHidden() and WasShown() in ShellPlatformDelegate to ensure the compositor and Aura correctly track visibility changes.
    • Deterministic Focus: Implemented a robust focus deferral mechanism in cobalt::Shell. Focus requests arriving while the window is hidden are queued and applied precisely when Aura signals visibility via OnVisibilityChanged.
  • Robust Integration Tests: Rewrote test_lifecycle.sh and test_preload.sh to include precise process cleanup validation (handling zombie states) and a new wait_for_state.sh helper that polls for JS state via DevTools, eliminating race conditions previously handled by brittle sleep commands.

Bug: 492166511
Bug: 491841704
Bug: 475990372
Bug: 477170017
Bug: 474496110
Bug: 492693465
Bug: 494002968

@cobalt-github-releaser-bot

This comment was marked as resolved.

@github-actions

This comment was marked as off-topic.

@jellefoks jellefoks force-pushed the cherry-pick-main-9798 branch 5 times, most recently from fecba7c to 96e0a65 Compare April 14, 2026 16:52
@jellefoks jellefoks marked this pull request as ready for review April 14, 2026 16:52
@jellefoks jellefoks requested review from a team as code owners April 14, 2026 16:52
@jellefoks jellefoks requested a review from briantting April 14, 2026 16:52
gemini-code-assist[bot]

This comment was marked as resolved.

@datadog-cobalt-youtube

This comment was marked as resolved.

This PR significantly hardens the Cobalt application lifecycle,
specifically addressing synchronous blocking requirements during
downward state transitions and improving the robustness of the message
pump for nested loops.

* **MessagePumpUIStarboard nested loop support:** Implemented the
previously missing `Run()` method (which formerly threw a
`NOTREACHED()`) to support a full Chromium-style message loop. This is
critical for enabling nested `base::RunLoop` calls, allowing the main OS
thread to block during teardown while the UI thread continues to process
asynchronous tasks (e.g., surface destruction). The loop blocks highly
efficiently using `base::WaitableEvent::TimedWait()`.
* **Synchronous State Transitions:** Refactored `AppEventDelegate` to
explicitly block the Starboard OS thread during downward transitions
(Blur, Conceal, Freeze) until the target state is reached and all
asynchronous side effects are completed. This uses a nested `RunLoop` on
the UI thread and dynamically instantiated `WaitableEvent`s on non-UI
threads. Added a 5-second safety timeout (`kTransitionTimeout`) to
prevent permanent Starboard thread hangs.
*   **Stability & Thread Safety:**
* **Deadlock Prevention:** The final `kStopped` state transition (which
destroys the `SequenceManager`) is now safely executed *synchronously*
outside of the nested `RunLoop`. Attempting to tear down the task queue
from within one of its own tasks would cause a fatal deadlock.
* Replaced `ContentMain` with `RunContentProcess` using a
process-lifetime static `ContentMainRunner` to resolve SIGSEGV issues
during shutdown.
* Introduced `base::Lock` and `std::atomic` state variables in
`AppEventDelegate` and `AppEventRunner` to serialize lifecycle event
processing and ensure thread safety across multiple threads without lock
contention.
* Improved `MessagePumpUIStarboard` destruction (calling `CancelAll()`
and `AfterRun()`) to prevent `DCHECK` crashes in `SequenceManager`
during teardown.
*   **Visibility & Focus Reporting:**
* Added explicit calls to `web_contents()->WasHidden()` and `WasShown()`
in `ShellPlatformDelegate` to ensure the compositor and Aura correctly
track visibility changes.
* **Deterministic Focus:** Implemented a robust focus deferral mechanism
in `cobalt::Shell`. Focus requests arriving while the window is hidden
are queued and applied precisely when Aura signals visibility via
`OnVisibilityChanged`.
* **Robust Integration Tests:** Rewrote `test_lifecycle.sh` and
`test_preload.sh` to include precise process cleanup validation
(handling zombie states) and a new `wait_for_state.sh` helper that polls
for JS state via DevTools, eliminating race conditions previously
handled by brittle `sleep` commands.

Bug: 492166511
Bug: 491841704
Bug: 475990372
Bug: 477170017
Bug: 474496110
Bug: 492693465
Bug: 494002968
(cherry picked from commit 93935f0)
@jellefoks jellefoks force-pushed the cherry-pick-main-9798 branch from 96e0a65 to 5e0d2ed Compare April 14, 2026 18:23
@jellefoks jellefoks enabled auto-merge (squash) April 15, 2026 00:28
@jellefoks jellefoks merged commit a793a7c into main Apr 15, 2026
453 checks passed
@jellefoks jellefoks deleted the cherry-pick-main-9798 branch April 15, 2026 00:33
@github-actions github-actions bot added the cp-27.lts Cherry Pick to the 27.lts branch label Apr 15, 2026
cobalt-github-releaser-bot added a commit that referenced this pull request Apr 15, 2026
…lifecycle and focus reporting issues

Refer to original PR: #10011

Refer to the original PR: #9798

This PR significantly hardens the Cobalt application lifecycle,
specifically addressing synchronous blocking requirements during
downward state transitions and improving the robustness of the message
pump for nested loops.

#### Key Improvements

* **MessagePumpUIStarboard nested loop support:** Implemented the
previously missing `Run()` method (which formerly threw a
`NOTREACHED()`) to support a full Chromium-style message loop. This is
critical for enabling nested `base::RunLoop` calls, allowing the main OS
thread to block during teardown while the UI thread continues to process
asynchronous tasks (e.g., surface destruction). The loop blocks highly
efficiently using `base::WaitableEvent::TimedWait()`.
* **Synchronous State Transitions:** Refactored `AppEventDelegate` to
explicitly block the Starboard OS thread during downward transitions
(Blur, Conceal, Freeze) until the target state is reached and all
asynchronous side effects are completed. This uses a nested `RunLoop` on
the UI thread and dynamically instantiated `WaitableEvent`s on non-UI
threads. Added a 5-second safety timeout (`kTransitionTimeout`) to
prevent permanent Starboard thread hangs.
*   **Stability & Thread Safety:**
* **Deadlock Prevention:** The final `kStopped` state transition (which
destroys the `SequenceManager`) is now safely executed *synchronously*
outside of the nested `RunLoop`. Attempting to tear down the task queue
from within one of its own tasks would cause a fatal deadlock.
* Replaced `ContentMain` with `RunContentProcess` using a
process-lifetime static `ContentMainRunner` to resolve SIGSEGV issues
during shutdown.
* Introduced `base::Lock` and `std::atomic` state variables in
`AppEventDelegate` and `AppEventRunner` to serialize lifecycle event
processing and ensure thread safety across multiple threads without lock
contention.
* Improved `MessagePumpUIStarboard` destruction (calling `CancelAll()`
and `AfterRun()`) to prevent `DCHECK` crashes in `SequenceManager`
during teardown.
*   **Visibility & Focus Reporting:**
* Added explicit calls to `web_contents()->WasHidden()` and `WasShown()`
in `ShellPlatformDelegate` to ensure the compositor and Aura correctly
track visibility changes.
* **Deterministic Focus:** Implemented a robust focus deferral mechanism
in `cobalt::Shell`. Focus requests arriving while the window is hidden
are queued and applied precisely when Aura signals visibility via
`OnVisibilityChanged`.
* **Robust Integration Tests:** Rewrote `test_lifecycle.sh` and
`test_preload.sh` to include precise process cleanup validation
(handling zombie states) and a new `wait_for_state.sh` helper that polls
for JS state via DevTools, eliminating race conditions previously
handled by brittle `sleep` commands.

Bug: 492166511
Bug: 491841704
Bug: 475990372
Bug: 477170017
Bug: 474496110
Bug: 492693465
Bug: 494002968

Co-authored-by: Jelle Foks <jfoks@google.com>

(cherry picked from commit a793a7c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cp-27.lts Cherry Pick to the 27.lts branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants