pola-rs/polars · error

invalid value for POLARS_MAX_CONCURRENT_SCANS

Error message

invalid value for POLARS_MAX_CONCURRENT_SCANS: {x}

What it means

Polars panics when POLARS_MAX_CONCURRENT_SCANS is set but not parseable as a non-zero usize. calc_max_concurrent_scans uses it to override the default limit of concurrent scans, which is otherwise min(num_pipelines, num_sources).clamp(1, 128).

Solutions

  1. Set the variable to a plain positive integer (1-128 is the sane range).
  2. Unset it to use the automatic clamp of min(num_pipelines, num_sources) to [1, 128].
  3. Validate with a quick shell check: case "$POLARS_MAX_CONCURRENT_SCANS" in ''|*[!0-9]*) unset ...;; esac or equivalent.

Example fix

// before
POLARS_MAX_CONCURRENT_SCANS=1.5
// after
POLARS_MAX_CONCURRENT_SCANS=32
Defensive patterns

Strategy: validation

Validate before calling

const v = process.env.POLARS_MAX_CONCURRENT_SCANS;
if (v !== undefined && (!/^\d+$/.test(v.trim()) || parseInt(v, 10) === 0 || parseInt(v, 10) > 128)) {
  throw new Error(`POLARS_MAX_CONCURRENT_SCANS must be an integer in 1..=128, got: '${v}'`);
}

Type guard

function isValidConcurrentScans(v) { const n = Number(v); return v === undefined || (Number.isInteger(n) && n >= 1 && n <= 128); }

Try / catch

try {
  runMultiFileScan();
} catch (e) {
  if (String(e).includes('POLARS_MAX_CONCURRENT_SCANS')) {
    delete process.env.POLARS_MAX_CONCURRENT_SCANS;
    runMultiFileScan();
  } else throw e;
}

Prevention

When it happens

Trigger: Running a multi-file streaming scan with the variable set to '', '0', '1.5', '128 scans', or any non-numeric / out-of-range value.

Common situations: Ops teams exporting the value with units or decimals from a capacity-planning doc; empty values in Helm/env manifests.

Understand the failure class

Background: "is not a valid" / "Invalid ... value" environment variable errors: how libraries validate env vars and what to do when they reject yours — this error's family across 48 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/35035b8dc986474d. Report an issue: GitHub.

Appendix: source

Thrown at crates/polars-stream/src/nodes/io_sources/multi_scan/functions/mod.rs:46

    let max_files_with_slice = match pre_slice {
        // Calculate the max number of files assuming 1 row per file.
        Some(v @ Slice::Positive { .. }) => v.end_position().max(1),
        Some(Slice::Negative { .. }) | None => usize::MAX,
    };

    // Set this generously high, there are users who scan 10,000's of small files from the cloud.
    num_pipelines
        .saturating_add(3)
        .min(max_files_with_slice)
        .min(num_sources)
        .clamp(1, 128)
}

pub fn calc_max_concurrent_scans(num_pipelines: usize, num_sources: usize) -> usize {
    if let Ok(v) = std::env::var("POLARS_MAX_CONCURRENT_SCANS").map(|x| {
        x.parse::<NonZeroUsize>()
            .unwrap_or_else(|_| panic!("invalid value for POLARS_MAX_CONCURRENT_SCANS: {x}"))
            .get()
    }) {
        return v;
    }

    num_pipelines.min(num_sources).clamp(1, 128)
}

pub async fn is_compressed_source(
    scan_source: ScanSource,
    cloud_options: Option<Arc<CloudOptions>>,
    io_metrics: Option<Arc<IOMetrics>>,
) -> PolarsResult<bool> {
    let byte_source_builder = if scan_source.is_cloud_url() || polars_config::config().force_async()
    {
        DynByteSourceBuilder::ObjectStore(FetchConfig::legacy())
    } else {
        DynByteSourceBuilder::Mmap

View on GitHub (pinned to fe841f959e)