[Cosmos] Make JsonSerializable.propertyBag final to fix sporadic NPE in DatabaseAccount lazy getters#49258
Open
lloydmeta wants to merge 2 commits into
Open
Conversation
Contributor
|
Thank you for your contribution @lloydmeta! We will review the pull request and get back to you soon. |
Signed-off-by: lloydmeta <lloydmeta@gmail.com>
c47f3ad to
d59c585
Compare
Contributor
Author
|
Build fails in an interesting way... which I'm not sure is related to this PR. |
This was referenced May 26, 2026
Member
|
Thanks @lloydmeta for your contribution. I'll do a review and have a potential fix for the CI failure (this isn't related to your PR) - #49263 Update the CI failure fix has been merged. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #49256.
JsonSerializable.propertyBagis non-volatileand non-final, assigned exactly once in each of the sixJsonSerializableconstructors and never written to outside them in the SDK. Combined with the lazy-initialised, non-volatilefield assignments inDatabaseAccount.getConsistencyPolicy()(and its three siblings), this enables an unsafe publication: a concurrent reader can observeconsistencyPolicy != nullwhile observing the newConsistencyPolicy'spropertyBagfield at its default valuenull, producing theNullPointerExceptionreported in #49256.Making
propertyBagfinalinvokes final-field publication semantics (JLS §17.5, JSR-133 FAQ): a thread that sees the reference to the constructed object after its constructor completes is guaranteed to see the correctly-initialised final field without requiring a synchronizes-with edge from the publishing thread. This closes the race window forConsistencyPolicyand for every otherJsonSerializablesubclass that could be lazy-published in the same way.The change is one keyword. Every existing constructor already assigns
propertyBag, so the only other mechanical change is dropping the redundant= nulldefault initialiser.Why this fix vs. the alternatives
volatileonconsistencyPolicy(and siblings) inDatabaseAccount: also closes the window, but only for those four fields, and leaves the latent risk for every otherJsonSerializablesubclass that could be lazy-published anywhere in the SDK.DatabaseAccount(ObjectNode): works forDatabaseAccountspecifically but is structurally larger, doesn't generalise to other subclasses, and changes when work happens (eagerly perDatabaseAccountconstruction, including on everyGlobalEndpointManagerrefresh).final propertyBagis the smallest patch that closes the bug class globally.Safety / compatibility
propertyBagis package-private tocom.azure.cosmos.implementation. A grep acrosssdk/cosmosforpropertyBag\s*=finds zero writes outside the sixJsonSerializableconstructors, zero reflective writes, and no setter. The only out-of-class mention is@JsonIgnoreProperties("propertyBag")onRange, which doesn't touch the field.populatePropertyBag()overrides: Several subclasses (e.g.DatabaseAccount,DocumentCollection, theFeedRange*ImplandChangeFeedStartFrom*Implfamilies) overridepopulatePropertyBag()and mutate the bag viathis.set(...)orsetProperties(this, false). These mutate theObjectNodecontents through the reference, not the reference itself, sofinaldoesn't affect them.propertyBag. Nothing in this SDK does, but flagging in case there's a downstream/private consumer to check.Testing
Existing unit tests cover the
JsonSerializableconstructor paths and thegetString/getObject/getWithMappingreads, with no expected behaviour change for correctly-published instances. The actual bug is a non-deterministic JMM unsafe-publication race that doesn't reproduce reliably in a standard JUnit test, a JCStress harness would be the right tool. Happy to add one in a follow-up if the team finds it useful.All SDK Contribution checklist:
General Guidelines and Best Practices
Testing Guidelines
See "Testing" section above for why a deterministic functional regression test isn't included with this PR.