Skip to content

CronTriggeringPolicy can name the first scheduled archive after the new period if it fires during startup #4529

Backlog

Description

@GChuf

Description

CronTriggeringPolicy.initialize() stores the scheduled future only after ConfigurationScheduler.scheduleWithCron() returns:

future = scheduler.scheduleWithCron(cronExpression, now, new CronTrigger());

The first fire runs on a scheduler thread. If it runs before future is stored, rollover() falls back to the current time:

final Date rollTime = future != null ? future.getFireTime() : new Date();
manager.rollover(cronExpression.getPrevFireTime(rollTime), rollTime);

If that rollover runs even 1 ms after the fire time, getPrevFireTime(rollTime) returns the fire time itself. The archive is then named after the period that has just started instead of the period that has just ended. The next scheduled rollover archives the following period under that same name. Without %i in the file pattern, the rename replaces the earlier archive.

The window is narrow. The policy must start just before a fire time, so that the first fire starts between scheduling and storing future. Or the starting thread must be paused at that point. Frequent schedules make this more likely. #2073 was the same kind of race inside ConfigurationScheduler and was hit with frequent schedules. I haven't seen this race in a running application; the test below forces the thread order.

future is also not volatile, although stop() and the scheduler thread read it from other threads.

Proposed fix: store future while holding the policy's lock, and read it in rollover() under the same lock. The first rollover then waits until future is set. Also make the field volatile.

synchronized (this) {
    future = scheduler.scheduleWithCron(cronExpression, now, new CronTrigger());
}
private void rollover() {
    final CronScheduledFuture<?> scheduledFuture;
    synchronized (this) {
        scheduledFuture = future;
    }
    final Date rollTime = scheduledFuture != null ? scheduledFuture.getFireTime() : new Date();
    ...
}

Configuration

Version: 2.x (2.27.0-SNAPSHOT)

Operating system: Windows 10

JDK: OpenJDK 25.0.2

Logs

Every-second schedule. The first fire at 13:05:55 runs 50 ms late, before scheduleWithCron() returns:

Wanted:
rollingFileManager.rollover(
    Wed Nov 25 13:05:54 CET 2026,
    Wed Nov 25 13:05:55 CET 2026
);
Actual invocations have different arguments:
rollingFileManager.rollover(
    Wed Nov 25 13:05:55 CET 2026,
    Wed Nov 25 13:05:55 CET 2026
);

The first argument is the period the archive is named after. The second is the current time, 50 ms after the fire time; Date.toString() drops the milliseconds.

Reproduction

CronTriggeringPolicyFirstFireTest in the linked PR. The test gives the policy a ConfigurationScheduler whose scheduleWithCron() runs the cron command on another thread, 50 ms after the fire time. scheduleWithCron() returns only once that command has finished or is blocked. The test then checks that the manager rolls over with the previous fire time and the fire time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions