Skip to content

LOG4J2-2073: Fix NPE in ConfigurationScheduler when scheduling cron tasks#3941

Closed
bhawnapannu2701 wants to merge 1 commit into
apache:2.xfrom
bhawnapannu2701:fix-2073-cron-npe
Closed

LOG4J2-2073: Fix NPE in ConfigurationScheduler when scheduling cron tasks#3941
bhawnapannu2701 wants to merge 1 commit into
apache:2.xfrom
bhawnapannu2701:fix-2073-cron-npe

Conversation

@bhawnapannu2701

Copy link
Copy Markdown

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.

@vy vy self-assigned this Oct 3, 2025
@vy vy added bug Incorrect, unexpected, or unintended behavior of existing code configuration Affects the configuration system in a general way labels Oct 3, 2025
@vy vy added this to the 2.26.0 milestone Oct 3, 2025

@vy vy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@bhawnapannu2701, thanks so much for the PR. Please see the remarks I've shared.

I strongly advice you to read the Development guide. This will make life easier for both of us.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would you implement the following changes, please?

  1. This test is not effective. That is, it even passes without the fix. Could you make sure this test fails without the fix?
  2. Could you move this test from log4j-core/src/test to log4j-core-test/src/test?
  3. Could you add the license header?
  4. Could you make sure ./mvnw verify -pl :log4j-core,:log4j-core-test passes before submitting your changes?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

  1. Remove the FIX (part 1)-sort comments, please. Instead, if something is not obvious at first glance, explain in the rationale, when necessary.
  2. Run linter: ./mvnw spotless:apply -pl :log4j-core
  3. Don't touch/change unrelated lines

@AnvitAjgaokar

Copy link
Copy Markdown

Hi @vy,

I noticed that this PR is still open and has some review comments pending.
I’d like to ask if it would be okay for me to work on addressing the requested changes
(for LOG4J2-2073: Fix NPE in ConfigurationScheduler when scheduling cron tasks).

If this is still active, I’d be happy to help move it forward and submit an update.
Please let me know if that would be helpful, or if someone is already working on it.

Thanks for your time and for maintaining the project.

@vy

vy commented Feb 16, 2026

Copy link
Copy Markdown
Member

@AnvitAjgaokar, you don't need a permission. Just submit your PR.

@vy

vy commented Apr 28, 2026

Copy link
Copy Markdown
Member

@ramanathan1504, this is very close to the finish line. Would you like to pick this one up?

@ramanathan1504

Copy link
Copy Markdown
Contributor

@vy, Yes, I can take this one. I’ll review what’s left and push it to completion.

@vy

vy commented May 28, 2026

Copy link
Copy Markdown
Member

Superseded by #4118.

@vy vy closed this May 28, 2026
@github-project-automation github-project-automation Bot moved this from Changes requested to Dropped in Log4j pull request tracker May 28, 2026
@github-project-automation github-project-automation Bot moved this from To triage to Done in Log4j bug tracker May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Incorrect, unexpected, or unintended behavior of existing code configuration Affects the configuration system in a general way

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants