Skip to content

Flaky test_wrap_around on MacOS-15 ARM and Intel #7964

Closed

Description

@lagru
Error

https://github.com/scikit-image/scikit-image/actions/runs/19601935645/job/56134728902#step:8:302

____________________________ test_wrap_around[3-2] _____________________________

ndim = 3, axis = 2

    @skipif(
        sys.version_info[:2] == (3, 4),
        reason="Doesn't work with python 3.4. See issue #3079",
    )
    @testing.parametrize("ndim, axis", dim_axis)
    def test_wrap_around(ndim, axis):
>       check_wrap_around(ndim, axis)

__warningregistry__ = {}
axis       = 2
ndim       = 3

tests/skimage/restoration/test_unwrap.py:138: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

ndim = 3, axis = 2

    def check_wrap_around(ndim, axis):
        # create a ramp, but with the last pixel along axis equalling the first
        elements = 100
        ramp = np.linspace(0, 12 * np.pi, elements)
        ramp[-1] = ramp[0]
        image = ramp.reshape(tuple([elements if n == axis else 1 for n in range(ndim)]))
        image_wrapped = np.angle(np.exp(1j * image))
    
        index_first = tuple([0] * ndim)
        index_last = tuple([-1 if n == axis else 0 for n in range(ndim)])
        # unwrap the image without wrap around
        # We do not want warnings about length 1 dimensions
        with expected_warnings([r'Image has a length 1 dimension|\A\Z']):
            image_unwrap_no_wrap_around = unwrap_phase(image_wrapped, rng=0)
        print(
            'endpoints without wrap_around:',
            image_unwrap_no_wrap_around[index_first],
            image_unwrap_no_wrap_around[index_last],
        )
        # without wrap around, the endpoints of the image should differ
        assert_(
            abs(
                image_unwrap_no_wrap_around[index_first]
                - image_unwrap_no_wrap_around[index_last]
            )
            > np.pi
        )
        # unwrap the image with wrap around
        wrap_around = [n == axis for n in range(ndim)]
        # We do not want warnings about length 1 dimensions
        with expected_warnings([r'Image has a length 1 dimension.|\A\Z']):
            image_unwrap_wrap_around = unwrap_phase(image_wrapped, wrap_around, rng=0)
        print(
            'endpoints with wrap_around:',
            image_unwrap_wrap_around[index_first],
            image_unwrap_wrap_around[index_last],
        )
        # with wrap around, the endpoints of the image should be equal
>       assert_almost_equal(
            image_unwrap_wrap_around[index_first], image_unwrap_wrap_around[index_last]
        )
E       AssertionError: 
E       Arrays are not almost equal to 7 decimals
E        ACTUAL: np.float64(-31.41592653589793)
E        DESIRED: np.float64(6.283185307179586)

__warningregistry__ = {}
axis       = 2
elements   = 100
image      = array([[[ 0.        ,  0.38079911,  0.76159822,  1.14239733,
          1.52319644,  1.90399555,  2.28479466,  2.665593...    35.03351808, 35.41431719, 35.7951163 , 36.1759154 ,
         36.55671451, 36.93751362, 37.31831273,  0.        ]]])
image_unwrap_no_wrap_around = array([[[-18.84955592, -18.46875681, -18.0879577 , -17.70715859,
         -17.32635948, -16.94556037, -16.56476126, -1...18396215,  16.56476126,  16.94556037,  17.32635948,
          17.70715859,  18.0879577 ,  18.46875681,  18.84955592]]])
image_unwrap_wrap_around = array([[[-31.41592654, -31.03512743, -30.65432832, -30.27352921,
         -29.8927301 , -29.51193099, -29.13113188, -2...61759154,   3.99839065,   4.37918976,   4.75998887,
           5.14078798,   5.52158709,   5.9023862 ,   6.28318531]]])
image_wrapped = array([[[ 0.00000000e+00,  3.80799110e-01,  7.61598219e-01,
          1.14239733e+00,  1.52319644e+00,  1.90399555e+00...90399555e+00, -1.52319644e+00,
         -1.14239733e+00, -7.61598219e-01, -3.80799110e-01,
          0.00000000e+00]]])
