Repository navigation
Fix asymmetric Observation scope propagation in Kotlin Coroutines - #36955
arnabnandy7 wants to merge 1 commit into
Conversation
Introduce a ThreadLocal nesting check inside PropagationContextElement to prevent duplicate/asymmetric thread-local scope updates during nested undispatched coroutine context changes. Closes spring-projects#36929 Signed-off-by: Arnab Nandy <arnab_nandy7@yahoo.com>
384f831 to
40ce895
Compare
|
@arnabnandy7 I would you like to get your personal opinion on the matter. Why do you consider this to be an issue in Spring Framework and suggest a fix here? In your experience, is this coroutines should behave with regards to the Your PR currently suggests to maintain a thread local value on the carrier thread on a per element basis. Given that Framework is using Thanks |
|
Thanks @bclozel, those are fair concerns. I focused on Spring because the report linked the regression to adding I took the reported two updates followed by one restore as a reason to suppress nested updates. That was too strong an assumption without reproducing the coroutine lifecycle. Nested updates can be legitimate, and I haven’t established why the corresponding restoration is missing or delayed. Your thread-pool concern also exposes a weakness in this approach. The counter is actually static, so it is shared across all element instances on a thread. A nested invocation carrying a different context would be skipped too. With The test only exercises a manually constructed update/restore sequence. I’d like to revisit this with an actual Flow subscription reproduction, including suspension and different nested contexts, before proposing a revised fix. I agree the current implementation needs more investigation. |
|
Closing this PR as we've confirmed the root cause is in I've submitted an upstream fix with reproduction tests here: Kotlin/kotlinx.coroutines#4756 (addressing Kotlin/kotlinx.coroutines#4754). cc @bclozel |
Summary
This PR addresses issue #36929 where Kotlin flow-to-flux subscriptions using
PropagationContextElementopen Observation scopes asymmetrically, causingAssertionErrorcrashes inObservationThreadLocalAccessor.restore.Cause of the Issue
During undispatched coroutine context modifications (e.g., from
ChannelFlowKt.withContextUndispatchednested inside the flow's execution path),updateThreadContextis called nestedly on the same thread. When a coroutine suspends within the nested block:finallyblock of the nested block (which callsrestoreThreadContextto close the nested scope) is NOT executed.restoreThreadContext.restoreThreadContextattempts to close the outer scope but finds a dirty thread-local observation stack (with the nested scope still on top), causing Micrometer's assertion to trip.Solution
We introduce a static
ThreadLocal<Integer> nestingDepthtracker insidePropagationContextElement:updateThreadContextis called, we incrementnestingDepth.Scopeimplementation, avoiding nested/asymmetric thread-local scope pushes.WrapperScopethat delegates to the original snapshot scope and resets thenestingDepthto 0 when closed.Verification
Added a unit test
nestedInvocationsinPropagationContextElementTestswhich simulates nested context updates and outer/nested restorations, verifying that no assertion errors are thrown and the Observation stack remains balanced. All tests inspring-corecompiled and passed.