Skip to content

Commit 5092263

Browse files
committed
check and suggest is not thread safe in a broker
1 parent 17b6a93 commit 5092263

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

enchant.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ static char* getString(char **c, int i) {
1313
import "C"
1414
import (
1515
"fmt"
16+
"sync"
1617
"unsafe"
1718
)
1819

1920
// Enchant is the type that contains the package internals.
2021
type Enchant struct {
22+
sync.Mutex
2123
broker *C.EnchantBroker
2224
dict *C.EnchantDict
2325
}
@@ -97,7 +99,9 @@ func (e *Enchant) DictCheck(word string) (found bool, err error) {
9799
size := uintptr(len(word))
98100
s := (*C.ssize_t)(unsafe.Pointer(&size))
99101

102+
e.Lock()
100103
status := C.enchant_dict_check(e.dict, cWord, *s)
104+
e.Unlock()
101105

102106
if status < 0 {
103107
return false, fmt.Errorf("could not check word: %s", word)
@@ -130,6 +134,9 @@ func (e *Enchant) DictSuggest(word string) (suggestions []string, err error) {
130134
var suggs uintptr
131135
ns := (*C.size_t)(unsafe.Pointer(&suggs))
132136

137+
e.Lock()
138+
defer e.Unlock()
139+
133140
response := C.enchant_dict_suggest(e.dict, cWord, *s, ns)
134141
if response == nil {
135142
return nil, nil

0 commit comments

Comments
 (0)