Skip to content

Backport: make BindingServiceProperties.bindings thread-safe and case-insensitive (5.0.x) - #3339

Open
AkashB23 wants to merge 2 commits into
spring-cloud:5.0.xfrom
AkashB23:gh-3321-bindings-thread-safe-5.0.x
Open

AkashB23 wants to merge 2 commits into
spring-cloud:5.0.xfrom
AkashB23:gh-3321-bindings-thread-safe-5.0.x

Conversation

@AkashB23

Copy link
Copy Markdown
Contributor

Backport of #3322 (4f9b513) and its follow-up (6412b30) to 5.0.x. Both cherry-picked cleanly (-x, so each message names its source commit).

5.0.x still has bindings as a plain TreeMap(String.CASE_INSENSITIVE_ORDER) (GH-3158), which is not thread-safe. Concurrent first use of binding names, for example StreamBridge.send() from several threads, corrupts the map: lost entries, NullPointerException in TreeMap.rotateRight, or a thread spinning forever in TreeMap.successor. Details are in #3321. The fix is already on main; this brings it to the next 5.0.x patch.

  • 4f9b513d5: ConcurrentSkipListMap(String.CASE_INSENSITIVE_ORDER) plus BindingServiceTests#concurrentFirstUseOfBindingsKeepsTheMapConsistent.
  • 6412b305a: atomic putIfAbsent in bindIfNecessary, with its test changes.

The BindingServiceTests file is identical to main after the backport. I have not run the Maven build for this branch locally, so I am relying on CI.

Refs #3321

AkashB23 and others added 2 commits November 18, 2026 18:19
spring-cloudGH-3158 changed the bindings map back to a TreeMap with a case-insensitive
comparator to restore case-insensitive binding names. That also dropped the
thread safety added by spring-cloudgh-3011 and kept by spring-cloudgh-3076.

The map is mutated on read: getBindingDestination(), getBindingProperties(),
getProducerProperties() and getConsumerProperties() add a missing binding on
first use (bindIfNecessary -> bindToDefault -> bindings.put). StreamBridge,
BindingService and the binders call these from many threads without any
lock. Concurrent first use of binding names corrupts the red-black tree:
entries are lost, size() no longer matches the iteration, a
NullPointerException is thrown from TreeMap.rotateRight (the failure
reported in spring-cloudgh-3011) or a thread spins forever in TreeMap.successor while
holding whatever monitor its caller holds, which stalls every thread that
needs the bindings.

Use a ConcurrentSkipListMap with String.CASE_INSENSITIVE_ORDER. It keeps
what spring-cloudGH-3158 needs (case-insensitive lookup, original key casing, sorted
iteration, same as the TreeMap) and is safe for concurrent use. A
ConcurrentHashMap cannot be used because it is case-sensitive.

The ConcurrentSkipListMap of spring-cloudgh-3011 was replaced by spring-cloudgh-3076 because of
slower start-up. That issue's thread does not show the map as the cause: the
reporter saw no improvement with the ConcurrentHashMap build and traced the
slowdown to a Spring Boot change in how child contexts are created, plus
binding properties placed under spring.cloud.config instead of
spring.cloud.stream, which made every first getBinder() call take the
bindToDefault() path.

The new test, in BindingServiceTests, fails on the TreeMap (entries are lost or
the map throws) and passes with this change.

Fixes spring-cloudgh-3321
See spring-cloudgh-3011, spring-cloudgh-3076, spring-cloudgh-3158

Signed-off-by: Akash Bhogar <[email protected]>
(cherry picked from commit 4f9b513)
Signed-off-by: Akash Bhogar <[email protected]>
Make binding creation atomic in BindingServiceProperties

Use putIfAbsent when registering default BindingProperties so that
concurrent first use of the same binding name always yields a single
shared instance. Previously, racing threads could each create and put
their own instance, silently discarding mutations made on the loser.

Resolves spring-cloud#3322

(cherry picked from commit 6412b30)
Signed-off-by: Akash Bhogar <[email protected]>

This branch has not been deployed

No deployments
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.

2 participants