Skip to content

CAMEL-24152: Fix concurrent marshal/unmarshal data corruption in camel-snakeyaml#24814

Merged
davsclaus merged 1 commit into
apache:mainfrom
Croway:CAMEL-24152
Jul 17, 2026
Merged

CAMEL-24152: Fix concurrent marshal/unmarshal data corruption in camel-snakeyaml#24814
davsclaus merged 1 commit into
apache:mainfrom
Croway:CAMEL-24152

Conversation

@Croway

@Croway Croway commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Summary

Claude Code on behalf of Croway

Fixes CAMEL-24152: concurrent marshal/unmarshal operations corrupt data because all per-thread Yaml instances share single BaseConstructor and Representer singletons created once in doStart().

This was a regression introduced by CAMEL-22354 (commit 5dfc5c2857e9, released in 4.15.0), which replaced Function<CamelContext, BaseConstructor> factories with shared singletons while retaining the ThreadLocal<Yaml> cache. Since SnakeYAML's BaseConstructor and Representer are heavily stateful, sharing them across threads causes:

  1. Internal SnakeYAML crashes (IndexOutOfBoundsException, ParserException, NPE)
  2. Marshal crashes (NullPointerException on nodes)
  3. Silent cross-thread data bleed (one thread's unmarshal returns another thread's payload)

Changes

  • SnakeYAMLDataFormat.doStart() — Removed default singleton creation for constructor, representer, dumperOptions, resolver.
  • SnakeYAMLDataFormat.getYaml() — When creating a new Yaml, calls default*() factory methods to produce fresh per-thread instances instead of reusing shared singletons. User-supplied custom instances (via setConstructor() etc.) are still used as-is.
  • SnakeYAMLConcurrentTest — New test: 8 threads × 500 marshal/unmarshal round-trips with distinct payloads, asserting no exceptions and no cross-thread data corruption.

Test plan

  • mvn verify in components/camel-snakeyaml — all 19 tests pass (including new concurrency test)
  • Verify existing SnakeYAML tests pass in CI (SnakeYAMLTest, SnakeYAMLDoSTest, SnakeYAMLTypeFilterTest, etc.)
  • Verify the new concurrent test passes in CI

🤖 Generated with Claude Code

…l-snakeyaml

Since CAMEL-22354 (4.15.0), all per-thread Yaml instances shared single
BaseConstructor and Representer singletons. These SnakeYAML classes are
heavily stateful and mutated on every load/dump, causing concurrent
crashes and silent cross-thread data bleed.

Create fresh BaseConstructor, Representer, DumperOptions, and Resolver
per cached Yaml in getYaml(), restoring per-thread construction.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

🌟 Thank you for your contribution to the Apache Camel project! 🌟
🤖 CI automation will test this PR automatically.

🐫 Apache Camel Committers, please review the following items:

  • First-time contributors require MANUAL approval for the GitHub Actions to run
  • You can use the command /component-test (camel-)component-name1 (camel-)component-name2.. to request a test from the test bot although they are normally detected and executed by CI.
  • You can label PRs using skip-tests and test-dependents to fine-tune the checks executed by this PR.
  • Build and test logs are available in the summary page. Only Apache Camel committers have access to the summary.

⚠️ Be careful when sharing logs. Review their contents before sharing them publicly.

@gnodet gnodet left a comment

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.

Claude Code on behalf of gnodet

LGTM — important concurrency fix for a real data-corruption bug.

Root cause: CAMEL-22354 (5dfc5c2857e9, Aug 2025) replaced per-thread Function<CamelContext, BaseConstructor> factories with shared singletons. The ThreadLocal<Yaml> cache remained, but all per-thread Yaml instances now shared the same BaseConstructor, Representer, DumperOptions, and Resolver. Since SnakeYAML's BaseConstructor and Representer mutate internal state on every load()/dump(), concurrent exchanges produce crashes (IndexOutOfBoundsException, NPEs) and silent cross-thread data corruption.

The fix: Correct and minimal — moves construction of default BaseConstructor, Representer, DumperOptions, and Resolver into getYaml() so each thread-cached Yaml gets fresh instances via the default*() factory methods. User-supplied custom instances (via setters) are still used as-is, which is a reasonable trade-off — restoring the factory pattern for custom instances would require an API change.

The test: Good stress test — 8 threads × 500 marshal/unmarshal round-trips with distinct per-thread payloads, CountDownLatch for maximum contention, and explicit per-iteration data integrity assertion.

Minor note: The dual LoaderOptions pattern (one in defaultConstructor(), another in getYaml()) is pre-existing — the PR preserves the original behavior.

This should be considered for backport to 4.18.x if 4.15.0+ releases are affected.

@github-actions

Copy link
Copy Markdown
Contributor

🧪 CI tested the following changed modules:

  • components/camel-snakeyaml

🔬 Scalpel shadow comparison — Scalpel: 9 tested, 29 compile-only — current: 9 all tested

Maveniverse Scalpel detected 38 affected modules (current approach: 9).

⚠️ Modules only in Scalpel (29)
  • apache-camel
  • camel-allcomponents
  • camel-catalog
  • camel-catalog-console
  • camel-catalog-lucene
  • camel-catalog-maven
  • camel-catalog-suggest
  • camel-componentdsl
  • camel-csimple-maven-plugin
  • camel-endpointdsl
  • camel-endpointdsl-support
  • camel-itest
  • camel-jbang-core
  • camel-jbang-it
  • camel-jbang-main
  • camel-jbang-plugin-edit
  • camel-jbang-plugin-generate
  • camel-jbang-plugin-kubernetes
  • camel-jbang-plugin-test
  • camel-kamelet-main
  • camel-launcher
  • camel-report-maven-plugin
  • camel-route-parser
  • camel-yaml-dsl
  • camel-yaml-dsl-deserializers
  • camel-yaml-dsl-maven-plugin
  • coverage
  • docs
  • dummy-component

Skip-tests mode would test 9 modules (1 direct + 8 downstream), skip tests for 29 (generated code, meta-modules)

Modules Scalpel would test (9)
  • camel-jbang-mcp
  • camel-jbang-plugin-mcp
  • camel-jbang-plugin-route-parser
  • camel-jbang-plugin-tui
  • camel-jbang-plugin-validate
  • camel-launcher-container
  • camel-snakeyaml
  • camel-yaml-dsl-validator
  • camel-yaml-dsl-validator-maven-plugin
Modules with tests skipped (29)
  • apache-camel
  • camel-allcomponents
  • camel-catalog
  • camel-catalog-console
  • camel-catalog-lucene
  • camel-catalog-maven
  • camel-catalog-suggest
  • camel-componentdsl
  • camel-csimple-maven-plugin
  • camel-endpointdsl
  • camel-endpointdsl-support
  • camel-itest
  • camel-jbang-core
  • camel-jbang-it
  • camel-jbang-main
  • camel-jbang-plugin-edit
  • camel-jbang-plugin-generate
  • camel-jbang-plugin-kubernetes
  • camel-jbang-plugin-test
  • camel-kamelet-main
  • camel-launcher
  • camel-report-maven-plugin
  • camel-route-parser
  • camel-yaml-dsl
  • camel-yaml-dsl-deserializers
  • camel-yaml-dsl-maven-plugin
  • coverage
  • docs
  • dummy-component

ℹ️ Shadow mode — Scalpel observes but does not affect test execution. Learn more

All tested modules (38 modules)
  • Camel :: All Components Sync point
  • Camel :: Assembly
  • Camel :: Catalog :: CSimple Maven Plugin (deprecated)
  • Camel :: Catalog :: Camel Catalog
  • Camel :: Catalog :: Camel Report Maven Plugin
  • Camel :: Catalog :: Camel Route Parser
  • Camel :: Catalog :: Console
  • Camel :: Catalog :: Dummy Component
  • Camel :: Catalog :: Lucene (deprecated)
  • Camel :: Catalog :: Maven
  • Camel :: Catalog :: Suggest
  • Camel :: Component DSL
  • Camel :: Coverage
  • Camel :: Docs
  • Camel :: Endpoint DSL
  • Camel :: Endpoint DSL :: Support
  • Camel :: Integration Tests
  • Camel :: JBang :: Core
  • Camel :: JBang :: Integration tests
  • Camel :: JBang :: MCP
  • Camel :: JBang :: Main
  • Camel :: JBang :: Plugin :: Edit
  • Camel :: JBang :: Plugin :: Generate
  • Camel :: JBang :: Plugin :: Kubernetes
  • Camel :: JBang :: Plugin :: MCP
  • Camel :: JBang :: Plugin :: Route Parser
  • Camel :: JBang :: Plugin :: TUI
  • Camel :: JBang :: Plugin :: Testing
  • Camel :: JBang :: Plugin :: Validate
  • Camel :: Kamelet Main
  • Camel :: Launcher
  • Camel :: Launcher :: Container
  • Camel :: SnakeYAML
  • Camel :: YAML DSL
  • Camel :: YAML DSL :: Deserializers
  • Camel :: YAML DSL :: Maven Plugins
  • Camel :: YAML DSL :: Validator
  • Camel :: YAML DSL :: Validator Maven Plugin

⚙️ View full build and test results

@oscerd oscerd left a comment

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.

LGTM — correct thread-safety fix for a real data-corruption bug. The shared SnakeYAMLDataFormat reused stateful BaseConstructor/Representer/DumperOptions/Resolver singletons across threads (snakeyaml's Yaml isn't thread-safe), so concurrent marshal/unmarshal corrupted output. Building fresh default*() instances per thread-cached Yaml isolates them; since they're cached in the existing per-thread ThreadLocal<WeakReference<Yaml>> (created once on cache-miss, not per call) there's no perf cliff and no new ThreadLocal leak, and doStop() still clears the cache. SnakeYAMLConcurrentTest (8 threads × 500 round-trips) is a good regression guard.

Two non-blocking notes: a user-supplied custom constructor/representer (via the setters) is still shared across threads — opt-in and out of scope here, but worth a follow-up if you want full isolation. And getConstructor()/getRepresenter()/etc. now return null after start when not user-set (was internal defaults) — no signature change and nothing internal depends on it, just flagging the observable getter change.

Reviewed with Claude Code on behalf of Andrea Cosentino. This review was generated by an AI agent and may contain inaccuracies; please verify all suggestions before applying.

@davsclaus davsclaus added this to the 4.22.0 milestone Jul 17, 2026
@davsclaus davsclaus added the bug Something isn't working label Jul 17, 2026
@davsclaus
davsclaus merged commit fdd9a7b into apache:main Jul 17, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants