Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue38080.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 38080, "Android SIGSEGV crash in GLFunctorDrawable when a ScrollView with an off-screen WebView is overscrolled", PlatformAffected.Android)]
public class Issue38080 : NavigationPage
{
public Issue38080() : base(new Issue38080HomePage()) { }

class Issue38080HomePage : ContentPage
{
public Issue38080HomePage()
{
Content = new VerticalStackLayout
{
VerticalOptions = LayoutOptions.Center,
Children =
{
new Button
{
AutomationId = "Issue38080NavigateButton",
Text = "Open repro page",
Command = new Command(async () => await Navigation.PushAsync(new Issue38080ReproPage()))
}
}
};
}
}

// Reproduces #38080: a single fixed-size WebView placed mid-list in a ScrollView is off-screen
// at both scroll extremes. Flinging to either extreme (or back-navigating) previously SIGSEGV'd
// the RenderThread in GLFunctorDrawable because the WebView carried a non-null ClipBounds that
// routed its off-screen compositing through the AOSP hwui GL-functor path.
class Issue38080ReproPage : ContentPage
{
const int FillerRows = 50;
const int MaxDiagnosticRefreshAttempts = 50;

public Issue38080ReproPage()
{
var stack = new VerticalStackLayout
{
Padding = new Thickness(12),
Spacing = 12
};

stack.Children.Add(new Label
{
AutomationId = "Issue38080Ready",
Text = "Page loaded — no crash"
});

var clipBoundsStatus = new Label
{
AutomationId = "Issue38080ClipBoundsStatus",
Text = "ClipBounds=pending"
};
stack.Children.Add(clipBoundsStatus);

var webViewLoadStatus = new Label
{
AutomationId = "Issue38080WebViewLoadStatus",
Text = "WebView load pending"
};

// The first/last rows are sentinels the UITest asserts on to prove the scroll
// actually reached the top/bottom extreme.
AddFiller(stack, FillerRows, sentinelRow: 1, sentinelId: "Issue38080TopSentinel");

stack.Children.Add(new Label
{
AutomationId = "Issue38080WebViewTopMarker",
Text = "Issue38080 WebView starts below"
});

// Single fixed-size WebView mid-list, so it is off-screen at both extremes.
var webView = new WebView
{
AutomationId = "Issue38080WebView",
HeightRequest = 220,
Source = new HtmlWebViewSource
{
Html = "<html><body style='background:#d6e4ff; margin:0; padding:24px'><h3 id='Issue38080HtmlProbe'>Issue38080 WebView HTML loaded</h3></body></html>"
}
};
RegisterWebViewDiagnostics(webView, clipBoundsStatus, webViewLoadStatus);
stack.Children.Add(webView);
stack.Children.Add(webViewLoadStatus);

AddFiller(stack, FillerRows, sentinelRow: FillerRows, sentinelId: "Issue38080BottomSentinel");

Content = new ScrollView
{
AutomationId = "Issue38080ScrollView",
Content = stack
};
}

static void AddFiller(VerticalStackLayout stack, int rows, int sentinelRow, string sentinelId)
{
for (int i = 1; i <= rows; i++)
{
stack.Children.Add(new Label
{
AutomationId = i == sentinelRow ? sentinelId : null,
Text = $"Filler row {i}",
FontSize = 20
});
}
}

static void RegisterWebViewDiagnostics(WebView webView, Label diagnosticsLabel, Label loadStatusLabel)
{
webView.Navigated += async (_, args) =>
{
if (args.Result != WebNavigationResult.Success)
{
loadStatusLabel.Text = $"WebView load result: {args.Result}";
return;
}

var htmlProbe = await webView.EvaluateJavaScriptAsync("document.getElementById('Issue38080HtmlProbe').textContent");
loadStatusLabel.Text = $"WebView load result: {args.Result}; HtmlProbe={htmlProbe}";
};

#if ANDROID
webView.HandlerChanged += (_, _) => UpdateClipBoundsDiagnostics(webView, diagnosticsLabel, attempt: 0);
webView.Loaded += (_, _) => UpdateClipBoundsDiagnostics(webView, diagnosticsLabel, attempt: 0);
webView.SizeChanged += (_, _) => UpdateClipBoundsDiagnostics(webView, diagnosticsLabel, attempt: 0);
#endif
}

#if ANDROID
static void UpdateClipBoundsDiagnostics(WebView webView, Label diagnosticsLabel, int attempt)
{
var nativeWebView = webView.Handler?.PlatformView as Android.Webkit.WebView;
var diagnosticsReady = false;

if (nativeWebView is null)
{
diagnosticsLabel.Text = "ClipBounds=pending; NativeWebView=null";
}
else
{
using var clipBounds = nativeWebView.ClipBounds;
var clipBoundsText = clipBounds is null
? "null"
: $"{clipBounds.Left},{clipBounds.Top},{clipBounds.Right},{clipBounds.Bottom}";
var nativeParent = nativeWebView.Parent;
var parentViewGroup = nativeParent as Android.Views.ViewGroup;
var parentClipChildren = parentViewGroup is null
? "null"
: parentViewGroup.ClipChildren.ToString();
var parentType = nativeParent?.GetType().Name ?? "null";
var isAttachedToWindow = nativeWebView.IsAttachedToWindow;
var isHardwareAccelerated = nativeWebView.IsHardwareAccelerated;

diagnosticsReady =
nativeWebView.Width > 0 &&
nativeWebView.Height > 0 &&
isAttachedToWindow &&
isHardwareAccelerated;

diagnosticsLabel.Text =
$"ClipBounds={clipBoundsText}; " +
$"Size={nativeWebView.Width}x{nativeWebView.Height}; " +
$"IsAttachedToWindow={isAttachedToWindow}; " +
$"HardwareAccelerated={isHardwareAccelerated}; " +
$"Parent={parentType}; " +
$"ParentClipChildren={parentClipChildren}";
}

if (!diagnosticsReady && attempt < MaxDiagnosticRefreshAttempts)
webView.Dispatcher.DispatchDelayed(TimeSpan.FromMilliseconds(100), () => UpdateClipBoundsDiagnostics(webView, diagnosticsLabel, attempt + 1));
else if (!diagnosticsReady)
diagnosticsLabel.Text += "; DiagnosticsTimeout=True";
}
#endif
}
}
140 changes: 140 additions & 0 deletions src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue38080.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Crash is Android-specific: RenderThread GL functor SIGSEGVs when a non-null ClipBounds
// routes an off-screen WebView's compositing through GLFunctorDrawable on overscroll.
#if ANDROID
using System;
using System.Linq;
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue38080 : _IssuesUITest
{
public Issue38080(TestDevice device) : base(device) { }

public override string Issue => "Android SIGSEGV crash in GLFunctorDrawable when a ScrollView with an off-screen WebView is overscrolled";

[Test]
[Category(UITestCategories.WebView)]
public void OffScreenWebViewInScrollViewShouldNotCrashOnOverscroll()
{
App.WaitForElement("Issue38080NavigateButton");
App.Tap("Issue38080NavigateButton");
App.WaitForElement("Issue38080Ready");

// ScrollUp/ScrollDown silently no-op when the element lookup returns null, which would
// turn this regression test into a false positive. Wait so a failed lookup fails the test.
App.WaitForElement("Issue38080ScrollView");
AssertFixedSizeWebViewIsMeasuredAndHardwareAccelerated();
ScrollToAndAssertWebViewRendered();

// Overscroll at the top extreme: the WebView is off-screen below the fold and the
// ScrollView overscrolls past its top edge.
ScrollToExtremeAndAssert("Issue38080TopSentinel", "top", scrollDown: false, swipePercentage: 0.9);

// Scroll/overscroll at the bottom extreme: the WebView is off-screen above the fold and the
// ScrollView overscrolls past its bottom edge.
// Use a mid-viewport gesture for the downward pass: once the WebView is visible near the
// bottom of the viewport, a bottom-origin fling can be consumed by the WebView itself instead
// of the parent ScrollView, leaving the test stuck at the midpoint.
ScrollToExtremeAndAssert("Issue38080BottomSentinel", "bottom", scrollDown: true, swipePercentage: 0.45);

// The WebView is now off-screen at the bottom; back navigation also reproduced the
// RenderThread crash while compositing. Assert we returned to the home page alive.
App.Back();
App.WaitForElement("Issue38080NavigateButton");
Comment thread
Copilot marked this conversation as resolved.
Resolved
}

void ScrollToExtremeAndAssert(string automationId, string extreme, bool scrollDown, double swipePercentage)
{
for (int i = 0; i < 60 && !TrySentinelDisplayed(automationId); i++)
ScrollOnce(scrollDown, swipePercentage);

AssertSentinelDisplayed(automationId, extreme);

// Exercise actual overscroll at the edge after proving we reached it.
for (int overscroll = 0; overscroll < 5; overscroll++)
ScrollOnce(scrollDown, swipePercentage);

AssertSentinelDisplayed(automationId, extreme);
}

void ScrollOnce(bool scrollDown, double swipePercentage)
{
if (scrollDown)
App.ScrollDown("Issue38080ScrollView", ScrollStrategy.Gesture, swipePercentage, 100);
else
App.ScrollUp("Issue38080ScrollView", ScrollStrategy.Gesture, swipePercentage, 100);
}

bool TrySentinelDisplayed(string automationId) =>
App.FindElements(automationId).Any(element => element.IsDisplayed());

// Displayed is a viewport check, not a view-hierarchy presence check: it proves the sentinel
// row is actually on screen at the extreme, so ignored/flaky scroll gestures fail the test
// instead of letting it pass as a false positive.
void AssertSentinelDisplayed(string automationId, string extreme)
{
App.RetryAssert(() =>
{
var sentinel = App.WaitForElement(automationId, timeout: TimeSpan.FromSeconds(5));
Assert.That(sentinel.IsDisplayed(), Is.True,
$"The {extreme} extreme was not reached: {automationId} is not displayed after scrolling");
}, timeout: TimeSpan.FromSeconds(30));
}

void AssertFixedSizeWebViewIsMeasuredAndHardwareAccelerated()
{
App.RetryAssert(() =>
{
var statusText = App.WaitForElement("Issue38080ClipBoundsStatus", timeout: TimeSpan.FromSeconds(5)).GetText();
Assert.That(statusText, Does.Match(@"Size=[1-9]\d*x[1-9]\d*"),
$"The diagnostic assertion should only pass after the native WebView has non-zero dimensions. Actual diagnostics: {statusText}");
Assert.That(statusText, Does.Contain("IsAttachedToWindow=True"),
$"The diagnostic assertion should only pass after the native WebView is attached. Actual diagnostics: {statusText}");
Assert.That(statusText, Does.Contain("HardwareAccelerated=True"),
$"This regression must exercise Android hardware-accelerated WebView rendering. Actual diagnostics: {statusText}");
Assert.That(statusText, Does.Contain("ParentClipChildren=True"),
$"The native WebView should be clipped by its Android parent wrapper. Actual diagnostics: {statusText}");
Assert.That(statusText, Does.Not.Contain("DiagnosticsTimeout=True"),
$"The native WebView diagnostics timed out before reaching a ready attached/hardware-accelerated state. Actual diagnostics: {statusText}");
}, timeout: TimeSpan.FromSeconds(30));
}

void ScrollToAndAssertWebViewRendered()
{
for (int i = 0; i < 20; i++)
{
if (IsWebViewRenderedAndDisplayed())
return;

App.ScrollDown("Issue38080ScrollView", ScrollStrategy.Gesture, 0.25, 100);
}

AssertWebViewRenderedAndDisplayed();
}

bool IsWebViewRenderedAndDisplayed()
{
var topMarker = App.FindElements("Issue38080WebViewTopMarker").FirstOrDefault();
var loadStatus = App.FindElements("Issue38080WebViewLoadStatus").FirstOrDefault();
return topMarker?.IsDisplayed() == true &&
loadStatus?.IsDisplayed() == true &&
loadStatus.GetText()?.Contains("HtmlProbe=Issue38080 WebView HTML loaded", StringComparison.Ordinal) == true;
}

void AssertWebViewRenderedAndDisplayed()
{
var topMarker = App.WaitForElement("Issue38080WebViewTopMarker", timeout: TimeSpan.FromSeconds(1));
Assert.That(topMarker.IsDisplayed(), Is.True,
"The marker immediately above the WebView must be visible before overscroll.");

var loadStatus = App.WaitForElement("Issue38080WebViewLoadStatus", timeout: TimeSpan.FromSeconds(1));
Assert.That(loadStatus.IsDisplayed(), Is.True,
"The WebView load status label immediately below the WebView should be visible with the midpoint check.");
Assert.That(loadStatus.GetText(), Does.Contain("HtmlProbe=Issue38080 WebView HTML loaded"),
"The WebView HTML must finish loading before the off-screen overscroll checks run.");
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Android.Views;
using Android.Webkit;
using static Android.Views.ViewGroup;
using AWebView = Android.Webkit.WebView;
Expand All @@ -7,6 +8,20 @@ namespace Microsoft.Maui.Handlers
{
public partial class HybridWebViewHandler : ViewHandler<IHybridWebView, AWebView>
{
/// <inheritdoc/>
public override bool NeedsContainer => true;

/// <inheritdoc/>
protected override void SetupContainer()
{
base.SetupContainer();

// Use native child clipping, including HWUI's off-screen rejection,
// rather than an explicit WebView ClipBounds rectangle (#38080).
if (ContainerView is ViewGroup container)
container.SetClipChildren(true);
}

protected override AWebView CreatePlatformView()
{
var platformView = new MauiHybridWebView(this, Context!)
Expand Down Expand Up @@ -68,7 +83,9 @@ protected override void DisconnectHandler(AWebView platformView)
//platformView.SetWebChromeClient(null);

platformView.StopLoading();

if (ContainerView?.Parent is ViewGroup containerParent)
containerParent.RemoveView(ContainerView);
HasContainer = false;

base.DisconnectHandler(platformView);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Core/src/Handlers/View/ViewHandlerOfT.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ protected override void SetupContainer() =>
WrapperView.SetupContainer(PlatformView, Context, ContainerView, (cv) => ContainerView = cv);

protected override void RemoveContainer() =>
WrapperView.RemoveContainer(PlatformView, Context, ContainerView, () => ContainerView = null);
// ElementHandler clears PlatformView before disconnecting; the wrapper
// cleanup also supports that null state.
WrapperView.RemoveContainer(base.PlatformView, Context, ContainerView, () => ContainerView = null);
}
}
18 changes: 18 additions & 0 deletions src/Core/src/Handlers/WebView/WebViewHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ public partial class WebViewHandler : ViewHandler<IWebView, AWebView>

protected internal string? UrlCanceled { get; set; }

/// <inheritdoc/>
public override bool NeedsContainer => true;

/// <inheritdoc/>
protected override void SetupContainer()
{
base.SetupContainer();

// Native child clipping both bounds WebView drawing and lets HWUI reject
// off-screen nodes before invoking Chromium during overscroll (#38080).
if (ContainerView is ViewGroup container)
container.SetClipChildren(true);
}

protected override AWebView CreatePlatformView()
{
var platformView = new MauiWebView(this, Context!)
Expand Down Expand Up @@ -75,6 +89,10 @@ protected override void DisconnectHandler(AWebView platformView)
platformView.SetWebChromeClient(null);

platformView.StopLoading();
if (ContainerView?.Parent is ViewGroup containerParent)
containerParent.RemoveView(ContainerView);
HasContainer = false;

if (platformView.Parent is ViewGroup parent)
parent.RemoveView(platformView);
platformView.RemoveAllViews();
Expand Down
Loading
Loading