[Cosmos] Make JsonSerializable.propertyBag final to fix sporadic NPE in DatabaseAccount lazy getters#49258
Conversation
|
Thank you for your contribution @lloydmeta! We will review the pull request and get back to you soon. |
c47f3ad to
d59c585
Compare
|
Build fails in an interesting way... which I'm not sure is related to this PR. |
|
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. |
Signed-off-by: lloydmeta <lloydmeta@gmail.com>
d59c585 to
ccd4415
Compare
|
/azp run java - cosmos - tests |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
Thanks @jeet1995 - there are a number of failed tests - do you think they're caused by the change in this PR are were they already failing/flakey on main? |
@lloydmeta - the failures are unrelated to your changes (I see it in a few other PRs too). |
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.