Fix race condition when adding debug data to connector context#5767
Fix race condition when adding debug data to connector context#5767pubmodmatt merged 3 commits intonextfrom
Conversation
|
CI performance tests
|
| context.extensions().with_lock(|mut lock| { | ||
| if let Some(ref mut debug) = lock.get_mut::<ConnectorContext>() { | ||
| debug.push_invalid_response(&parts, body); | ||
| } | ||
| }); |
There was a problem hiding this comment.
I worry about lock contention—especially since it could slow down all requests even if debug is disabled. Can we get router team eyes on this for a suggestion? Maybe we could make extensions RwLock, or maybe we should be storing an Option<Mutex<ConnectorContext>> and getting a reference to that (so we can still check for debug disabled).
There was a problem hiding this comment.
Sure, I can reach out for suggestions. For your second proposal, storing Option<Mutex<ConnectorContext>> would still require locking the ExtensionsGuard to get that Option, so there's still potential contention there, or am I misunderstanding?
There was a problem hiding this comment.
Actually, I think I see - you mean we could get that Option once and then if Some, lock the second Mutex inside the Option. So we'd only lock the extensions guard once instead of several times per request.
There was a problem hiding this comment.
And because the Option is on the outside of the second mutex, we wouldn't need to lock it at all if debugging is disabled
There was a problem hiding this comment.
I like this option the best and coded it up.
There was a race condition due to the way debug data was being added to the
ConnectorsContext. The connectors context was removed from the request context extensions for each query, updated, then put back. If the query planner executed multiple queries in parallel, they would all try to do this at once. Once one of them successfully removed the connectors context, any others running concurrently would get backNone.The connectors context is now left in place rather than removed, and is accessed through
Option<Arc<Mutex<ConnectorsContext>>>. This preserves a single lookup in theExtensionsfor each fetch. If the debug feature is not enabled, the option will beNone. If it is enabled, the mutex is locked and the context is updated with the debug information.Checklist
Complete the checklist (and note appropriate exceptions) before the PR is marked ready-for-review.
Exceptions
Notes
Footnotes
It may be appropriate to bring upcoming changes to the attention of other (impacted) groups. Please endeavour to do this before seeking PR approval. The mechanism for doing this will vary considerably, so use your judgement as to how and when to do this. ↩
Configuration is an important part of many changes. Where applicable please try to document configuration examples. ↩
Tick whichever testing boxes are applicable. If you are adding Manual Tests, please document the manual testing (extensively) in the Exceptions. ↩