Skip to content

Fix NRE in batch state checker on fresh ExecutionContext#25367

Merged
salihozkara merged 1 commit into
rel-10.3from
maliming/fix-batch-state-checker-nre
May 5, 2026
Merged

Fix NRE in batch state checker on fresh ExecutionContext#25367
salihozkara merged 1 commit into
rel-10.3from
maliming/fix-batch-state-checker-nre

Conversation

@maliming

@maliming maliming commented May 5, 2026

Copy link
Copy Markdown
Member

RequireFeaturesSimpleBatchStateChecker<TState>.Current and RequirePermissionsSimpleBatchStateChecker<TState>.Current initialize their AsyncLocal<> value from a static type initializer, so the value is only visible on the ExecutionContext that triggered it. Any other ExecutionContext (a new HTTP request, queued event handler, Task.Run without flow) sees null and crashes AddCheckModels(...) with NRE — reproducible by await _distributedEventBus.PublishAsync(new StaticPermissionDefinitionChangedEvent()), which clears the static permission caches and forces the next request to rebuild definitions on a fresh ExecutionContext through a provider that uses RequireFeatures(...) (e.g. AbpAuditLoggingPermissionDefinitionProvider).

Lazy-init Current per ExecutionContext.

Copilot AI review requested due to automatic review settings May 5, 2026 01:05
@maliming maliming added this to the 10.3-patch milestone May 5, 2026

Copilot AI 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.

Pull request overview

This PR fixes an AsyncLocal initialization bug in ABP’s batch state checkers so fresh ExecutionContexts can rebuild feature/permission-based state without hitting a NullReferenceException. It updates the core batch checker accessors and adds regression coverage around permission-definition cache rebuilds and fresh-context access.

Changes:

  • Changed RequireFeaturesSimpleBatchStateChecker<TState>.Current and RequirePermissionsSimpleBatchStateChecker<TState>.Current to lazily initialize per execution context.
  • Added regression tests for accessing Current from a fresh ExecutionContext and for rebuilding static permission definitions after cache clears.
  • Updated authorization test setup to include the features module and a feature-gated permission definition provider.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
framework/src/Volo.Abp.Features/Volo/Abp/Features/RequireFeaturesSimpleBatchStateChecker.cs Switches feature batch checker Current from static-constructor init to lazy AsyncLocal init.
framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/RequirePermissionsSimpleBatchStateChecker.cs Applies the same lazy AsyncLocal initialization to permission batch checkers.
framework/test/Volo.Abp.Features.Tests/Volo/Abp/Features/RequireFeaturesSimpleBatchStateChecker_Tests.cs Adds a fresh-ExecutionContext regression test for feature batch checker access.
framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/RequirePermissionsSimpleBatchStateChecker_Tests.cs Adds the matching fresh-ExecutionContext regression test for permission batch checker access.
framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/FeatureGatedTestPermissionDefinitionProvider.cs Introduces a permission definition that exercises RequireFeatures(...) during permission-store rebuilds.
framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/StaticPermissionDefinitionStore_Tests.cs Adds a cache-clear/rebuild regression test for permission definitions on a fresh execution context.
framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/Authorization_Tests.cs Relaxes group-count expectations to allow the extra test permission group.
framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/AbpAuthorizationTestModule.cs Adds AbpFeaturesModule to the authorization test module dependencies.
framework/test/Volo.Abp.Authorization.Tests/Volo.Abp.Authorization.Tests.csproj Adds the features project reference needed by the new authorization regression tests.

@codecov

codecov Bot commented May 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 49.78%. Comparing base (d6c5fc7) to head (da37d5f).

Additional details and impacted files
@@             Coverage Diff              @@
##           rel-10.3   #25367      +/-   ##
============================================
+ Coverage     49.30%   49.78%   +0.48%     
============================================
  Files          3667     3609      -58     
  Lines        123171   120714    -2457     
  Branches       9407     9220     -187     
============================================
- Hits          60731    60099     -632     
+ Misses        60631    58818    -1813     
+ Partials       1809     1797      -12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@maliming maliming requested a review from salihozkara May 5, 2026 02:01
@salihozkara salihozkara merged commit 6d770b0 into rel-10.3 May 5, 2026
8 checks passed
@salihozkara salihozkara deleted the maliming/fix-batch-state-checker-nre branch May 5, 2026 04:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants