Skip to content

Commit d70801a

Browse files
authored
Merge pull request yuin#295 from johejo/fix_race_condition
fix race condition
2 parents 5aeaeab + 834e27b commit d70801a

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

_state.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package lua
33
import (
44
"context"
55
"fmt"
6-
"github.com/yuin/gopher-lua/parse"
76
"io"
87
"math"
98
"os"
@@ -12,6 +11,8 @@ import (
1211
"sync"
1312
"sync/atomic"
1413
"time"
14+
15+
"github.com/yuin/gopher-lua/parse"
1516
)
1617

1718
const MultRet = -1
@@ -1212,6 +1213,10 @@ func NewState(opts ...Options) *LState {
12121213
return ls
12131214
}
12141215

1216+
func (ls *LState) IsClosed() bool {
1217+
return ls.stack == nil
1218+
}
1219+
12151220
func (ls *LState) Close() {
12161221
atomic.AddInt32(&ls.stop, 1)
12171222
for _, file := range ls.G.tempFiles {
@@ -2016,7 +2021,7 @@ func (ls *LState) SetMx(mx int) {
20162021
go func() {
20172022
limit := uint64(mx * 1024 * 1024) //MB
20182023
var s runtime.MemStats
2019-
for ls.stop == 0 {
2024+
for atomic.LoadInt32(&ls.stop) == 0 {
20202025
runtime.ReadMemStats(&s)
20212026
if s.Alloc >= limit {
20222027
fmt.Println("out of memory")

state.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package lua
77
import (
88
"context"
99
"fmt"
10-
"github.com/yuin/gopher-lua/parse"
1110
"io"
1211
"math"
1312
"os"
@@ -16,6 +15,8 @@ import (
1615
"sync"
1716
"sync/atomic"
1817
"time"
18+
19+
"github.com/yuin/gopher-lua/parse"
1920
)
2021

2122
const MultRet = -1
@@ -2175,7 +2176,7 @@ func (ls *LState) SetMx(mx int) {
21752176
go func() {
21762177
limit := uint64(mx * 1024 * 1024) //MB
21772178
var s runtime.MemStats
2178-
for ls.stop == 0 {
2179+
for atomic.LoadInt32(&ls.stop) == 0 {
21792180
runtime.ReadMemStats(&s)
21802181
if s.Alloc >= limit {
21812182
fmt.Println("out of memory")

0 commit comments

Comments
 (0)