Skip to content

Commit 0e31b76

Browse files
committed
fix: deflake syncapi test race between sendManfiest and oplog subscription
1 parent fa379bb commit 0e31b76

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

internal/api/syncapi/syncclient.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ type syncSessionHandlerClient struct {
227227
canForwardPlansSet map[string]struct{}
228228

229229
oplogSubscription *oplog.Subscription // set while subscribed; unsubscribed in OnConnectionDisconnected.
230+
231+
// manifestMu prevents sendManifest flow from racing with operations forwarded from the server, sendManifest deletes missing operations so we'd miss any operations created while a flow is in progress.
232+
manifestMu sync.Mutex
230233
}
231234

232235
func newSyncHandlerClient(
@@ -295,6 +298,9 @@ func (c *syncSessionHandlerClient) canForwardMeta(meta oplog.OpMetadata) bool {
295298
}
296299

297300
func (c *syncSessionHandlerClient) sendManifest(stream *bidiSyncCommandStream) (int, error) {
301+
c.manifestMu.Lock()
302+
defer c.manifestMu.Unlock()
303+
298304
var opIDs, modnos []int64
299305
if err := c.oplog.QueryMetadata(oplog.Query{}, func(meta oplog.OpMetadata) error {
300306
if c.canForwardMeta(meta) {
@@ -420,6 +426,10 @@ func (c *syncSessionHandlerClient) OnConnectionEstablished(ctx context.Context,
420426
return
421427
}
422428

429+
// Hold manifestMu so this event cannot slot in between a concurrent
430+
// sendManifest's oplog query and the manifest's own Send; see manifestMu.
431+
c.manifestMu.Lock()
432+
defer c.manifestMu.Unlock()
423433
stream.Send(&v1sync.SyncStreamItem{
424434
Action: &v1sync.SyncStreamItem_ReceiveOperations{
425435
ReceiveOperations: &v1sync.SyncStreamItem_SyncActionReceiveOperations{

0 commit comments

Comments
 (0)