Skip to content

[CRE-7120]core/config: deep-copy in Core.SetFrom to avoid aliasing source config - #24059

Draft
pavel-raykov wants to merge 3 commits into
developfrom
cre-7120-deep-copy-core-setfrom
Draft

pavel-raykov wants to merge 3 commits into
developfrom
cre-7120-deep-copy-core-setfrom

Conversation

@pavel-raykov

@pavel-raykov pavel-raykov commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

Alternative to #24052 that fixes the root cause in SetFrom instead of re-decoding defaults on every call.

Problem

toml.Core.SetFrom (and every nested setFrom) copies pointers, slices and maps from its source. Its result therefore shares memory with the source:

  • CoreDefaults() shares with the package-level decoded defaults. In-place test overrides such as *c.Insecure.DevWebServer = true change the defaults for every later config, and race under parallel tests.
  • setDefaults (core.SetFrom(&c.Core)) makes the effective config share with the user input config, and Config.SetFrom does the same when merging config strings.

Fix

  • Core.SetFrom deep-copies its argument with huandu/go-clone (clone.Slowly) before merging. The same library is already used for copying config defaults in chainlink-common x/config. It is a new direct dependency here (indirect in the other modules, via make gomodtidy).
  • CoreDefaults() still decodes the embedded core.toml once at init. That keeps fail-fast startup and avoids a TOML decode per call.

Tests

  • TestCoreDefaults_distinctInstances: writes through one CoreDefaults() result and checks that another is unaffected.
  • TestCoreDefaults_noSharedReferences: reflection check that two CoreDefaults() results share no pointer, slice or map.
  • TestGeneralConfigOpts_ConcurrentOverrideAndNew: concurrent in-place overrides and New(); reports races under -race without the fix.

With the deep copy removed, the first two tests fail and the third reports data races.

Not covered

  • EVM chain defaults (configtoml.Defaults in chainlink-evm) still share pointers with package-level state and need a matching change there.
  • Config.SetFrom merges the other chains (EVM, Solana, etc.) through their own SetFrom, which this PR does not change.

🤖 Generated with Claude Code

Core.SetFrom copied pointers, slices and maps from its source, so the
result aliased it: writing through the effective config (e.g. test
overrides like *c.Insecure.DevWebServer = true) mutated the shared
package-level defaults and the user input config, and raced under
parallel tests. SetFrom now deep-copies its argument first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

I see you updated files related to core. Please run make gocs in the root directory to add a changeset as well as in the text include at least one of the following tags:

  • #added For any new functionality added.
  • #breaking_change For any functionality that requires manual action for the node to boot.
  • #bugfix For bug fixes.
  • #changed For any change to the existing functionality.
  • #db_update For any feature that introduces updates to database schema.
  • #deprecation_notice For any upcoming deprecation functionality.
  • #internal For changesets that need to be excluded from the final changelog.
  • #nops For any feature that is NOP facing and needs to be in the official Release Notes for the release.
  • #removed For any functionality/config that is removed.
  • #updated For any functionality that is updated.
  • #wip For any change that is not ready yet and external communication about it should be held off till it is feature complete.

@github-actions

Copy link
Copy Markdown
Contributor

✅ No conflicts with other open PRs targeting develop

Comment thread core/config/toml/types.go Outdated
// Values are deep-copied, so c never aliases f: later writes through either one
// do not affect the other.
func (c *Core) SetFrom(f *Core) {
fc := deepCopy(*f)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

concerns as expressed earlier for the deep copy approach comment here: #24052 (comment)

Replace the hand-rolled reflection helper with go-clone, already used
for the same purpose in chainlink-common x/config. It also copies
private fields (e.g. the *big.Int inside decimal.Decimal), which the
reflection helper had to copy by value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@trunk-io

trunk-io Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Static Badge   Static Badge   Static Badge

View Full Report ↗︎ ⋅ Docs

@pavel-raykov pavel-raykov changed the title core/config: deep-copy in Core.SetFrom to avoid aliasing source config [CRE-7120]core/config: deep-copy in Core.SetFrom to avoid aliasing source config Oct 23, 2026
Slowly also handles cyclic and shared pointers, matching how
chainlink-common x/config clones its config defaults.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cl-sonarqube-production

Copy link
Copy Markdown

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