Skip to content

GH-1517: Isolate around-wrapper reentrancy state per thread - #1521

Closed
mohit32886 wants to merge 1 commit into
spring-cloud:mainfrom
mohit32886:gh-1517-thread-safe-wrapped-flag
Closed

mohit32886 wants to merge 1 commit into
spring-cloud:mainfrom
mohit32886:gh-1517-thread-safe-wrapped-flag

Conversation

@mohit32886

Copy link
Copy Markdown
Contributor

Fixes #1517

Summary

  • Replaced the shared non-volatile boolean wrapped field on SimpleFunctionRegistry.FunctionInvocationWrapper with a per-thread ThreadLocal<Boolean> (wrapped) that is set before invoking FunctionAroundWrapper.apply(...) and removed in a finally block.
  • Removed the redundant targetFunction.wrapped = false reset in FunctionAroundWrapper.apply(...), as FunctionInvocationWrapper.apply(...) owns and cleans up the reentrancy guard around the entire wrapper invocation.

Problem

SimpleFunctionRegistry caches FunctionInvocationWrapper instances in wrappedFunctionDefinitions and shares them across threads. Previously, toggling a single non-volatile wrapped boolean field on the shared wrapper instance created a data race and caused concurrent invocations on other threads to observe wrapped == true, bypassing FunctionAroundWrapper (including ObservationFunctionAroundWrapper).

Verification

  • Added concurrency regression tests in SimpleFunctionRegistryTests:
    • testAroundWrapperAppliedToConcurrentInvocations (deterministic latch test verifying concurrent invocations do not skip FunctionAroundWrapper, and subsequent invocations continue to be wrapped)
    • testObservationAroundWrapperAppliedUnderConcurrentLoad (200 concurrent invocations across 8 threads verifying every invocation records a spring.cloud.function observation via ObservationFunctionAroundWrapper)
  • Ran ./mvnw -pl spring-cloud-function-core,spring-cloud-function-context,spring-cloud-function-web test (including checkstyle:check):
    • spring-cloud-function-core: 3 passed, 0 failed
    • spring-cloud-function-context: 241 run, 0 failed, 13 skipped (pre-existing)
    • spring-cloud-function-web: 199 run, 0 failed, 25 skipped (pre-existing)

Replace the shared non-volatile boolean wrapped flag on FunctionInvocationWrapper with a per-thread ThreadLocal<Boolean> cleaned up in finally, and remove the redundant flag reset from FunctionAroundWrapper.

Previously, concurrent invocations of a cached FunctionInvocationWrapper shared a single wrapped boolean, allowing concurrent calls to skip FunctionAroundWrapper (and observability hooks) or observe stale state.

Fixes spring-cloud#1517

Signed-off-by: Mohit Gupta <mohit32886@gmail.com>
@olegz olegz added this to the 5.1.1 milestone Nov 21, 2026
@olegz olegz closed this in fcf86dc Nov 21, 2026
olegz pushed a commit that referenced this pull request Nov 21, 2026
Replace the shared non-volatile boolean wrapped flag on FunctionInvocationWrapper with a per-thread ThreadLocal<Boolean> cleaned up in finally, and remove the redundant flag reset from FunctionAroundWrapper.

Previously, concurrent invocations of a cached FunctionInvocationWrapper shared a single wrapped boolean, allowing concurrent calls to skip FunctionAroundWrapper (and observability hooks) or observe stale state.

Resolves #1517
Resolves #1521

Signed-off-by: Mohit Gupta <mohit32886@gmail.com>
Signed-off-by: Oleg Zhurakousky <oleg.zhurakousky@broadcom.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The non-volatile wrapped flag is toggled in apply() on wrappers that are cached and shared between threads.

2 participants