Skip to content

Commit 13f78ee

Browse files
authored
Merge pull request #1732 from anyproto/go-4344-research-tantivy-crashes-on-macos
GO-4344 perform tantivy write operations under the lock
2 parents ffeaba5 + 3dd6011 commit 13f78ee

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

pkg/lib/localstore/ftsearch/ftsearchtantivy.go

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"os"
2020
"path/filepath"
2121
"strings"
22+
"sync"
2223
"time"
2324

2425
"github.com/anyproto/any-sync/app"
@@ -49,12 +50,15 @@ type ftSearchTantivy struct {
4950
index *tantivy.TantivyContext
5051
schema *tantivy.Schema
5152
parserPool *fastjson.ParserPool
53+
mu sync.Mutex
5254
}
5355

5456
func (f *ftSearchTantivy) BatchDeleteObjects(ids []string) error {
5557
if len(ids) == 0 {
5658
return nil
5759
}
60+
f.mu.Lock()
61+
defer f.mu.Unlock()
5862
start := time.Now()
5963
defer func() {
6064
spentMs := time.Since(start).Milliseconds()
@@ -74,6 +78,8 @@ func (f *ftSearchTantivy) BatchDeleteObjects(ids []string) error {
7478
}
7579

7680
func (f *ftSearchTantivy) DeleteObject(objectId string) error {
81+
f.mu.Lock()
82+
defer f.mu.Unlock()
7783
return f.index.DeleteDocuments(fieldIdRaw, objectId)
7884
}
7985

@@ -206,6 +212,8 @@ func (f *ftSearchTantivy) tryToBuildSchema(schema *tantivy.Schema) (*tantivy.Tan
206212
}
207213

208214
func (f *ftSearchTantivy) Index(doc SearchDoc) error {
215+
f.mu.Lock()
216+
defer f.mu.Unlock()
209217
metrics.ObjectFTUpdatedCounter.Inc()
210218
tantivyDoc, err := f.convertDoc(doc)
211219
if err != nil {
@@ -256,7 +264,9 @@ func (f *ftSearchTantivy) BatchIndex(ctx context.Context, docs []SearchDoc, dele
256264
l.Debugf("ft index done")
257265
}
258266
}()
267+
f.mu.Lock()
259268
err = f.index.DeleteDocuments(fieldIdRaw, deletedDocs...)
269+
f.mu.Unlock()
260270
if err != nil {
261271
return err
262272
}
@@ -268,6 +278,8 @@ func (f *ftSearchTantivy) BatchIndex(ctx context.Context, docs []SearchDoc, dele
268278
}
269279
tantivyDocs = append(tantivyDocs, tantivyDoc)
270280
}
281+
f.mu.Lock()
282+
defer f.mu.Unlock()
271283
return f.index.AddAndConsumeDocuments(tantivyDocs...)
272284
}
273285

0 commit comments

Comments
 (0)