Skip to content

ximgproc: stop RICInterpolator using the global rand() - #4278

Open
pctablet505 wants to merge 2 commits into
opencv:4.xfrom
pctablet505:fix-ximgproc-ric-global-rand
Open

pctablet505 wants to merge 2 commits into
opencv:4.xfrom
pctablet505:fix-ximgproc-ric-global-rand

Conversation

@pctablet505

@pctablet505 pctablet505 commented Nov 5, 2026 •

Copy link
Copy Markdown

Resolves #4260

InterpolatorTest.RICSameResultOnAnyNumberOfThreads fails now and then on the macOS CI jobs, also in PRs that do not touch ximgproc. The reason: PropagateModels() takes its random numbers from the C library rand(), which the whole process shares. This PR gives it its own cv::RNG, as EdgeAwareInterpolator in the same file already has.

Why the test is flaky

  • PropagateModels() calls srand(0), then draws with rand().
  • A rand() call from anywhere else in the process takes a number out of that sequence, and the flow changes.
  • On Linux, one extra rand() call anywhere in the first 2000 draws moves the flow by exactly 1.45967 pixel.

The macOS failures look like that. Each platform always fails with the same value: 1.81992 on macOS-ARM64 (job) and 0.266933 on macOS-X64 (job). Other runs in the same job pass.

I don't know what calls rand() on macOS. On Linux I could not make the test fail with more threads, under load, with ThreadSanitizer, or with a pattern-filled heap.

srand(0) also reset the generator for the rest of the application.

Fix

PropagateModels() creates RNG rng(0) and passes it to HypothesisGeneration().

Test

New InterpolatorTest.RICResultDoesNotDependOnRand: the flow must not change while another thread calls rand() in a loop. It fails on 4.x and passes with this PR. The other ximgproc tests still pass, and RICReferenceAccuracy stays within its limit.

I tested on Linux x86-64 only, so the macOS CI jobs are the real check.

Related

#4249 and #4256 fix other RICInterpolator bugs in the same file. All three merge cleanly with each other.

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

PropagateModels() took its random numbers from rand(), so a rand() call
made by anything else in the process changed the flow, and its srand(0)
reset the generator for the rest of the application. Use a local cv::RNG,
as EdgeAwareInterpolator in the same file does.
…test even on exceptions

Use scope guards, so an exception from interpolate() cannot leave the
thread count at 1 for later tests or destroy a running std::thread.
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.

1 participant