Skip to content

Cothread locks up in response to runtime warning from numpy #78

Open

Description

@Araneidae

The following code fails to complete and has to be interrupted with CTRL-\:

import cothread                                                                 
import numpy                                                                    
 
def ticker():                                                                   
    print('Hello')                                                              
    print(numpy.std([]))                                                        
    print('End')                                                                
    
cothread.Spawn(ticker)                                                          
cothread.Yield()                                                                
print('Bye') 

Ths generates the following output (I have elided the full paths for clarity):

Hello
.../site-packages/numpy/core/_methods.py:263: RuntimeWarning: Degrees of freedom <= 0 for slice
  keepdims=keepdims, where=where)
^\Quit (core dumped)

One interesting detail of note: only one of three warnings is generated in this case, if numpy.std([]) is computed outside of a cothread the following is printed:

.../site-packages/numpy/core/_methods.py:263: RuntimeWarning: Degrees of freedom <= 0 for slice
  keepdims=keepdims, where=where)
.../site-packages/numpy/core/_methods.py:223: RuntimeWarning: invalid value encountered in true_divide
  arrmean, rcount, out=arrmean, casting='unsafe', subok=False)
.../site-packages/numpy/core/_methods.py:254: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)
nan

This is tested for Python 3.7.2 and cothread version 2.19.1.

Activity

  1. AlexanderWells-diamond commented on Oct 8, 2025

    @AlexanderWells-diamond
    Contributor

    There appears to be something wrong with how Cothread handles Numpy's error handling. Whatever interaction is happening is causing this permanent hang. It also seems to somehow be overriding the Numpy error handling setting.

    The below program demonstrates the call inside of numpy.std() that is where the hang is actually happening. Note that we are doing numpy.seterr(all='raise'), which should make all Warnings instead convert into Exceptions. They are not, we just get a hang instead.

    import numpy
    import cothread
    from numpy._core import umath as um
    
    
    def ticker():
        print('Hello')
        numpy.seterr(all='raise')
        arrmean = numpy.zeros(1)
        um.true_divide(arrmean, 0, out=arrmean,
                        casting='unsafe', subok=False)
        print('End')
    
    cothread.Spawn(ticker)
    cothread.Yield()
    print('Bye')
    
    

    Compare this to the same program running outside of a Cothread:

    import numpy
    import cothread
    from numpy._core import umath as um
    
    numpy.seterr(all='raise')
    arrmean = numpy.zeros(1)
    arrmean = um.true_divide(arrmean, 0, out=arrmean,
                                         casting='unsafe', subok=False)
    print(arrmean)
    

    Which yields the expected Exception from Numpy:

    Traceback (most recent call last):
      File "/scratch/eyh46967/dev/cothread/test.py", line 20, in <module>
        arrmean = um.true_divide(arrmean, 0, out=arrmean,
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    FloatingPointError: invalid value encountered in divide
    
  2. Araneidae commented on Oct 9, 2025

    @Araneidae
    CollaboratorAuthor

    This seems to be a regression from Python2: I can repeat this test with dls-python and the exception is reported normally. (In my test I'm simply using numpy.std([]) as this is a little less brittle than trying to import from numpy._core, which seems to be quite version specific.)

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions