Skip to content

aosp: Defer Start until there's a surface - #13245

Merged
jellefoks merged 2 commits into
youtube:mainfrom
ogarai:yts/fix_aosp_background_during_startup
Oct 23, 2026
Merged

jellefoks merged 2 commits into
youtube:mainfrom
ogarai:yts/fix_aosp_background_during_startup

Conversation

@ogarai

@ogarai ogarai commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

If the app goes to the background while Cobalt is still loading, there
is no surface when Start runs. SbWindowCreate() then returns an invalid
window and Cobalt aborts on a CHECK.

Don't start Cobalt until Android provides a surface. If the app was
backgrounded before Start, start it once the app comes back. A Stop
before Start now ends the event loop, and other events before Start
aren't passed to the app.

Clear the surface under the same lock as window creation, so no window
is created on a surface that is going away. Skip the 2 s release wait
in surfaceDestroyed() when no window holds the surface.

This is the first part of the fix. Backgrounding after Start has begun
still makes SbWindowCreate() fail. A follow-up will handle that failure
in Ozone and the lifecycle code, as proposed in the review.

Bug: 532158865

@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor
Context State Description
CI / All Pass ✅ PASS All checks passed
CI / android ✅ PASS Passed
CI / evergreen ✅ PASS Passed
CI / linux ✅ PASS Passed
CI / tvos ✅ PASS Passed
@ogarai
ogarai requested a review from jellefoks October 8, 2026 21:30
@ogarai
ogarai requested a review from rogerzanoni October 8, 2026 21:30
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

🤖 Gemini Suggested Commit Message


android: Fix startup crash when backgrounded

If the application is moved to the background before Cobalt creates
its window, SbWindowCreate would fail to find a surface and cause
a crash. This change ensures the application waits for a valid
surface instead of aborting. It also synchronizes surface
management to prevent windows from being created on surfaces that
are about to be destroyed.

Bug: 532158865

💡 Pro Tips for a Better Commit Message:

  1. Influence the Result: Want to change the output? You can write custom prompts or instructions directly in the Pull Request description. The model uses that text to generate the message.
  2. Re-run the Generator: Post a comment with: /generate-commit-message

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a thread-safe mechanism to wait for an Android window surface when creating a window in the AOSP application implementation. Specifically, CreateWindow now loops and blocks using a new condition variable helper WaitForWindowSurface if the surface is not immediately available. Additionally, ReleaseWindowSurfaceAndWait is updated to clear the surface first under a new mutex (window_mutex_) to prevent race conditions during window creation. I have no further feedback to provide as the changes are well-structured and there are no review comments to address.

@ogarai ogarai changed the title aosp: Fix a crash when backgrounded at startup Oct 8, 2026
@rogerzanoni

Copy link
Copy Markdown
Collaborator

@ogarai LGTM, I couldn't reproduce the crash with this fix.

@ogarai ogarai changed the title android: Fix startup crash when backgrounded Oct 20, 2026
@jellefoks

Copy link
Copy Markdown
Member

Thanks for updating the PR — the Step 1 changes and the kStateUnstarted handling in Application::DispatchAndDelete() look very clean. LGTM.

It looks like the branch just needs a quick rebase onto main to resolve the conflict with #13498 (which flattened starboard/aosp/shared to the single-level starboard namespace, so android::shared::HasWindowSurface() in ApplicationAOSP::IsStartImmediate() becomes HasWindowSurface()). I also re-triggered the failed android and evergreen CI jobs, which hit a transient lab device allocation outage.

If the app went to the background before Cobalt created its window,
SbWindowCreate() found no surface and Cobalt aborted on a CHECK.

Wait for a new surface instead. Clear the surface under the same lock
as window creation, so no window is created on a surface that is going
away. Skip the 2 s release wait in surfaceDestroyed() when no window
holds the surface.

Test: Pressing HOME 0.8 s after launch crashed Cobalt in 1 of 10
runs. With this change 20 of 20 runs keep running. Without a window,
surfaceDestroyed() returns immediately.

Bug: 532158865
@ogarai
ogarai force-pushed the yts/fix_aosp_background_during_startup branch from 8245f05 to 5fbd6f6 Compare October 23, 2026 14:31
@ogarai

ogarai commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for updating the PR — the Step 1 changes and the kStateUnstarted handling in Application::DispatchAndDelete() look very clean. LGTM.

It looks like the branch just needs a quick rebase onto main to resolve the conflict with #13498 (which flattened starboard/aosp/shared to the single-level starboard namespace, so android::shared::HasWindowSurface() in ApplicationAOSP::IsStartImmediate() becomes HasWindowSurface()). I also re-triggered the failed android and evergreen CI jobs, which hit a transient lab device allocation outage.

Thanks @jellefoks ! Rebased.

@jellefoks
jellefoks merged commit 42eb10d into youtube:main Oct 23, 2026
553 of 555 checks passed
cobalt-github-releaser-bot pushed a commit that referenced this pull request Oct 23, 2026
Refer to original PR: #13245

If the app goes to the background while Cobalt is still loading, there
is no surface when Start runs. SbWindowCreate() then returns an invalid
window and Cobalt aborts on a CHECK.

Don't start Cobalt until Android provides a surface. If the app was
backgrounded before Start, start it once the app comes back. A Stop
before Start now ends the event loop, and other events before Start
aren't passed to the app.

Clear the surface under the same lock as window creation, so no window
is created on a surface that is going away. Skip the 2 s release wait
in surfaceDestroyed() when no window holds the surface.

This is the first part of the fix. Backgrounding after Start has begun
still makes SbWindowCreate() fail. A follow-up will handle that failure
in Ozone and the lifecycle code, as proposed in the review.

Bug: 532158865

(cherry picked from commit 42eb10d)
briantting pushed a commit that referenced this pull request Oct 27, 2026
Refer to original PR: #13245

If the app goes to the background while Cobalt is still loading, there
is no surface when Start runs. SbWindowCreate() then returns an invalid
window and Cobalt aborts on a CHECK.

Don't start Cobalt until Android provides a surface. If the app was
backgrounded before Start, start it once the app comes back. A Stop
before Start now ends the event loop, and other events before Start
aren't passed to the app.

Clear the surface under the same lock as window creation, so no window
is created on a surface that is going away. Skip the 2 s release wait
in surfaceDestroyed() when no window holds the surface.

This is the first part of the fix. Backgrounding after Start has begun
still makes SbWindowCreate() fail. A follow-up will handle that failure
in Ozone and the lifecycle code, as proposed in the review.

Bug: 532158865

(cherry picked from commit 42eb10d)
jellefoks pushed a commit that referenced this pull request Oct 30, 2026
Since #13245, kSbEventTypeStart waits for a surface. The surface can
still go away before Cobalt creates its first window, for example on
HOME during startup. SbWindowCreate() then fails, and Cobalt crashes
on a CHECK in PlatformWindowStarboard::Show().

Handle the missing window instead:
- Log the failure in Show() and Restore() and leave the window unset.
  The Restore() on the next Reveal creates it. Until then there's no
  widget, so don't pass one to the compositor in the Views host.
- Conceal after Start or Reveal without a window, so the next Focus
  reveals the app and creates the window. Because of this:
  - Wait for the conceal in HandleEvent(Start), just like a Conceal
    event.
  - Retry a failed Reveal once if a Focus arrives during it, so the
    Focus isn't lost.
  - Don't wait for a Reveal ACK without a window, and drop a pending
    one on Conceal.
  - ACK at once for a frame already in the target state. It has no
    state change to report, so its ACK would never come.
  - Ignore a Blur while concealed or concealing. The platform doesn't
    know Cobalt concealed itself, so it can still send a Blur.
- Keep every WebContents on Conceal for the next Reveal. HOME during
  the splash screen used to come back black. Because the hidden main
  page now comes back, show the splash again on a Reveal during the
  splash, until the main page has loaded.

This implements steps 2 and 3 of the design @jellefoks proposed in the

Test: cobalt_unittests. On device, HOME during startup and back: no
crash, and the app returns to the splash screen or the page. Android
TV with and without the Starboard lifecycle: no new failures.

Bug: 532158865
@ogarai
ogarai deleted the yts/fix_aosp_background_during_startup branch November 4, 2026 14:50
cobalt-github-releaser-bot pushed a commit that referenced this pull request Nov 23, 2026
Refer to original PR: #13722

Since #13245, kSbEventTypeStart waits for a surface. The surface can
still go away before Cobalt creates its first window, for example on
HOME during startup. SbWindowCreate() then fails, and Cobalt crashes
on a CHECK in PlatformWindowStarboard::Show().

Handle the missing window instead:
- Log the failure in Show() and Restore() and leave the window unset.
  The Restore() on the next Reveal creates it. Until then there's no
  widget, so don't pass one to the compositor in the Views host.
- Conceal after Start or Reveal without a window, so the next Focus
  reveals the app and creates the window. Because of this:
  - Wait for the conceal in HandleEvent(Start), just like a Conceal
    event.
  - Retry a failed Reveal once if a Focus arrives during it, so the
    Focus isn't lost.
  - Don't wait for a Reveal ACK without a window, and drop a pending
    one on Conceal.
  - ACK at once for a frame already in the target state. It has no
    state change to report, so its ACK would never come.
  - Ignore a Blur while concealed or concealing. The platform doesn't
    know Cobalt concealed itself, so it can still send a Blur.
- Keep every WebContents on Conceal for the next Reveal. HOME during
  the splash screen used to come back black. Because the hidden main
  page now comes back, show the splash again on a Reveal during the
  splash, until the main page has loaded.

This implements steps 2 and 3 of the design @jellefoks proposed in the

Test: cobalt_unittests. On device, HOME during startup and back: no
crash, and the app returns to the splash screen or the page. Android
TV with and without the Starboard lifecycle: no new failures.

Bug: 532158865

(cherry picked from commit 4f174e6)
cobalt-github-releaser-bot pushed a commit that referenced this pull request Nov 23, 2026
Refer to original PR: #13722

Since #13245, kSbEventTypeStart waits for a surface. The surface can
still go away before Cobalt creates its first window, for example on
HOME during startup. SbWindowCreate() then fails, and Cobalt crashes
on a CHECK in PlatformWindowStarboard::Show().

Handle the missing window instead:
- Log the failure in Show() and Restore() and leave the window unset.
  The Restore() on the next Reveal creates it. Until then there's no
  widget, so don't pass one to the compositor in the Views host.
- Conceal after Start or Reveal without a window, so the next Focus
  reveals the app and creates the window. Because of this:
  - Wait for the conceal in HandleEvent(Start), just like a Conceal
    event.
  - Retry a failed Reveal once if a Focus arrives during it, so the
    Focus isn't lost.
  - Don't wait for a Reveal ACK without a window, and drop a pending
    one on Conceal.
  - ACK at once for a frame already in the target state. It has no
    state change to report, so its ACK would never come.
  - Ignore a Blur while concealed or concealing. The platform doesn't
    know Cobalt concealed itself, so it can still send a Blur.
- Keep every WebContents on Conceal for the next Reveal. HOME during
  the splash screen used to come back black. Because the hidden main
  page now comes back, show the splash again on a Reveal during the
  splash, until the main page has loaded.

This implements steps 2 and 3 of the design @jellefoks proposed in the

Test: cobalt_unittests. On device, HOME during startup and back: no
crash, and the app returns to the splash screen or the page. Android
TV with and without the Starboard lifecycle: no new failures.

Bug: 532158865

(cherry picked from commit 4f174e6)
cobalt-roller-and-merger Bot pushed a commit that referenced this pull request Nov 23, 2026
Refer to original PR: #13722

Since #13245, kSbEventTypeStart waits for a surface. The surface can
still go away before Cobalt creates its first window, for example on
HOME during startup. SbWindowCreate() then fails, and Cobalt crashes
on a CHECK in PlatformWindowStarboard::Show().

Handle the missing window instead:
- Log the failure in Show() and Restore() and leave the window unset.
  The Restore() on the next Reveal creates it. Until then there's no
  widget, so don't pass one to the compositor in the Views host.
- Conceal after Start or Reveal without a window, so the next Focus
  reveals the app and creates the window. Because of this:
  - Wait for the conceal in HandleEvent(Start), just like a Conceal
    event.
  - Retry a failed Reveal once if a Focus arrives during it, so the
    Focus isn't lost.
  - Don't wait for a Reveal ACK without a window, and drop a pending
    one on Conceal.
  - ACK at once for a frame already in the target state. It has no
    state change to report, so its ACK would never come.
  - Ignore a Blur while concealed or concealing. The platform doesn't
    know Cobalt concealed itself, so it can still send a Blur.
- Keep every WebContents on Conceal for the next Reveal. HOME during
  the splash screen used to come back black. Because the hidden main
  page now comes back, show the splash again on a Reveal during the
  splash, until the main page has loaded.

This implements steps 2 and 3 of the design @jellefoks proposed in the

Test: cobalt_unittests. On device, HOME during startup and back: no
crash, and the app returns to the splash screen or the page. Android
TV with and without the Starboard lifecycle: no new failures.

Bug: 532158865

(cherry picked from commit 4f174e6)
cobalt-roller-and-merger Bot pushed a commit that referenced this pull request Nov 24, 2026
Refer to original PR: #13722

Since #13245, kSbEventTypeStart waits for a surface. The surface can
still go away before Cobalt creates its first window, for example on
HOME during startup. SbWindowCreate() then fails, and Cobalt crashes
on a CHECK in PlatformWindowStarboard::Show().

Handle the missing window instead:
- Log the failure in Show() and Restore() and leave the window unset.
  The Restore() on the next Reveal creates it. Until then there's no
  widget, so don't pass one to the compositor in the Views host.
- Conceal after Start or Reveal without a window, so the next Focus
  reveals the app and creates the window. Because of this:
  - Wait for the conceal in HandleEvent(Start), just like a Conceal
    event.
  - Retry a failed Reveal once if a Focus arrives during it, so the
    Focus isn't lost.
  - Don't wait for a Reveal ACK without a window, and drop a pending
    one on Conceal.
  - ACK at once for a frame already in the target state. It has no
    state change to report, so its ACK would never come.
  - Ignore a Blur while concealed or concealing. The platform doesn't
    know Cobalt concealed itself, so it can still send a Blur.
- Keep every WebContents on Conceal for the next Reveal. HOME during
  the splash screen used to come back black. Because the hidden main
  page now comes back, show the splash again on a Reveal during the
  splash, until the main page has loaded.

This implements steps 2 and 3 of the design @jellefoks proposed in the

Test: cobalt_unittests. On device, HOME during startup and back: no
crash, and the app returns to the splash screen or the page. Android
TV with and without the Starboard lifecycle: no new failures.

Bug: 532158865

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

4 participants