fix: migrate engine fields to AtomicReference snapshots (S3077, Phase 3)#425
Open
fix: migrate engine fields to AtomicReference snapshots (S3077, Phase 3)#425
Conversation
Phase 3 of the Sonar bug remediation blueprint. Migrates the remaining 13 volatile fields across 5 engine classes (Capability, ClientAdapter, McpServerAdapter, ScriptStepExecutor, TelemetryBootstrap) from `volatile` keyword to `AtomicReference<T>` with immutable snapshots. Static singleton TelemetryBootstrap.instance becomes a static AtomicReference because it is reset at runtime by init/forTesting/reset (the IODH idiom does not fit a resettable singleton). Pattern applied: - Scalar references: AtomicReference<T> with .get()/.set() - List<T> fields: AtomicReference<List<T>> wrapping CopyOnWriteArrayList for lock-free iteration - Map<K,V> fields: AtomicReference<Map<K,V>> with HashMap snapshot built before publication - ScriptStepExecutor.execute() now snapshots the spec once at entry to observe a consistent view for the call duration Closes #424
4 tasks
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.
Summary
Phase 3 of the Sonar bug remediation blueprint. Migrates the remaining 13
volatilefields across 5 engine-layer classes from thevolatilekeyword toAtomicReference<T>(or staticAtomicReferencefor resettable singletons) holding immutable snapshots.This is the engine-layer counterpart to #423 (spec POJOs), and addresses the same SonarQube rule
java:S3077. The two PRs touch disjoint files and can land in any order.Closes #424.
Files migrated
io.naftiko.Capabilityspec,serverAdapters,clientAdapters,aggregates,bindings,scriptingSpec,stepHandlerRegistryCopyOnWriteArrayList), C (Map snapshot)io.naftiko.engine.consumes.ClientAdaptercapability,specio.naftiko.engine.exposes.mcp.McpServerAdapterstdioHandler,stdioThreadio.naftiko.engine.scripting.ScriptStepExecutorscriptingSpecexecute()io.naftiko.engine.observability.TelemetryBootstrapinstanceAtomicReference(singleton is reset at runtime byinit/forTesting/reset, so the IODH idiom does not fit)Why
AtomicReferenceand not the holder idiomTelemetryBootstrap.instanceis explicitly reset at runtime byinit(String, ObservabilitySpec),init(OpenTelemetry)andreset()— the initialization-on-demand holder idiom assumes a write-once singleton, which does not match this lifecycle.Notes for reviewers
ScriptStepExecutor.execute()now snapshotsscriptingSpeconce at entry into a local of the same name, so all subsequent reads in the method observe a consistent view for the duration of the call (avoids races with a future hot-reload setter).Capabilityconstructor builds local list variables fully before publishing them viaAtomicReference#set— avoids partial-state observation if the constructor throws.CopyOnWriteArrayListinside theAtomicReferenceso request-handling iteration remains lock-free even if a future hot-reload swaps the slot.ClientAdapter#setSpec(HttpClientSpec)keeps its subtype-narrowing parameter (intentional, predates this PR).Test plan
EngineFieldThreadSafetyTest(5 parameterized cases) — reflects over each migrated class and asserts no field isvolatile. Acts as a regression guard.Step{2-8,10}Shipyard*IntegrationTest) that return prose like"The..."instead of JSON, causingUnrecognized token 'The'— same set as onmainand unaffected by this change.test: add failing meta-test...), then fix (fix: migrate engine fields...).