Skip to content

Make TextFileBasedViolationStore thread-safe under parallel test execution - #1656

Merged
StefanGraeber merged 1 commit into
TNG:mainfrom
kelunik:fix/thread-safe-violation-store
Jul 24, 2026
Merged

StefanGraeber merged 1 commit into
TNG:mainfrom
kelunik:fix/thread-safe-violation-store

Conversation

@kelunik

@kelunik kelunik commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Multiple FreezingArchRule instances each create a fresh TextFileBasedViolationStore and load stored.rules into their own private snapshot. Concurrent saves caused a lost-update: the last writer overwrote the file with only its own entries.

Fix by sharing one FileSyncedProperties per stored.rules canonical path via a static ConcurrentHashMap, and synchronizing writes with a putIfAbsent method that atomically checks, sets, and flushes to disk under one lock.

Fixes #1654.

@StefanGraeber StefanGraeber 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.

Thanks for contributing 🙂
The code itself looks good, just one minor edge case in the case of a non-existing directory which remains as race condition.

to make the pipeline pass, you must include a Signed-off-by: footer to your commit
https://github.com/TNG/ArchUnit/pull/1656/checks?check_run_id=85373415790

@StefanGraeber

Copy link
Copy Markdown
Contributor

you might have a merge conflict with #1655 which will be submitted soon.
It migrates some tests from Junit4 to Junit5 and thus exchange the Temporary File provider Rule with an Extension.
I think it has not touched the same test as you, but I might have missed it.

@kelunik
kelunik force-pushed the fix/thread-safe-violation-store branch from 7518e4c to 85656a5 Compare July 13, 2026 08:04
@kelunik

kelunik commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

you might have a merge conflict with #1655 which will be submitted soon.

Thanks for the heads up! It didn't have a conflict, but I rebased anyway and converted the test to JUnit 5.

The code itself looks good, just one minor edge case in the case of a non-existing directory which remains as race condition.

True, I added a separate test for that.

@kelunik

kelunik commented Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

Oh, these old Java versions, should be fixed.

@StefanGraeber StefanGraeber 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.

Please squash at least the last two commits to avoid having an invalid intermediate state.

The other comments are not hugely important to me. If you have an opposing preference, it's also ok for me

@kelunik
kelunik force-pushed the fix/thread-safe-violation-store branch from e847785 to 0fc0aaa Compare July 21, 2026 13:31
@kelunik

kelunik commented Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

@StefanGraeber Should all be addressed except for the optional comment on the assertion move.

…ution

Multiple FreezingArchRule instances each create a fresh TextFileBasedViolationStore
and load stored.rules into their own private snapshot. Concurrent saves caused a
lost-update: the last writer overwrote the file with only its own entries.

Fix by sharing one FileSyncedProperties per stored.rules canonical path via a
static ConcurrentHashMap, and synchronizing writes with a putIfAbsent method that
atomically checks, sets, and flushes to disk under one lock.

- Move directory creation into FileSyncedProperties.initializePropertiesFile
- Use mkdirs() || isDirectory() to handle the race where a concurrent process
  creates the directory between mkdirs() returning false and the isDirectory check
- Add TextFileBasedViolationStoreConcurrencyTest with separate initialize-race and
  save-race scenarios
- Convert both test classes from JUnit 4 to JUnit 5

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Niklas Keller <niklas@chrono24.com>
@StefanGraeber
StefanGraeber force-pushed the fix/thread-safe-violation-store branch from 0fc0aaa to 48f6118 Compare July 23, 2026 07:06
@StefanGraeber
StefanGraeber merged commit d42520d into TNG:main Jul 24, 2026
32 checks passed
@hankem hankem mentioned this pull request Aug 4, 2026
hankem added a commit that referenced this pull request Aug 4, 2026
# [ArchUnit 1.5.0](https://github.com/TNG/ArchUnit/releases/tag/v1.5.0)

## Enhancements

### Core
* Support Java 27 / class file major version 71 (#1618)
* Improve descriptions of `JavaAccess.Predicates.originOwner` and
`JavaAccess.Predicates.targetOwner` (#1603)
* Expose information on sealed classes via `JavaClass`: `isSealed()` and
`getPermittedSubclasses()` (#1677)
* Consider dependencies from caught exceptions (#1555)
* `ImportOption.DoNotIncludeTests` and `OnlyIncludeTests` consider tests
in custom Gradle source sets (#1660)

### Lang
* `ArchConditions` offers new `ArchCondition<JavaClass>
haveAnyDependenciesThat(DescribedPredicate<Dependency>)` (#1580)

### Library
* `ArchitectureMetrics.lakosMetrics` is computed much more performantly
(#1629)
* `TextFileBasedViolationStore` is now thread-safe under parallel test
execution (#1656)
* `ModuleDependency` now provides stable descriptions (#1648)

### JUnit
* `archunit-junit6` supports ArchUnit with JUnit 6 (~#1556~, ~#1658~,
#1659)
* `AnalyzeClasses` supports individual classes (#1569)
* (`archunit-junit:0.9.0` (which was renamed with [ArchUnit
0.9.0](https://github.com/TNG/ArchUnit/releases/tag/v0.9.0)) now
relocates to `archunit-junit4:0.9.0` (#1673))
    * will only be published after #1684 🙈

### Documentation
* Document `Priority` (#1671)
* Document Package Identifiers in User Guide (#1672)

## Internal Improvements
* Text files now use UNIX line breaks (#1602, #1611)
* Update Gradle from 8 to 9; build & test with JDK 25 (#1550, and also
#1612, #1619)
* Separate integration tests by JUnit version (#1636)
* Replace `junit-dataprovider` with `junit-jupiter-params` (#1655)
* ArchUnit releases now update Maven examples (and Gradle wrapper) of
[ArchUnit-Examples](https://github.com/TNG/ArchUnit-Examples) (#1450)
* Avoid managing hamcrest dependency (#1467)
@kelunik
kelunik deleted the fix/thread-safe-violation-store branch August 13, 2026 08:48
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.

TextFileBasedViolationStore is not thread-safe under parallel test execution

2 participants