Skip to content

[BUG] Daily Limits are broken #3875

Description

@YukiNagat0

demo (on main):

Demo.mp4

Steps to reproduce:

  1. Create a new profile, a new deck and a new preset
  2. In the New cards/day section set all Preset, This deck and Today only to zero
  3. Click Save
  4. Again, go to Deck options, click on the Preset and change it to any value
  5. Click Save -> panic at rslib\src\deckconfig\update.rs:419:39: attempt to subtract with overflow

I modified the rslib\src\deckconfig\update.rs:update_deck_limits/update_day_limit in the following way:

fn update_deck_limits(deck: &mut NormalDeck, limits: &Limits, today: u32) {
    println!("limits={limits:?}");
    deck.review_limit = limits.review;
    deck.new_limit = limits.new;
    update_day_limit(&mut deck.review_limit_today, limits.review_today, today);
    update_day_limit(&mut deck.new_limit_today, limits.new_today, today);
}

fn update_day_limit(day_limit: &mut Option<DayLimit>, new_limit: Option<u32>, today: u32) {
    println!("day_limit={day_limit:?}\tnew_limit={new_limit:?}\ttoday={today:?}");
    if let Some(limit) = new_limit {
        day_limit.replace(DayLimit { limit, today });
    } else if let Some(limit) = day_limit {
        // instead of setting to None, only make sure today is in the past,
        // thus preserving last used value
        limit.today = limit.today.min(today - 1);
    }
}

After doing that we get the following traceback (and debug prints):

### First save ###

limits=Limits { review: None, new: None, review_today: None, new_today: Some(0), review_today_active: false, new_today_active: false }
day_limit=None  new_limit=None  today=0
day_limit=None  new_limit=Some(0)       today=0

### First save ###

### Panic (on second save) ###

limits=Limits { review: None, new: None, review_today: None, new_today: None, review_today_active: false, new_today_active: true }
day_limit=None  new_limit=None  today=0
day_limit=Some(DayLimit { limit: 0, today: 0 }) new_limit=None  today=0

thread '<unnamed>' panicked at rslib\src\deckconfig\update.rs:419:39:
attempt to subtract with overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Traceback (most recent call last):
  File "C:\Users\redacted\Documents\dev\anki\qt\aqt\progress.py", line 121, in handler
    func()
  File "C:\Users\redacted\Documents\dev\anki\qt\aqt\taskman.py", line 135, in <lambda>
    100, lambda: on_done(fut), requires_collection=False
  File "C:\Users\redacted\Documents\dev\anki\qt\aqt\operations\__init__.py", line 123, in wrapped_done
    future.result()
  File "C:\Users\redacted\Documents\dev\anki\out\extracted\python\lib\concurrent\futures\_base.py", line 439, in result
    return self.__get_result()
  File "C:\Users\redacted\Documents\dev\anki\out\extracted\python\lib\concurrent\futures\_base.py", line 391, in __get_result
    raise self._exception
  File "C:\Users\redacted\Documents\dev\anki\out\extracted\python\lib\concurrent\futures\thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "C:\Users\redacted\Documents\dev\anki\qt\aqt\operations\__init__.py", line 108, in wrapped_op
    return self._op(mw.col)
  File "C:\Users\redacted\Documents\dev\anki\qt\aqt\operations\deck.py", line 98, in <lambda>
    return CollectionOp(parent, lambda col: col.decks.update_deck_configs(input))
  File "C:\Users\redacted\Documents\dev\anki\pylib\anki\decks.py", line 298, in update_deck_configs
    op_bytes = self.col._backend.update_deck_configs_raw(input.SerializeToString())
  File "C:\Users\redacted\Documents\dev\anki\out/pylib\anki\_backend_generated.py", line 752, in update_deck_configs_raw
    return self._run_command(11, 7, message)
  File "C:\Users\redacted\Documents\dev\anki\pylib\anki\_backend.py", line 161, in _run_command
    return self._backend.command(service, method, input)
pyo3_runtime.PanicException: attempt to subtract with overflow

### Panic ###

Most obvious candidates on bugs are:

  • rslib\src\deckconfig\update.rs:update_day_limit: limit.today = limit.today.min(today - 1); <- no protection from overflow
  • ts/routes/deck-options/DailyLimits.svelte: newTabs array on the line 72
  • ts/routes/deck-options/lib.ts

The problem seems not even be the overflow itself but the fact that new_today is None in the Limits object (and not Some(0) as on the first save).
If you fix the overflow by doing something like limit.today = limit.today.min(today.max(1) - 1);, you are now stuck: you can not change the New cards/day to Preset, you are stuck on the Today only.

On the 24.06.3 there is the same issue (without panic - only the Today only hardlock/softlock):

24_06_3_Demo.mp4

@dae, can you please investigate it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions