Crash in Sheet.GetTexture(): Nullable<Rectangle>.Value thrown when glyphs are rasterised off the render thread

オープン
#22,716 コメント 6 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 9 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
22/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
csharp

調査の方向性

The race sits between Sheet.GetTexture() and Sheet.CommitBufferedData() in OpenRA.Game/Graphics/Sheet.cs, which read and write dirtyRegion. The off-thread writer is MissionBrowserLogic.SelectMap triggering glyph rasterisation through the shared font SheetBuilder created in Renderer.InitializeFonts(). Done means the campaign briefing list no longer throws when a non-ASCII font is used, and the fix is agreed on, since the issue weighs a minimal read-once change against serialising sheet access or moving the work to Game.RunAfterTick.

索引モデルが issue の本文から書いたものです。

説明

Engine: playtest-20261026 (x64, .NET 10.0.13), Windows 11 26220
Mod: ra, all Fonts: in mods/ra/mod.yaml pointed at Noto Sans SC, campaign briefings translated to Simplified Chinese

The crash is engine-side, but it needs a font whose glyphs are not all pre-cached to reproduce.

Stack trace
Exception of type `System.InvalidOperationException`: Nullable object must have a value.
   at System.Nullable`1.get_Value()
   at OpenRA.Graphics.Sheet.GetTexture()
   at OpenRA.Graphics.SpriteRenderer.Flush()
   at OpenRA.Renderer.EnableScissor(Rectangle rect)
   at OpenRA.Mods.Common.Widgets.ScrollPanelWidget.DrawOuter()
   at OpenRA.Widgets.Widget.DrawOuter()
   at OpenRA.Widgets.Ui.Draw()
   at OpenRA.Game.RenderTick()
   at OpenRA.Game.Loop()
   at OpenRA.Game.Run()
Reproduction
  1. Use a font with non-ASCII glyphs for all Fonts: entries (e.g. Noto Sans SC in mods/common/) and translate a few campaign briefings.
  2. Red Alert → Campaign → click missions in the list.
  3. Crash within seconds (reproduced twice in 3 minutes).

With the stock FreeSans.ttf the same screen never crashes: SpriteFont's constructor pre-caches 0x20..0x7E, so the campaign briefing needs no new glyphs.

Cause

Sheet.dirtyRegion (Rectangle?) is read and written from two threads without any synchronisation:

// OpenRA.Game/Graphics/Sheet.cs (playtest-20261026)
if (dirtyRegion != null && texture.Size == Size && Size != dirtyRegion?.Size)
{
    var region = dirtyRegion.Value;   // another thread cleared the nullable in between
  • Render thread: SpriteRenderer.Flush() → Sheet.GetTexture().
  • Background thread: MissionBrowserLogic.SelectMap() starts new Thread(...) that calls WidgetUtils.WrapText(...) and descriptionFont.Measure(...). Glyph rasterisation is lazy and writes into the shared font SheetBuilder (Renderer.InitializeFonts() creates a single builder for every font), which calls Sheet.CommitBufferedData() and, when a sheet fills up, ReleaseBufferAndTryTransferTo() → Sheet.GetTexture() off the render thread.

Those two methods are the only places that assign dirtyRegion = null, so one thread can clear it while the other is between the != null check and .Value.

Confirmed in the shipped assembly: Sheet.GetTexture() calls Nullable<Rectangle>::get_Value at IL body offset 0x00BA, and Sheet.CommitBufferedData() at 0x0035.

Two changes in this playtest made the race easy to hit:

  1. Sheet.dirtyRegion (partial upload) is new. playtest-20260222's Sheet.cs uploads the whole sheet with SetData and has no nullable, so this is a regression.
  2. ThreadedGraphicsContext makes texture.Size a blocking cross-thread round trip, widening the race window from nanoseconds to hundreds of microseconds.
Suggested fix

Minimum: read the field once.

var region = dirtyRegion;
if (region != null && texture.Size == Size && Size != region.Value.Size)
{
    texture.SetSubData(data, region.Value.X, region.Value.Y, region.Value.Width, region.Value.Height);

I verified locally that replacing both get_Value() calls with GetValueOrDefault() (identical signature, 2 bytes of IL) stops the crash; the glyph sheet then occasionally re-uploads a stale region, which is harmless.

Better: serialise Sheet access (lock or marshal commits to the render thread), and/or move the WrapText/Measure work in MissionBrowserLogic.SelectMap into Game.RunAfterTick so glyphs are only rasterised on the render thread.

Localisation contribution

I maintain a Simplified Chinese localisation for playtest-20261026 covering Red Alert, Tiberian Dawn and Dune 2000 (208 translated files, including 140 campaign briefings), released under GPL-3.0. The patch and full file manifest are available at Janlhin/OpenRA_Simplified_Chinese. I would be glad to contribute the translations upstream in whatever form the team prefers — per-mod Fluent .ftl pull requests, or a small first batch to validate the workflow — and to help with review and a shared glossary. Happy to split it into reviewable chunks and follow CONTRIBUTING.

主要言語
C#
スター
17.9k
フォーク
3.1k
平均マージ
9日 2時間
マージ済み PR(30日)
3

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

OpenRA/OpenRA のほかの issue

OpenRA/OpenRA の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。