@@ -326,13 +326,15 @@ func (c *MongoConnector) PullRecords(
326326 if err := recreateChangeStream (false ); err != nil {
327327 return fmt .Errorf ("failed to recreate change stream: %w" , err )
328328 }
329+ c .logger .Info ("[mongo] recreated change stream because context deadline exceeded" , slog .Duration ("elapsed" , time .Since (pullStart )))
329330 continue
330331 }
331332
332333 if isResumeTokenNotFoundError (err ) {
333334 if err := recreateChangeStream (true ); err != nil {
334335 return fmt .Errorf ("failed to recreate change stream: %w" , err )
335336 }
337+ c .logger .Info ("[mongo] recreated change stream because resume token not found" , slog .Duration ("elapsed" , time .Since (pullStart )))
336338 continue
337339 }
338340
@@ -462,14 +464,19 @@ func createPipeline(tableNameMapping map[string]model.NameAndExclude) (mongo.Pip
462464 return pipeline , nil
463465}
464466
465- // createChangeStream calls client.Watch with a 5 minute context deadline
466- // so the driver sends it as maxTimeMS over the wire. Otherwise server-
467- // side default maxTimeMS is used which can sometimes be too short.
468- func createChangeStream (client * mongo.Client , parent context.Context , pipeline mongo.Pipeline , opts * options.ChangeStreamOptionsBuilder ,
467+ // createChangeStream calls client.Watch with a context deadline so the driver
468+ // sends it as maxTimeMS over the wire. Without this, the server-side default
469+ // maxTimeMS may be too short for the initial aggregate.
470+ func createChangeStream (
471+ client * mongo.Client , parent context.Context , pipeline mongo.Pipeline , opts * options.ChangeStreamOptionsBuilder ,
469472) (* mongo.ChangeStream , error ) {
470473 watchCtx , cancel := context .WithTimeout (parent , 5 * time .Minute )
471474 defer cancel ()
472- return client .Watch (watchCtx , pipeline , opts )
475+ cs , err := client .Watch (watchCtx , pipeline , opts )
476+ if err != nil {
477+ return nil , fmt .Errorf ("failed to open change stream: %w" , err )
478+ }
479+ return cs , nil
473480}
474481
475482// This can happen if the resumeToken we are attempting to `ResumeAfter` refers to a table that has been
0 commit comments