[JAVA-4788] Reactor Context loss in BatchCursorFlux Created: 21/Oct/22  Updated: 28/Oct/23  Resolved: 03/Nov/22

Status: Closed
Project: Java Driver
Component/s: Reactive Streams
Affects Version/s: None
Fix Version/s: 4.8.0

Type: Bug Priority: Major - P3
Reporter: Mark Paluch Assignee: Ross Lawley
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Summary

BatchCursorFlux uses a lambda to subscribe to batchCursorPublisher without propagating the subscriber context.

How to Reproduce

A tad difficult. 

 

MongoClientSettings settings = MongoClientSettings.builder()
		.contextProvider((ReactiveContextProvider) subscriber -> {
			if (subscriber instanceof CoreSubscriber<?> cs) {
 
				Map<Object, Object> map = cs.currentContext().stream()
						.collect(Collectors.toConcurrentMap(Entry::getKey, Entry::getValue));
				
				// map is empty
			}
			
			return
		}).build();
 
// later:
 
MongoCollection<Document> reproducer = operations.getCollection("foo").block();
Flux.from(reproducer.find()).contextWrite(Context.of("key", "value")).blockLast();

 

Additional Background

Discovered during context propagation testing for observability. Other places where lambda-based subscribers are used might be affected, too.



 Comments   
Comment by Githook User [ 03/Nov/22 ]

Author:

{'name': 'Ross Lawley', 'email': 'ross.lawley@gmail.com', 'username': 'rozza'}

Message: Ensure reactor context is passed to any child BatchCursor Mono/Flux

JAVA-4788
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/8e917658b22d037c84dedc3a38735e4569a55809

Comment by Ross Lawley [ 21/Oct/22 ]

Hi mpaluch@paluch.biz,

Interesting, the migration to using Project Reactor in the driver essentially treated Mono's as just Publishers, so unfortunately, there could be a number of places where the context is not passed. That said the driver generally doesn't subscribe / consume its own Publishers - batch cursors / cursors are definitely one place where there is the need for a async flatmap and custom subscription.

How to Reproduce
A tad difficult.

Yeah, agreed We'll have to think about how to add a regression test for this internally, thankfully your test provides a good insight.

Ross

Generated at Thu Feb 08 09:02:58 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.