From e38794ed88199aaa1c95144d0f2886fe186440ab Mon Sep 17 00:00:00 2001 From: Meo597 <197331664+Meo597@users.noreply.github.com> Date: Sun, 4 Oct 2026 01:36:48 +0800 Subject: [PATCH] lua: reset pooled state stack to prevent memory leaks --- common/lua/pool.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/common/lua/pool.go b/common/lua/pool.go index 41ab213ab..974850c7c 100644 --- a/common/lua/pool.go +++ b/common/lua/pool.go @@ -18,6 +18,7 @@ type Pool struct { factory LStateFactory idle []*glua.LState + top int mu sync.Mutex active sync.WaitGroup @@ -34,7 +35,7 @@ func NewPool(ctx context.Context, factory LStateFactory) (*Pool, error) { return nil, err } - return &Pool{ctx: poolCtx, cancel: cancel, factory: factory, idle: []*glua.LState{state}}, nil + return &Pool{ctx: poolCtx, cancel: cancel, factory: factory, idle: []*glua.LState{state}, top: state.GetTop()}, nil } // Context is cancelled by Close. Query contexts should derive from it. @@ -92,6 +93,7 @@ func (p *Pool) WithState(work func(*glua.LState) error) error { // Release returns a healthy state to the pool and closes a failed or cancelled one. func (p *Pool) Release(state *glua.LState, reusable bool) { if reusable { + state.SetTop(p.top) p.mu.Lock() if !p.closed && p.ctx.Err() == nil && len(p.idle) < maxIdleStates { p.idle = append(p.idle, state)