Skip to content

Commit 482dd30

Browse files
author
OpenClaw Agent (on behalf of BennyAI2)
committed
fix(chat): coalesce Publish() bursts to prevent WinUI3 layout-cycle crash
Rapid Publish() calls during fast tool-activity bursts each independently scheduled a Changed?.Invoke dispatch via _post(). When events arrived faster than the WinUI3 dispatcher could drain them, the Reactor visual tree was mutated multiple times before a layout pass settled, which crashed the tray process with Microsoft.UI.Xaml.LayoutCycleException ("Layout cycle detected" / "Element is already the child of another element"). Coalesce bursts so only the latest snapshot per drain cycle is published, using a small lock-guarded pending-snapshot flag. This matches the existing UI-thread marshaling contract (Action<Action> _post) and does not change observable behavior when updates are not bursty. Reproduced twice on a live install (2026-08-30 and 2026-09-01) via crash.log + Windows Event Log correlation with openclaw-tray.log; both crashes occurred within ~150ms of a fast tool-call event burst followed by a sessions.changed-triggered refresh.
1 parent f46400a commit 482dd30

1 file changed

Lines changed: 43 additions & 3 deletions

File tree

‎src/OpenClaw.Tray.WinUI/Chat/OpenClawChatDataProvider.cs‎

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public sealed class OpenClawChatDataProvider : IChatDataProvider
7878
private readonly ChatHistoryLoader _historyLoader;
7979
private readonly Action<Action>? _post;
8080
private readonly Func<Func<Task>, Task> _deferredAbortScheduler;
81+
private readonly object _publishGate = new();
82+
private ChatDataSnapshot? _pendingPublishSnapshot;
83+
private bool _publishScheduled;
8184

8285
/// <summary>Whether any thread is in an aborted state (suppress TTS/notifications).</summary>
8386
public bool IsResponseSuppressed => _state.IsResponseSuppressed;
@@ -1809,14 +1812,51 @@ private ChatProjectionContext ProjectionContext() =>
18091812

18101813
private void Publish(ChatDataSnapshot snapshot)
18111814
{
1812-
var args = new ChatDataChangedEventArgs(snapshot);
18131815
if (_post is null)
18141816
{
1815-
Changed?.Invoke(this, args);
1817+
Changed?.Invoke(this, new ChatDataChangedEventArgs(snapshot));
18161818
}
18171819
else
18181820
{
1819-
_post(() => Changed?.Invoke(this, args));
1821+
// Coalesce bursts of Publish() calls (rapid tool-activity grouping
1822+
// updates, streaming tokens) into a single UI-thread dispatch per
1823+
// drain. Without this, a burst of N Publish() calls enqueues N
1824+
// separate Changed?.Invoke dispatches; when they arrive faster
1825+
// than the dispatcher drains them, the Reactor tree mutates
1826+
// repeatedly before WinUI3 settles a layout pass. That produced
1827+
// field crashes: Microsoft.UI.Xaml.LayoutCycleException
1828+
// ("Layout cycle detected" / "Element is already the child of
1829+
// another element") when tool-call Expander controls are re-keyed
1830+
// mid-burst (see ToolCallCardRenderer / ChatToolActivityPresentation
1831+
// grouping, whose row keys shift as new tool entries arrive
1832+
// faster than the UI can settle). Only the latest snapshot matters
1833+
// for rendering, so dropping superseded intermediate snapshots
1834+
// within one burst is safe and loses no information the UI would
1835+
// have shown anyway.
1836+
bool shouldSchedule;
1837+
lock (_publishGate)
1838+
{
1839+
_pendingPublishSnapshot = snapshot;
1840+
shouldSchedule = !_publishScheduled;
1841+
_publishScheduled = true;
1842+
}
1843+
1844+
if (shouldSchedule)
1845+
{
1846+
_post(() =>
1847+
{
1848+
ChatDataSnapshot? toPublish;
1849+
lock (_publishGate)
1850+
{
1851+
toPublish = _pendingPublishSnapshot;
1852+
_pendingPublishSnapshot = null;
1853+
_publishScheduled = false;
1854+
}
1855+
1856+
if (toPublish is not null)
1857+
Changed?.Invoke(this, new ChatDataChangedEventArgs(toPublish));
1858+
});
1859+
}
18201860
}
18211861

18221862
// Debounce-save last-known UI state so the next launch can show

0 commit comments

Comments
 (0)