index_first = (0, 0, 0)
index_last = (0, 0, -1)
ndim       = 3
ramp       = array([ 0.        ,  0.38079911,  0.76159822,  1.14239733,  1.52319644,
        1.90399555,  2.28479466,  2.66559377, ...271897, 35.03351808, 35.41431719, 35.7951163 ,
       36.1759154 , 36.55671451, 36.93751362, 37.31831273,  0.        ])
wrap_around = [False, False, True]

I think I've seen this test being flaky before. For example see #3079 and #3237.

Activity

  1. added
    💻 Arch specific
    on Nov 28, 2025
  2. lagru commented on Nov 28, 2025

    @lagru
    MemberAuthor

    I'm unsure how to properly debug this without convenient access to that arch. :/

  3. matthew-brett commented on Dec 10, 2025

    @matthew-brett
    Contributor

    I've tried reproducing the build from https://github.com/scikit-image/scikit-image/actions/runs/19970158590/job/57272587215. I have the same version of Python (3.13.9) and the same Pip packages, and am using the same commit of Scikit-image, running on MacOS Tahoe (26.1), on an M2 chip. The CI run was on the macos-15-arm64 image, giving MacOS 15.7.2.

    I've run pytest tests/skimage/restoration/test_unwrap.py over 2500 times without error.

    However, I have run the full test suite, and generated the error twice, once after a few iterations, and once after 510 iterations. So it looks like a rare (say 1 time in 300 or so) error, from the full test suite only - the worst kind!

    I suspect the exact environment is not important, but the platform is. I'll revert to my usual environment, and try repeated test runs on that. I'll do the same for Intel, expecting that won't generate the error.

  4. lagru commented on Dec 10, 2025

    @lagru
    MemberAuthor

    Thank you so much for investigating and reproducing this! 🙏

  5. changed the title [-]Flaky `test_wrap_around` for `restoration.unwrap_phase` on MacOS-15[/-] [+]Flaky `test_wrap_around` for `restoration.unwrap_phase` on MacOS-15-ARM[/+] on Dec 10, 2025
  6. changed the title [-]Flaky `test_wrap_around` for `restoration.unwrap_phase` on MacOS-15-ARM[/-] [+]Flaky `test_wrap_around` on MacOS-15-ARM[/+] on Dec 10, 2025
  7. matthew-brett commented on Dec 12, 2025

    @matthew-brett
    Contributor

    Perhaps there are differences between Python versions. I did replicate the error with Python 3.12, but after 1164 iterations, again, with the full test suite.

    Trying on Intel.

  8. added this to the 0.26 milestone on Dec 12, 2025
  9. matthew-brett commented on Dec 12, 2025

    @matthew-brett
    Contributor

    Yup, replicates quickly (3 iterations) on Mac Intel, with Python 3.13.

    I'll investigate further.

    In the meantime - I think it's reasonable to release - it's a very rare failure that may not occur in the wild. We have this issue to track it - I will try hard to get to the bottom of it.

  10. matthew-brett commented on Dec 14, 2025

    @matthew-brett
    Contributor

    Replicated after 11 runs again on Intel.

    No replication after 64K runs of just the individual test file on Intel, and after 120K runs on M1.

    So, definitively only replicates as part of full test run.

  11. lagru commented on Dec 16, 2025

    @lagru
    MemberAuthor

    See #3079 for earlier similar issues with this test on Python 3.4. 🤷

  12. matthew-brett commented on Dec 16, 2025

    @matthew-brett
    Contributor
  13. 5 remaining items

  14. modified the milestones: 0.26, 0.27 on Dec 18, 2025
  15. ngoldbaum commented on Feb 18, 2026

    @ngoldbaum
    Contributor

    It may be a good idea to run the scikit-image tests under ASan builds. I've found it's good at finding issues that lead to random crashes like this.

  16. ngoldbaum commented on Feb 25, 2026

    @ngoldbaum
    Contributor

    I just triggered this on my Mac locally. I'm planning to build scikit-image with sanitizers so I'll just go ahead and do that ASan pass to look at this issue.

  17. ngoldbaum commented on Feb 26, 2026

    @ngoldbaum
    Contributor

    I built scikit-image and all Python dependencies (including CPython) using address sanitizer and unfortunately didn't see any issues.

    That doesn't completely rule out heap corruption as the cause of the issue if the corruption is happening inside system libraries. I double-checked and the 2D and 3D C unwrapping code is being correctly wrapped by address sanitizer.

    However, I have run the full test suite, and generated the error twice, once after a few iterations, and once after 510 iterations. So it looks like a rare (say 1 time in 300 or so) error, from the full test suite only - the worst kind!

    I should say, if I run just this test, I eventually see failures after 47 iterations if I do spin test "tests/skimage/restoration/test_unwrap.py::test_wrap_around" in a bash for loop in my Mac.

  18. lagru commented on Mar 19, 2026

    @lagru
    MemberAuthor

    Thanks for the investigation and additional info! 😊

    A bit off-topic: I'm curious how you checked the code in question since I may want to debug similar issues at some point. Which sanitizer did you use? Anything needed besides getting the correct flags passed down somewhere in our build process?

  19. ngoldbaum commented on Mar 19, 2026

    @ngoldbaum
    Contributor

    I'm curious how you checked the code in question since I may want to debug similar issues at some point. Which sanitizer did you use? Anything needed besides getting the correct flags passed down somewhere in our build process?

    I used address sanitizer. See Quansight-Labs/free-threaded-compatibility#309 for detailed build instructions. Those instructions are for thread sanitizer, but you can do the exact same thing for address sanitizer. It's a little annoying because building SciPy needs OpenMP support, so you need to use homebrew's LLVM compilers and override the default MacOS compilers.

    The one big pain point for the LLVM sanitizers is that the entire codebase needs to be compiled with the same sanitizer runtime (e.g. the same LLVM major version). For Python projects, that means the interpreter and all native code that is getting called. Unfortunately there's no way to instrument system libraries like that, which is why I called it out above.

  20. michaelbratsch commented on Mar 20, 2026

    @michaelbratsch
    Contributor

    If I am not mistaken, use_seed is not set properly, see https://github.com/scikit-image/scikit-image/pull/8099/changes.

    There has been quite some investigation on the stability in this issue so I was wondering why we see the observed behaviour and consulted AI:

      Why only in the full test suite, not in isolation?                                                                  
      
      When run alone, the process starts fresh and rand() is in its default initial state (equivalent to srand(1)), which 
      happens to always produce a correct spanning tree for this image. In the full suite, the prior tests each call
      unwrap_phase on different images, advancing the rand() state by a variable amount depending on image size and which 
      tests ran.      
    
      Why only on macOS?                                                                                                  
      
      rand() is not thread-safe — it operates on shared global state. On Linux, rand() is typically backed by a           
      thread-local implementation in glibc, so concurrent test workers don't interfere with each other's state. On macOS,
      rand() uses genuinely shared global state across threads, so parallel test execution creates non-deterministic      
      interleaving of rand() calls between workers. This makes the accumulated state at the point test_wrap_around runs
      much less predictable on macOS, increasing the chance of hitting a bad state.
    
  21. matthew-brett commented on May 4, 2026

    @matthew-brett
    Contributor

    @michaelbratsch - thanks - that's a useful analysis. And thanks for the PR. I wonder whether we should be using something that is more thread-safe for the random numbers.

  22. ngoldbaum commented on May 4, 2026

    @ngoldbaum
    Contributor
  23. matthew-brett commented on May 4, 2026

    @matthew-brett
    Contributor

    @ngoldbaum - could you say more? Am I right in thinking you didn't address the test_unwrap tests there?

  24. ngoldbaum commented on May 4, 2026

    @ngoldbaum
    Contributor

    No, I don't think so. More pointing to there being a better pattern for this going forward in the tests.

  25. stefanv commented on May 16, 2026

    @stefanv
    Member

    We've seeded phase unwrapping, so this should be addressed. @matthew-brett please confirm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    💻 Arch specificCI failure

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions