Skip to content

Commit c02a8f4

Browse files
sylvestrecakebaker
authored andcommitted
sort: disable rayon on wasi
1 parent 4f39f94 commit c02a8f4

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

src/uu/sort/src/sort.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use foldhash::fast::FoldHasher;
2929
use foldhash::{HashMap, SharedSeed};
3030
use numeric_str_cmp::{NumInfo, NumInfoParseSettings, human_numeric_str_cmp, numeric_str_cmp};
3131
use rand::{RngExt as _, rng};
32+
#[cfg(not(target_os = "wasi"))]
3233
use rayon::prelude::*;
3334
use std::cmp::Ordering;
3435
use std::env;
@@ -2127,9 +2128,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
21272128
Ok(0) | Err(_) => std::thread::available_parallelism().map_or(1, NonZero::get),
21282129
Ok(n) => n,
21292130
};
2130-
let _ = rayon::ThreadPoolBuilder::new()
2131-
.num_threads(num_threads)
2132-
.build_global();
2131+
#[cfg(not(target_os = "wasi"))]
2132+
{
2133+
let _ = rayon::ThreadPoolBuilder::new()
2134+
.num_threads(num_threads)
2135+
.build_global();
2136+
}
21332137
}
21342138

21352139
if let Some(size_str) = matches.get_one::<String>(options::BUF_SIZE) {
@@ -2602,10 +2606,19 @@ fn exec(
26022606
}
26032607

26042608
fn sort_by<'a>(unsorted: &mut Vec<Line<'a>>, settings: &GlobalSettings, line_data: &LineData<'a>) {
2609+
let cmp = |a: &Line<'a>, b: &Line<'a>| compare_by(a, b, settings, line_data, line_data);
2610+
// WASI does not support threads, so use non-parallel sort to avoid
2611+
// rayon's thread pool which triggers an unreachable trap.
26052612
if settings.stable || settings.unique {
2606-
unsorted.par_sort_by(|a, b| compare_by(a, b, settings, line_data, line_data));
2613+
#[cfg(not(target_os = "wasi"))]
2614+
unsorted.par_sort_by(cmp);
2615+
#[cfg(target_os = "wasi")]
2616+
unsorted.sort_by(cmp);
26072617
} else {
2608-
unsorted.par_sort_unstable_by(|a, b| compare_by(a, b, settings, line_data, line_data));
2618+
#[cfg(not(target_os = "wasi"))]
2619+
unsorted.par_sort_unstable_by(cmp);
2620+
#[cfg(target_os = "wasi")]
2621+
unsorted.sort_unstable_by(cmp);
26092622
}
26102623
}
26112624

0 commit comments

Comments
 (0)