fix: 闪退真凶——装载历史缓存后 transInflight 为 nil
crash.log 实锤:panic: assignment to entry in nil map, docTranslation(hover 渲染帧内)。 合并判空是错的:loadTranslations 只装 a.trans,缓存有数据后 a.trans 永远非 nil,跳过了 transInflight 初始化——hover 一个 未缓存符号就往 nil map 写,主线程渲染帧直接炸。两次崩溃同因。 修复:两个 map 分别判空;loadTranslations 同时初始化两个。 回归测试:TestDocTranslationNilInflightWithLoadedCache。
This commit is contained in:
1 parent
665efb6027
commit
e7de0bd93a
2 files changed
+28
No files matched your search
@@ -72,3 +72,25 @@ func TestHoverTranslationInProgress(t *testing.T) {
|
|||||||
t.Fatalf("翻译中应显示原文, got %q", got)
|
t.Fatalf("翻译中应显示原文, got %q", got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestDocTranslationNilInflightWithLoadedCache 复现闪退:启动时装载了历史
|
||||||
|
// 缓存(a.trans 非 nil)但 transInflight 仍为 nil,此时 hover 一个未缓存的
|
||||||
|
// 文档——不得往 nil map 写(crash.log: assignment to entry in nil map)。
|
||||||
|
func TestDocTranslationNilInflightWithLoadedCache(t *testing.T) {
|
||||||
|
a := &App{
|
||||||
|
trans: map[string]string{"cached": "已缓存译文"}, // 只有 trans 被装载
|
||||||
|
set: settings{TranslateDocs: true, AIBaseURL: "x", AIAPIKey: "y", AIModel: "z"},
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
t.Fatalf("不得 panic: %v", r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
got := a.docTranslation("some uncached doc text")
|
||||||
|
if got != "some uncached doc text" {
|
||||||
|
t.Fatalf("未缓存应返回原文, got %q", got)
|
||||||
|
}
|
||||||
|
if a.transInflight == nil {
|
||||||
|
t.Fatal("transInflight 应被初始化")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -36,8 +36,13 @@ func (a *App) docTranslation(text string) string {
|
|||||||
if a.transInflight[key] {
|
if a.transInflight[key] {
|
||||||
return text
|
return text
|
||||||
}
|
}
|
||||||
|
// 两个 map 分别判空:loadTranslations 只装 a.trans(有历史缓存时它
|
||||||
|
// 非 nil),transInflight 仍是 nil——之前合并判空导致往 nil map 写,
|
||||||
|
// 渲染帧里直接 panic 闪退(crash.log 实锤)。
|
||||||
if a.trans == nil {
|
if a.trans == nil {
|
||||||
a.trans = map[string]string{}
|
a.trans = map[string]string{}
|
||||||
|
}
|
||||||
|
if a.transInflight == nil {
|
||||||
a.transInflight = map[string]bool{}
|
a.transInflight = map[string]bool{}
|
||||||
}
|
}
|
||||||
a.transInflight[key] = true
|
a.transInflight[key] = true
|
||||||
@@ -98,6 +103,7 @@ func (a *App) loadTranslations() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
a.trans = map[string]string{}
|
a.trans = map[string]string{}
|
||||||
|
a.transInflight = map[string]bool{}
|
||||||
for k, v := range m {
|
for k, v := range m {
|
||||||
if k != "" && v != "" {
|
if k != "" && v != "" {
|
||||||
// 旧版缓存 key 是原文,统一规范化,能被预热与懒翻译共同命中。
|
// 旧版缓存 key 是原文,统一规范化,能被预热与懒翻译共同命中。
|
||||||
|
|||||||
Reference in new issue
Block a user