Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid panic in Itertools::format_with #108

Merged
merged 1 commit into from
Feb 10, 2025

Conversation

jrray
Copy link
Contributor

@jrray jrray commented Feb 6, 2025

Run this new test by itself with cargo test test_tracing.

When tracing is configured with multiple output channels, it is possible for the tracing::debug! macro to call fmt multiple times on its arguments.

format_with is documented to panic if formatted more than once. https://docs.rs/itertools/latest/itertools/trait.Itertools.html#method.format_with

The panic can be avoided by adding .to_string() to the end of the use of format_with around line 569 of solver/mod.rs, but this may not be the best fix.

This test sets a global default which conflicts with tracing_test, so this new test doesn't play nice with the other tests when running the whole test suite.

Edit:

Test removed and the to_string fix has been put in place instead.

@jrray
Copy link
Contributor Author

jrray commented Feb 6, 2025

Oops didn't mean to reformat Cargo.toml. This PR is not intended to be merged anyway.

@baszalmstra
Copy link
Contributor

I had to read it a couple of times but now it makes sense to me. I would be fine adding to_string at the end! To me the most important thing is that we do not pay for allocate the string if the log is also not going to be visible. I think adding to_string behind the format_with achieves this though.

Would you mind implementing the fix too?

@jrray
Copy link
Contributor Author

jrray commented Feb 7, 2025

Yes, I'm happy to. I have been running with that change already in an internal build and it's been fine.

I just don't know how to implement a test for this problem that won't interfere with the other tests.

Would you prefer I modify this PR to add the fix (and remove the test) or submit a new PR?

@baszalmstra
Copy link
Contributor

Awesome, feel free to modify this pr

When tracing is configured with multiple output layers, the argument to
`tracing::debug!` may be evaluated multiple times. Since
`Itertools::format_with` panics if `fmt` is called on it more than once,
use `to_string` to materialize the value into something safe to format
multiple times.
@jrray jrray force-pushed the format-with-panic branch from 465798c to 3c50199 Compare February 10, 2025 01:03
@jrray jrray changed the title Demonstrate panic using format_with Avoid panic in Itertools::format_with Feb 10, 2025
@jrray
Copy link
Contributor Author

jrray commented Feb 10, 2025

Awesome, feel free to modify this pr

PR has been updated. Let me know if it needs anything else.

@baszalmstra baszalmstra enabled auto-merge (squash) February 10, 2025 04:18
@baszalmstra baszalmstra merged commit d62f208 into prefix-dev:main Feb 10, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants