Fatal Python error: unexpected exception during garbage collection

Issue #249 resolved
Sebastian Pölsterl created an issue

Using rpy2 2.5.3 with python 3.4.1 and R 3.1.1 under Fedora 21 I get the following message and python crashes with SIGABRT when running the attached script:

Exception ignored in: 'garbage collection'
KeyError: Trying to release object ID 33546936 while not preserved

Fatal Python error: unexpected exception during garbage collection

Current thread 0x00007f55bcf59700 (most recent call first):

Output of GDB is attached as well.

Comments (6)

  1. Laurent Gautier
    • changed status to open

    Thanks for the report (and providing a small example triggering the issue).

    I can reproduce the issue with Python 3.4. Things seem to be working fine with Python 2.7.

  2. Laurent Gautier

    There seems to be something about garbage collection happening differently between Python 2 and Python 3.

    The variable formula is the one triggering the problem, with the KeyError originating from rpy2 trying to clear it.

    I am suspecting something around the order in which the collection is happening (note that the segfault is only occurring when the script is terminating, and Python is likely calling terminators for all existing object instances).

    The following very slight modification of your script was made in the hope to change the order in which terminators are called (formula is now existing outside of the scope of fit())... and it made the issue disappear.

    from rpy2 import robjects
    
    def _fit(formula):
        robjects.r('set.seed(-1)')
        print(formula.r_repr())
    
    
    def fit(y_name):
        formula = robjects.Formula("%s ~ ." % y_name)
        print('R formula object has an rpy2 ID: %i' % formula.rid)
        _fit(formula)
        return formula
    
    
    if __name__ == '__main__':
        # calling function causes problem in garbage collector
        formula=fit('Species')
    
  3. Laurent Gautier

    An earlier garbage collection seems to also preventthe issue from happening:

    from rpy2 import robjects
    
    def _fit(formula):
        robjects.r('set.seed(-1)')
        print(formula.r_repr())
    
    
    def fit(y_name):
        formula = robjects.Formula("%s ~ ." % y_name)
        print('R formula object has an rpy2 ID: %i' % formula.rid)
        _fit(formula)
        import gc
        gc.collect()
    
    
    if __name__ == '__main__':
        # calling function causes problem in garbage collector
        fit('Species')
    
  4. Laurent Gautier

    Oddly, the evaluation of set.seed(1) (where formula is not used at all) is also controlling whether the bug is occurring or not.

    For example, the following is no longer crashing.

    from rpy2 import robjects
    
    def _fit(formula):
        robjects.baseenv['set.seed'](-1)
        print(formula.r_repr())
    
    def fit(y_name):
        formula = robjects.Formula("%s ~ ." % y_name)
        print('R formula object has an rpy2 ID: %i' % formula.rid)
        _fit(formula)
    
    
    if __name__ == '__main__':
        # calling function causes problem in garbage collector
        fit('Species')
    
  5. Log in to comment