ReapplyLocalizationsToAllComponents aborts (and keeps aborting) on a disposed registered control
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 78/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- csharp
調査の方向性
XliffLocalizationManagerWinforms.cs の ReapplyLocalizationsToAllComponents から始め、登録されたコンポーネントがどのように保存され、ローカライズされるかを確認してください。issue には XliffLocalizationManagerTests.cs の再現手順が記載されています。ディスプレイのある Windows で実行し、破棄されたコントロールによって処理が中断されなくなり、その後にある有効なラベルがローカライズされることを確認してください。回帰テストが成功すれば変更は完了です。
索引モデルが issue の本文から書いたものです。
説明
Summary
ReapplyLocalizationsToAllComponents() (invoked by LocalizationManagerWinforms.SetUILanguage(langId, true), which every WinForms app calls on every UI language switch) walks ComponentCache with no check for whether a registered control has already been disposed, and the foreach has no per-item try/catch:
// XliffLocalizationManagerWinforms.cs
public void ReapplyLocalizationsToAllComponents()
{
foreach (var component in ComponentCache.Keys)
ApplyLocalization(component);
}
ComponentCache itself never has entries removed — a control registered once via an L10NSharpExtender's SetLocalizingId (i.e. nearly every control in every dialog a typical app ever shows) stays in the cache for the life of the process, even long after its owning form has been closed and disposed.
Real-world crash
Hit this in SayMore (reported by a user switching from English to Tibetan mid-session, after having opened and closed an unrelated short-lived dialog earlier):
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'AutoHeightLabel'.
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.CreateGraphicsInternal()
at System.Windows.Forms.Control.CreateGraphics()
at SIL.Windows.Forms.Widgets.AutoHeightLabel.AdjustHeight()
at L10NSharp.Windows.Forms.XLiffUtils.XliffLocalizationManagerWinforms.ApplyLocalizationsToControl(Control ctrl, String id)
at L10NSharp.Windows.Forms.XLiffUtils.XliffLocalizationManagerWinforms.ApplyLocalization(IComponent component)
at L10NSharp.Windows.Forms.XLiffUtils.XliffLocalizationManagerWinforms.ReapplyLocalizationsToAllComponents()
at L10NSharp.Windows.Forms.LocalizationManagerInternalWinforms`1.ReapplyLocalizationsToAllObjectsInAllManagers()
at L10NSharp.Windows.Forms.LocalizationManagerWinforms.SetUILanguage(String langId, Boolean reapplyLocalizationsToAllObjectsInAllManagers)
AutoHeightLabel (from SIL.Windows.Forms) reacts to a text/font change by calling CreateGraphics() to remeasure itself, which throws once the control is disposed — but this is not really an AutoHeightLabel-specific problem: it is just the first control type anyone noticed hitting it, because it happens to touch its own handle on a text change. Any control that does the same is exposed the same way.
Severity beyond the crash itself
Because the foreach has no per-item recovery, the exception does not just affect the one stale control — it aborts the whole reapply pass partway through, silently leaving every component that happens to be registered after the disposed one in ComponentCache's enumeration order un-relocalized for that language switch. And because the stale entry is never removed, every subsequent language switch for the rest of the process hits the same wall at the same point, so this is not a one-time blip — once a single short-lived dialog has been shown and closed, language switching is effectively broken for the rest of that run.
Proposed minimal test
This reproduces the failure directly against XliffLocalizationManagerWinforms, without needing SIL.Windows.Forms — a plain Control subclass that touches its handle on OnTextChanged stands in for AutoHeightLabel. Verified failing against current master (pre-fix); confirmed on both net48 and net8.0-windows.
/// ------------------------------------------------------------------------------------
/// <summary>
/// Reproduces a crash seen in a real application (SayMore): ReapplyLocalizationsToAllComponents
/// -- called from SetUILanguage(..., true), i.e. every time the user switches UI language --
/// walks ComponentCache with no check for whether a registered control has already been
/// disposed, and the foreach has no per-item try/catch. A disposed control that reacts to
/// its own OnTextChanged by touching its handle -- as SIL.Windows.Forms' AutoHeightLabel does,
/// calling CreateGraphics() to remeasure itself -- throws ObjectDisposedException the moment
/// ApplyLocalizationsToControl sets its Text, and nothing catches it: the one disposed control
/// aborts the whole pass, silently leaving every component registered after it in the cache
/// (here, a perfectly live Label) un-relocalized for this and every later language switch,
/// since the disposed entry is never removed from ComponentCache.
/// </summary>
/// ------------------------------------------------------------------------------------
[Test]
[Category("RequiresDisplay")]
public void ReapplyLocalizationsToAllComponents_OneRegisteredControlIsDisposed_ThrowsAndLeavesLaterComponentsUnlocalized()
{
LocalizationManagerWinforms.SetUILanguage("en", true);
LocalizationManagerInternalWinforms<XLiffDocument>.LoadedManagers.Clear();
using (var folder = new L10NSharp.Tests.TempFolder())
{
var installedFolder = Path.Combine(folder.Path, "installed");
// ReSharper disable once AssignNullToNotNullAttribute
var userRelativeFolder = Path.Combine("Temp", Path.GetFileName(Path.GetDirectoryName(folder.Path)),
Path.GetFileName(folder.Path), "user");
// Give each control's id a real, retrievable English string up front (unlike
// RegisterComponentForLocalizing's defaultText parameter, which XliffTransUnitUpdater.Update
// silently ignores whenever LangId isn't set -- not the bug this test is about, so
// side-stepping it here by seeding the string cache the same way the other tests in
// this file do for their own ids).
var englishDoc = CreateNewDocument(AppVersion, "en");
englishDoc.AddTransUnit(CreateTransUnit("disposed.id", false,
CreateTransUnitVariant("en", "original disposed text")));
englishDoc.AddTransUnit(CreateTransUnit("live.id", false,
CreateTransUnitVariant("en", "live label text")));
englishDoc.Save(Path.Combine(installedFolder,
LocalizationManager.GetTranslationFileNameForLanguage(AppId, "en")));
var manager = (XliffLocalizationManagerWinforms)LocalizationManagerWinforms.Create("en", AppId,
AppName, AppVersion, installedFolder, userRelativeFolder, null, new string[] { });
// Registration happens the way it does in a real app -- after Create(), as each
// dialog/control is constructed over the app's lifetime -- not before it.
var disposedControl = new ControlThatTouchesItsHandleOnTextChanged();
manager.RegisterComponentForLocalizing(disposedControl, "disposed.id",
"original disposed text", null, null, null);
disposedControl.Dispose();
// Registered AFTER the disposed control, so it sits later in ComponentCache's
// enumeration order and would be the casualty of an aborted reapply pass.
var liveLabel = new Label();
manager.RegisterComponentForLocalizing(liveLabel, "live.id", "live label text", null, null, null);
Assert.That(() => manager.ReapplyLocalizationsToAllComponents(),
Throws.InstanceOf<ObjectDisposedException>(),
"ReapplyLocalizationsToAllComponents should not let one disposed control's " +
"ObjectDisposedException escape unhandled.");
// The real-world consequence of that unhandled exception: the foreach loop
// aborted at disposedControl and never reached liveLabel. liveLabel is otherwise
// perfectly healthy, but it was never relocalized to its registered text.
Assert.That(liveLabel.Text, Is.EqualTo(string.Empty),
"liveLabel was registered after the disposed control, so the aborted reapply " +
"loop never reached it and it was never relocalized to 'live label text'.");
}
}
/// <summary>
/// Mirrors SIL.Windows.Forms.Widgets.AutoHeightLabel, which reacts to a Text/Font change
/// by calling CreateGraphics() to remeasure itself. CreateGraphics() throws
/// ObjectDisposedException once the control has been disposed.
/// </summary>
private class ControlThatTouchesItsHandleOnTextChanged : Control
{
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
using (CreateGraphics()) { }
}
}
(Drops into XliffLocalizationManagerTests.cs, alongside the existing tests there; needs using System.Windows.Forms; added to that file's usings.)
Possible directions for an actual fix (not proposed here, just flagging)
ReapplyLocalizationsToAllComponents/ApplyLocalizationcould skip (ortry/catchand continue past) a component whoseControlIsDisposed.ComponentCachecould have entries removed proactively — e.g. a control'sDisposedevent could unregister it — rather than only ever growing for the life of the process.
No public API currently exists for a consuming application to remove a disposed control from a LocalizationManager's cache itself, so a host app's only options right now are to avoid ever disposing a registered control (impractical) or to reflect into ComponentCache via the internal ILocalizationManagerInternal interface (fragile — which is what we ended up doing as a stopgap in SayMore).
- 主要言語
- C#
- スター
- 3
- フォーク
- 10
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
sillsdev/l10nsharp のほかの issue
-
api change bug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 72/100
-
Update .NET target from net8.0 to net10.0再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープンenhancement
難易度 4/5 3〜5日 初心者へのやさしさ 52/100
-
enhancement
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
-
bug
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
sillsdev/l10nsharp の issue をすべて見る
似ている issue
-
quality 提案 要トリアージ
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
メンテナーはふだん 1 日以内に返信
-
The Language and Region lists in Options read to a screen reader as "LanguageInfo { Id = ar, ... }"オープンbug localization small ui
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
hexide-io/HexIDE#4176 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
MarcelRoozekrans/Rag.NET#1030 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
AvaloniaUI/Avalonia#22841 ·
メンテナーはふだん 1 日以内に返信
-
a11y tech-debt
難易度 1/5 1時間未満 初心者へのやさしさ 78/100
philipphoeninger/procure-access#350 ·
メンテナーはふだん 1 日以内に返信