Skip to content

Validate maximum thread count - #2090

Open
Sushanth012 wants to merge 1 commit into
sharkdp:masterfrom
Sushanth012:fix-threads-limit-2078
Open

Validate maximum thread count#2090
Sushanth012 wants to merge 1 commit into
sharkdp:masterfrom
Sushanth012:fix-threads-limit-2078

Conversation

@Sushanth012

Copy link
Copy Markdown

Summary

Validate --threads during CLI argument parsing and reject values greater than the supported maximum (64).

Previously, excessively large values could be accepted and later trigger panics or aborts during channel allocation or thread creation. This change surfaces those cases as a normal CLI validation error instead.

Changes

  • Add a dedicated parse_num_threads parser for --threads
  • Reject values greater than MAX_NUM_THREADS (64)
  • Reuse MAX_NUM_THREADS for the default thread limit
  • Add unit tests for the parser
  • Add integration tests covering invalid --threads values
  • Document the fix in the changelog

Fixes

Closes #2078

@tmccombs tmccombs left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

While values below 64 are probably safe, I think that is probably too low for a hard-coded limit.

A fundamental problem here is that the maximum number of threads that won't result in an error is basically impossible to know.

It depends on a variety of factors, including OS, amount of available memory, ulimit settings for the current process, etc. I think something like say 2^16 is probably reasonable to use as a limit, but 64 is definitely too low. That means that on, say, a 128 core computer, you can't even use all of your cores.

Comment thread src/cli.rs
use crate::filter::OwnerFilter;
use crate::filter::SizeFilter;

const MAX_NUM_THREADS: usize = 64;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why 64?

The number of threads can be significantly higher than that. Especially on hosts with a large number of cpu cores.

Comment thread src/cli.rs
/// Set number of threads to use for searching & executing (default: number
/// of available CPU cores)
#[arg(long, short = 'j', value_name = "num", hide_short_help = true, value_parser = str::parse::<NonZeroUsize>)]
#[arg(long, short = 'j', value_name = "num", hide_short_help = true, value_parser = parse_num_threads)]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

why not use clap's built-in support for ranges?

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.

--threads accepts unsatisfiable values and panics or aborts during channel allocation and thread creation

2 participants