LOG4J2-2073: Fix NPE in ConfigurationScheduler when scheduling cron tasks #3941
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.
Problem
ConfigurationScheduler.scheduleWithCron created a CronRunnable, scheduled it, and only afterwards assigned its CronScheduledFuture.
Under fast-firing cron expressions the runnable could execute before its future was set, causing a NullPointerException when calling scheduledFuture.getFireTime().
Root cause
Race condition between scheduling the task and assigning the CronScheduledFuture to the runnable.
Fix
Assign a placeholder CronScheduledFuture to the runnable before scheduling.
Update that placeholder with the real ScheduledFuture immediately after scheduling.
Add null-guards in CronRunnable.run() and handle first execution safely.
Enhance toString() to tolerate an unassigned future.
Tests
Added CronSchedulerNpeTest which schedules a cron expression firing every second and asserts that it runs without throwing.
Before the fix it reproduced the NPE; after the fix all tests pass.
Risks
Minimal. Changes are contained to cron scheduling logic and do not alter normal scheduling semantics.