Skip to content

[ESSREDUCE] Avoid thread-unsafe warning suppression in unwrap _polygon_intersections#657

Merged
SimonHeybrock merged 1 commit into
mainfrom
fix-allnan-warning-threadsafe
Jun 30, 2026
Merged

[ESSREDUCE] Avoid thread-unsafe warning suppression in unwrap _polygon_intersections#657
SimonHeybrock merged 1 commit into
mainfrom
fix-allnan-warning-threadsafe

Conversation

@SimonHeybrock

Copy link
Copy Markdown
Member

Fixes #656.

_polygon_intersections reduces the per-bound interpolated wavelengths with np.nanmin/np.nanmax. An all-NaN column is the expected case of a time bin that no subframe covers, so those reductions legitimately emit numpy's "All-NaN slice encountered" RuntimeWarning. Suppressing it required warnings.catch_warnings, whose process-global filter state is not thread-safe; under sciline's default dask threaded scheduler, concurrent providers reaching this function race on that state and the warning intermittently leaks. Downstream projects that treat warnings as errors (e.g. esslivedata's wavelength-LUT workflow) then fail flakily.

Reducing with the fmin/fmax ufuncs directly avoids the problem: they ignore NaNs and yield NaN only for fully-NaN columns — exactly what nanmin/nanmax compute internally, minus the all-NaN check that emits the warning. No suppression is needed (the warnings import is dropped), and it is 2-5x faster as it skips nanmin's NaN-masking copy.

Benchmark (y of shape bounds×x, ~20% scattered NaNs + ~15% all-NaN columns):

shape nanmin/nanmax (+catch_warnings) fmin.reduce/fmax.reduce
4×1000 23.8 µs 5.4 µs
8×1000 19.6 µs 6.1 µs
4×5000 33.3 µs 14.3 µs
20×2000 32.7 µs 17.8 µs

Results are identical to the previous code (NaN for all-NaN columns, NaN-ignoring min/max otherwise).

Test plan

  • Added a regression test computing _polygon_intersections over uncovered (all-NaN) columns under warnings.simplefilter("error").
  • tests/unwrap passes (141 tests).

@github-actions github-actions Bot added the essreduce Issues for essreduce. label Jun 30, 2026
@github-actions github-actions Bot changed the title Avoid thread-unsafe warning suppression in unwrap _polygon_intersections [ESSREDUCE] Avoid thread-unsafe warning suppression in unwrap _polygon_intersections Jun 30, 2026
nanmin/nanmax emit an "All-NaN slice encountered" RuntimeWarning for the
expected case of a time bin no subframe covers. Suppressing it required
warnings.catch_warnings, whose process-global filter state is not thread-safe
and races when the unwrap pipeline runs under a multi-threaded scheduler,
intermittently leaking the warning (a flaky failure for downstream projects
that treat warnings as errors).

Reduce with the fmin/fmax ufuncs directly instead: they ignore NaNs and yield
NaN only for fully-NaN columns, which is what nanmin/nanmax compute internally
minus the all-NaN check that emits the warning. No suppression needed, and it
is 2-5x faster as it drops nanmin's NaN-masking copy.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@SimonHeybrock SimonHeybrock force-pushed the fix-allnan-warning-threadsafe branch from 9c24017 to 2a883e1 Compare June 30, 2026 09:18
@SimonHeybrock SimonHeybrock marked this pull request as ready for review June 30, 2026 09:23
@SimonHeybrock SimonHeybrock requested a review from nvaytet June 30, 2026 09:23
@SimonHeybrock SimonHeybrock added this pull request to the merge queue Jun 30, 2026
Merged via the queue into main with commit 8cb8459 Jun 30, 2026
24 checks passed
@SimonHeybrock SimonHeybrock deleted the fix-allnan-warning-threadsafe branch June 30, 2026 12:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

essreduce Issues for essreduce.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Thread-unsafe warning suppression in unwrap _polygon_intersections leaks under dask threaded scheduler

2 participants