Skip to content

Conversation

@unlimitedsola
Copy link
Contributor

@unlimitedsola unlimitedsola commented Jul 26, 2025

Motivation

When max_files set to zero, rolling.rs:695 will cause integer underflow and panicking in debug build. In my opinion the API should prevent this from happening as "zero" is a valid usize value to pass in.

Solution

Currently, in release builds, if max_files is set to zero, it behaves just like u64::MAX on x86_64 platforms. Therefore, I decided to simply make zero == None, which should align with the existing behavior but without the panicking. I believe this can be considered as "non-breaking".

Feel free to advise if there a more preferred way of fixing this. Any help on how to provide tests would also be appreciated!

@unlimitedsola unlimitedsola requested a review from a team as a code owner July 26, 2025 10:52
Copy link
Contributor

@hds hds left a comment

Choose a reason for hiding this comment

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

Thanks for your change! It's a good idea, I think we should document the special meaning of 0 in the function docs.

pub fn max_log_files(self, n: usize) -> Self {
Self {
max_files: Some(n),
max_files: Some(n).filter(|&n| n > 0),
Copy link
Contributor

Choose a reason for hiding this comment

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

We should also add a comment stating that setting n to 0 will disable the max files (effectively make it infinite).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for your feedback! I've updated the documentation and added comment to the line.

@unlimitedsola unlimitedsola force-pushed the max-files-underflow branch 2 times, most recently from 862c784 to ac5d5af Compare November 20, 2025 17:42
Copy link
Contributor

@hds hds left a comment

Choose a reason for hiding this comment

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

Looks good. Thank you!

@hds hds added kind/bug Something isn't working crate/appender Related to the `tracing-appender` crate. labels Nov 22, 2025
@hds hds changed the title fix: max_files integer underflow when set to zero appender: fix max_files integer underflow when set to zero Nov 22, 2025
@hds hds merged commit adbd8a4 into tokio-rs:main Nov 22, 2025
55 checks passed
hds added a commit that referenced this pull request Nov 25, 2025
# 0.2.4 (November 30, 2025)

### Added

- Prune old files at startup ([#2966])
- Add fallback to file creation date ([#3000])

### Fixed

- Fix `max_files` integer underflow when set to zero ([#3348])

### Documented

- Update tracing-appender docs link to correct docs.rs URL ([#3325])

[#2966]: https://github.com/tokio-rs/tracing/pull/#2966
[#3000]: https://github.com/tokio-rs/tracing/pull/#3000
[#3325]: https://github.com/tokio-rs/tracing/pull/#3325
[#3348]: https://github.com/tokio-rs/tracing/pull/#3348
hds added a commit that referenced this pull request Nov 26, 2025
# 0.2.4 (November 30, 2025)

### Added

- Prune old files at startup ([#2966])
- Add fallback to file creation date ([#3000])
- Introduce weekly rotation ([#3218])

### Fixed

- Fix `max_files` integer underflow when set to zero ([#3348])

### Documented

- Update tracing-appender docs link to correct docs.rs URL ([#3325])

[#2966]: https://github.com/tokio-rs/tracing/pull/#2966
[#3000]: https://github.com/tokio-rs/tracing/pull/#3000
[#3218]: https://github.com/tokio-rs/tracing/pull/#3218
[#3325]: https://github.com/tokio-rs/tracing/pull/#3325
[#3348]: https://github.com/tokio-rs/tracing/pull/#3348
hds added a commit that referenced this pull request Nov 26, 2025
# 0.2.4 (November 26, 2025)

### Added

- Prune old files at startup ([#2966])
- Add fallback to file creation date ([#3000])
- Introduce weekly rotation ([#3218])

### Fixed

- Fix `max_files` integer underflow when set to zero ([#3348])

### Documented

- Update tracing-appender docs link to correct docs.rs URL ([#3325])

[#2966]: https://github.com/tokio-rs/tracing/pull/#2966
[#3000]: https://github.com/tokio-rs/tracing/pull/#3000
[#3218]: https://github.com/tokio-rs/tracing/pull/#3218
[#3325]: https://github.com/tokio-rs/tracing/pull/#3325
[#3348]: https://github.com/tokio-rs/tracing/pull/#3348

# Please enter the commit message for your changes. Lines starting
# with '#' will be kept; you may remove them yourself if you want to.
# An empty message aborts the commit.
#
# Date:      Tue Nov 25 12:06:02 2025 +0100
#
# On branch hds/tracing-appender-0.2.4
# Your branch is up to date with 'origin/hds/tracing-appender-0.2.4'.
#
# Changes to be committed:
#	modified:   tracing-appender/CHANGELOG.md
#	modified:   tracing-appender/Cargo.toml
#	modified:   tracing-appender/README.md
#
# Untracked files:
#	backport-2025-01.txt
#	backport-2025-05.txt
#	backup-Cargo.lock
#	commit-msg
#	dingxiangfei-patch2.diff
#	justfile
#	tracing_subscriber.d
#
hds added a commit that referenced this pull request Nov 26, 2025
# 0.2.4 (November 26, 2025)

### Added

- Prune old files at startup ([#2966])
- Add fallback to file creation date ([#3000])
- Introduce weekly rotation ([#3218])

### Fixed

- Fix `max_files` integer underflow when set to zero ([#3348])

### Documented

- Update tracing-appender docs link to correct docs.rs URL ([#3325])

[#2966]: https://github.com/tokio-rs/tracing/pull/#2966
[#3000]: https://github.com/tokio-rs/tracing/pull/#3000
[#3218]: https://github.com/tokio-rs/tracing/pull/#3218
[#3325]: https://github.com/tokio-rs/tracing/pull/#3325
[#3348]: https://github.com/tokio-rs/tracing/pull/#3348
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

crate/appender Related to the `tracing-appender` crate. kind/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants