Skip to content

fix: correct LRU cache list operation from PushFront to MoveToFront#1808

Merged
MarksonHon merged 1 commit intov2rayA:mainfrom
782042369:fix/lru-interface-conversion-error
Jan 25, 2026
Merged

fix: correct LRU cache list operation from PushFront to MoveToFront#1808
MarksonHon merged 1 commit intov2rayA:mainfrom
782042369:fix/lru-interface-conversion-error

Conversation

@782042369
Copy link
Copy Markdown
Contributor

@782042369 782042369 commented Jan 22, 2026

🐛 Bug Description

Type conversion error occurred when deploying v2rayA with Docker:

interface conversion: interface {} is *list.Element, not *lru.EncapsulatedValue

Environment

  • Deployment Method: Docker
  • Docker Version: Server 24.0.2 / Client 1.43
  • System: DiskStation 4.4.302+ (x86_64)
  • Hardware: 4 cores / 15.61GB RAM

Stack Trace

v2raya  | interface conversion: interface {} is *list.Element, not *lru.EncapsulatedValue
v2raya  | runtime/iface.go:275 (0x419de4)
v2raya  | github.com/v2rayA/v2rayA/infra/dataStructure/lru/lru.go:86 (0x9c92f3)
v2raya  | github.com/v2rayA/v2rayA/infra/dataStructure/lru/lru.go:71 (0x9c8b06)
v2raya  | github.com/v2rayA/v2rayA/common/response.go:34 (0x9cc1b3)
v2raya  | github.com/v2rayA/v2rayA/common/response.go:70 (0x9cc0af)
v2raya  | github.com/v2rayA/v2rayA/common/response.go:77 (0xfd92c8)
v2raya  | github.com/v2rayA/v2rayA/server/controller/version.go:19 (0xfd8f58)
v2raya  | github.com/gin-gonic/gin@v1.9.1/context.go:174 (0xfc76b9)
v2raya  | github.com/v2rayA/v2rayA/pkg/server/reqCache/reqCache.go:49 (0xfc75ff)
v2raya  | github.com/gin-gonic/gin@v1.9.1/context.go:174 (0x97988e)
v2raya  | github.com/gin-gonic/gin@v1.9.1/recovery.go:102 (0x97987b)
v2raya  | github.com/gin-gonic/gin@v1.9.1/context.go:174 (0x9786cd)
v2raya  | github.com/gin-gonic/gin@v1.9.1/gin.go:620 (0x978367)
v2raya  | github.com/gin-gonic/gin@v1.9.1/gin.go:576 (0x977eac)
v2raya  | net/http/server.go:3340 (0x730d0d)
v2raya  | net/http/server.go:2109 (0x70f5c4)
v2raya  | runtime/asm_amd64.s:1693 (0x4845a0)

🔍 Root Cause

In the get() function at line 62 of service/infra/dataStructure/lru/lru.go, the wrong list operation method was used:

Problematic code:

l.list.PushFront(v)  // v is already a *list.Element

This causes:

  1. The list.Element is duplicated in the list, corrupting the list structure
  2. When cache eviction occurs, back.Value may point to *list.Element instead of the expected *EncapsulatedValue
  3. Type assertion back.Value.(*EncapsulatedValue) fails

✅ Fix

Changed PushFront(v) to MoveToFront(v):

l.list.MoveToFront(v)  // Move existing element to front

Key difference:

  • PushFront(v): Adds v as a value to the list, creating a new element
  • MoveToFront(v): Moves v (which must be an existing element) to the front of the list, without creating a new element

📝 Testing

  • ✅ Code compiles successfully
  • ✅ Type conversion logic is now correct
  • ✅ Follows proper LRU cache implementation pattern

🔧 Impact

  • Modified file: service/infra/dataStructure/lru/lru.go
  • Lines changed: 1 line (line 62)
  • Affected features: All features using LRU cache (e.g., response cache)

问题描述:
- lru.go 的 get() 函数使用了 PushFront(v) 而不是 MoveToFront(v)
- 这导致 list.Element 被重复添加到列表中,破坏了列表结构
- 当缓存淘汰时,back.Value 可能指向 *list.Element 而不是 *EncapsulatedValue
- 类型断言失败:interface{} is *list.Element, not *lru.EncapsulatedValue

修复方法:
- 将 l.list.PushFront(v) 改为 l.list.MoveToFront(v)
- MoveToFront 会将现有 element 移到列表前面,而不是创建新的 element

Fixes: interface conversion error in LRU cache
@782042369 782042369 changed the title fix: 修复 LRU 缓存中 PushFront 导致的类型转换错误 fix: correct LRU cache list operation from PushFront to MoveToFront Jan 22, 2026
@MarksonHon MarksonHon merged commit 3d1fc77 into v2rayA:main Jan 25, 2026
4 checks passed
YLinXin pushed a commit to YLinXin/v2rayA that referenced this pull request Apr 5, 2026
…rsion-error

fix: correct LRU cache list operation from PushFront to MoveToFront
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants