Opened 6 years ago

Closed 6 years ago

#29543 closed Bug (fixed)

CPointerBase.__del__() crashes with ImportError

Reported by: Evandro Myller Owned by: Mushtaq Ali
Component: GIS Version: 2.0
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I think something is wrong when GeoDjango tries to cleanup a pointer before the program exits.

I noticed this while writing some tests; everything goes just fine if all tests pass, but I'm getting this whenever pytest exits with a failure:

============================================== FAILURES ==============================================
_________________________________________TestManagerGetQueryset.test_address ___________________________________________
self = <unit.models.test_address.TestManagerGetQueryset object at 0x7f740aee0828>, address = <Address: ...>

    def test_address(self, address):
>       raise
E       RuntimeError: No active exception to reraise

tests/unit/models/test_address.py:14: RuntimeError
======================================================================================================== 1 failed, 1 error in 3.82 seconds ========================================================================================================
Exception ignored in: <bound method CPointerBase.__del__ of <Point object at 0x7f740afb2b38>>
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/contrib/gis/ptr.py", line 36, in __del__
  File "/usr/local/lib/python3.6/site-packages/django/contrib/gis/geos/libgeos.py", line 155, in __call__
  File "/usr/local/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
  File "/usr/local/lib/python3.6/site-packages/django/contrib/gis/geos/libgeos.py", line 159, in func
ImportError: sys.meta_path is None, Python is likely shutting down
Exception ignored in: <bound method CPointerBase.__del__ of <Polygon object at 0x7f740afb2808>>
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/contrib/gis/ptr.py", line 36, in __del__
  File "/usr/local/lib/python3.6/site-packages/django/contrib/gis/geos/libgeos.py", line 155, in __call__
  File "/usr/local/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
  File "/usr/local/lib/python3.6/site-packages/django/contrib/gis/geos/libgeos.py", line 159, in func
ImportError: sys.meta_path is None, Python is likely shutting down
Exception ignored in: <bound method CPointerBase.__del__ of <Polygon object at 0x7f740afb2cd0>>
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/contrib/gis/ptr.py", line 36, in __del__
  File "/usr/local/lib/python3.6/site-packages/django/contrib/gis/geos/libgeos.py", line 155, in __call__
  File "/usr/local/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
  File "/usr/local/lib/python3.6/site-packages/django/contrib/gis/geos/libgeos.py", line 159, in func
ImportError: sys.meta_path is None, Python is likely shutting down

This is how I'm able to patch it:

import pytest
from django.contrib.gis.ptr import CPointerBase


@pytest.fixture(autouse=True, scope="session")
def bad_del_fix():
    """
    Fix a bad __del__ happening at GeoDjango
    """
    original_del = CPointerBase.__del__

    def patched_del(self):
        try:
            original_del(self)
        except ImportError:
            pass

    CPointerBase.__del__ = patched_del

I'm not really sure if the fix is just add ImportError to the exception list it expects for at https://github.com/django/django/blob/2.0/django/contrib/gis/ptr.py#L37.

Change History (8)

comment:1 by Claude Paroz, 6 years ago

Easy pickings: set
Triage Stage: UnreviewedAccepted

I think your suggested fix makes sense.

comment:2 by Mushtaq Ali, 6 years ago

Evandro Myller are you working on this?

comment:3 by Mushtaq Ali, 6 years ago

Has patch: set
Owner: changed from nobody to Mushtaq Ali
Status: newassigned

I have submitted the fix here https://github.com/django/django/pull/10130

comment:4 by Tim Graham, 6 years ago

Easy pickings: unset
Needs tests: set
Summary: Bad __del__ code in GeoDjangoCPointerBase.__del__() crashes with ImportError

Can you add a regression test?

comment:5 by Mushtaq Ali, 6 years ago

Sure.

in reply to:  1 comment:6 by Evandro Myller, 6 years ago

Replying to Claude Paroz:

I think your suggested fix makes sense.

I was hoping there was something else to fix the underlying issue, but that works for me.

comment:7 by Mushtaq Ali, 6 years ago

Tim I have added the test.

comment:8 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: assignedclosed

In 66b6b689:

Fixed #29543 -- Fixed CPointerBase.del() ImportError crash.

Note: See TracTickets for help on using tickets.
Back to Top