Skip to content

Commit 63a49a4

Browse files
committed
Fix lua coroutine context leak
1 parent d212719 commit 63a49a4

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

_state.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,9 @@ func (ls *LState) isStarted() bool {
769769

770770
func (ls *LState) kill() {
771771
ls.Dead = true
772+
if ls.ctxCancelFn != nil {
773+
ls.ctxCancelFn()
774+
}
772775
}
773776

774777
func (ls *LState) indexToReg(idx int) int {
@@ -1402,6 +1405,7 @@ func (ls *LState) NewThread() (*LState, context.CancelFunc) {
14021405
if ls.ctx != nil {
14031406
thread.mainLoop = mainLoopWithContext
14041407
thread.ctx, f = context.WithCancel(ls.ctx)
1408+
thread.ctxCancelFn = f
14051409
}
14061410
return thread, f
14071411
}

state.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,9 @@ func (ls *LState) isStarted() bool {
815815

816816
func (ls *LState) kill() {
817817
ls.Dead = true
818+
if ls.ctxCancelFn != nil {
819+
ls.ctxCancelFn()
820+
}
818821
}
819822

820823
func (ls *LState) indexToReg(idx int) int {
@@ -1561,6 +1564,7 @@ func (ls *LState) NewThread() (*LState, context.CancelFunc) {
15611564
if ls.ctx != nil {
15621565
thread.mainLoop = mainLoopWithContext
15631566
thread.ctx, f = context.WithCancel(ls.ctx)
1567+
thread.ctxCancelFn = f
15641568
}
15651569
return thread, f
15661570
}

value.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ type LState struct {
218218
hasErrorFunc bool
219219
mainLoop func(*LState, *callFrame)
220220
ctx context.Context
221+
ctxCancelFn context.CancelFunc
221222
}
222223

223224
func (ls *LState) String() string { return fmt.Sprintf("thread: %p", ls) }

0 commit comments

Comments
 (0)