[Django] #30274: Segfault on iteration of GEOS Polygons/LinearRings

15 views
Skip to first unread message

Django

unread,
Mar 20, 2019, 2:57:02 PM3/20/19
to django-...@googlegroups.com
#30274: Segfault on iteration of GEOS Polygons/LinearRings
-------------------------------------+-------------------------------------
Reporter: Murray | Owner: nobody
Christopherson |
Type: Bug | Status: new
Component: GIS | Version: 2.1
Severity: Normal | Keywords: geos gis
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
**Environment Details**
Ubuntu 14.04 (trusty)
Python 3.6.8 (via package manager, unofficial repo: ppa:deadsnakes/ppa)
Django 2.1.7 (via pip)
libgeos (both 3.4.2 via apt-get, and 3.7.1 via source distribution)

First and foremost, I am aware that the OS is a bit older (hence the
unofficial installation routes of some newer dependencies), but I was
unable to find any reason these components shouldn't play nice.

**Steps to reproduce**
I have a minimal script showcasing the issue:

{{{
from django.contrib.gis.geos import Polygon

def main():
polygon = Polygon( ((0.0, 0.0), (0.0, 50.0), (50.0, 50.0), (50.0,
0.0), (0.0, 0.0)) )

for p in polygon[0]:
print('Longitude:', p[0])
print('Latitude:', p[1])

if __name__ == '__main__':
main()
}}}

**Expected Behaviour**
Output to stdout, something like:

{{{
Latitude: 0.0
Longitude: 0.0
Latitude: 0.0
Longitude: 50.0
...
}}}

**Observed Behaviour**
Output to stderr:

{{{
Segmentation fault (core dumped)
}}}

**Notes**
It should be noted, when originally observed, there was a stacktrace
(which I won't post in full, as it's mostly uWSGI functions), but the
relevant section suggested the segfault is in `GEOSCoordSeq_getSize` in
`libgeos.so`, which leads me to believe the issue may be in
https://github.com/django/django/blob/master/django/contrib/gis/geos/prototypes/coordseq.py.

Furthermore, this issue was uncovered during a Python 2 to 3 migration
attempt. The issue did not manifest in this environment:
Ubuntu 14.04 (trusty)
Python 2.7.12 (via package manager, unofficial repo: ppa:deadsnakes/ppa)
Django 1.11.18 (via pip)
libgeos 3.4.2 (via apt-get)

Lastly, I was able to find a workaround (in case anyone else encounters
this). This un-Pythonic variation seems to work:

{{{
from django.contrib.gis.geos import Polygon

def main():
polygon = Polygon( ((0.0, 0.0), (0.0, 50.0), (50.0, 50.0), (50.0,
0.0), (0.0, 0.0)) )

for i in range(len(polygon[0])):
p = polygon[0][i]
print('Longitude:', p[0])
print('Latitude:', p[1])

if __name__ == '__main__':
main()
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30274>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 20, 2019, 7:45:29 PM3/20/19
to django-...@googlegroups.com
#30274: Segfault on iteration of GEOS Polygons/LinearRings
-------------------------------------+-------------------------------------
Reporter: Murray | Owner: nobody
Christopherson |
Type: Bug | Status: closed
Component: GIS | Version: 2.1
Severity: Normal | Resolution: needsinfo

Keywords: geos gis | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => needsinfo


Comment:

Generally a segmentation fault suggests an issue outside of Django's
control as none of Django is written in C. If you identify that Django is
at fault, please reopen and give the fix.

--
Ticket URL: <https://code.djangoproject.com/ticket/30274#comment:1>

Django

unread,
Mar 20, 2019, 10:54:43 PM3/20/19
to django-...@googlegroups.com
#30274: Segfault on iteration of GEOS Polygons/LinearRings
-------------------------------------+-------------------------------------
Reporter: Murray | Owner: nobody
Christopherson |
Type: Bug | Status: closed
Component: GIS | Version: 2.1
Severity: Normal | Resolution: needsinfo

Keywords: geos gis | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

Hello Murray, in order to identify the origin of segfault I suggest you
enable [https://docs.python.org/3/library/faulthandler.html faulthandler]
and rely on `catchsegv`.

That would be `catchsegv python -X faulthandler script.py`.

--
Ticket URL: <https://code.djangoproject.com/ticket/30274#comment:2>

Django

unread,
Jan 9, 2020, 9:50:12 AM1/9/20
to django-...@googlegroups.com
#30274: Segfault on iteration of GEOS Polygons/LinearRings
-------------------------------------+-------------------------------------
Reporter: Murray | Owner: nobody
Christopherson |
Type: Bug | Status: new
Component: GIS | Version: 2.1
Severity: Normal | Resolution:

Keywords: geos gis | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Fabian Schindler):

* cc: Fabian Schindler (added)
* status: closed => new
* resolution: needsinfo =>


Comment:

I'm still experiencing this issue (Django 2.2.9), I could reproduce it
with the above script or a similar one:

{{{
# python3
Python 3.7.5 (default, Nov 20 2019, 09:21:52)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from django.contrib.gis.geos import Polygon


>>> polygon = Polygon( ((0.0, 0.0), (0.0, 50.0), (50.0, 50.0), (50.0,
0.0), (0.0, 0.0)) )
>>>
>>> for p in polygon[0]:

... print('Longitude:', p[0])
... print('Latitude:', p[1])
...
Segmentation fault
}}}

My OS details are as follows:

Ubuntu 19.10
libgeos-3.7.2/eoan (apt)
Python 3.7.5 (apt)
Django 2.2.9 (via pip)

For what version should this issue be fixed? Is it possible to backport
the fix if it is only available for Django 3?

Also, I could not find any commit related to this issue.

--
Ticket URL: <https://code.djangoproject.com/ticket/30274#comment:3>

Django

unread,
Jan 9, 2020, 12:14:08 PM1/9/20
to django-...@googlegroups.com
#30274: Segfault on iteration of GEOS Polygons/LinearRings
-------------------------------------+-------------------------------------
Reporter: Murray | Owner: nobody
Christopherson |
Type: Bug | Status: new
Component: GIS | Version: 2.1
Severity: Normal | Resolution:

Keywords: geos gis | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Claude Paroz):

I can reproduce too. Note that putting `polygon[0]` in a variable runs
fine:
{{{
>>> from django.contrib.gis.geos import Polygon


>>> polygon = Polygon( ((0.0, 0.0), (0.0, 50.0), (50.0, 50.0), (50.0,
0.0), (0.0, 0.0)) )
>>>

>>> line = polygon[0]
>>> for p in line:
... print('Longitude:', p[0])
... print('Latitude:', p[1])
Longitude: 0.0
Latitude: 0.0
etc.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30274#comment:4>

Django

unread,
Jan 10, 2020, 3:08:10 AM1/10/20
to django-...@googlegroups.com
#30274: Segfault on iteration of GEOS Polygons/LinearRings.

-------------------------------------+-------------------------------------
Reporter: Murray | Owner: nobody
Christopherson |
Type: Bug | Status: new
Component: GIS | Version: master
Severity: Normal | Resolution:
Keywords: geos gis | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* version: 2.1 => master
* stage: Unreviewed => Accepted


Comment:

Segmentation fault is raised in:
{{{
File "django/django/contrib/gis/geos/prototypes/threadsafe.py", line 47
in __call__
File "django/django/contrib/gis/geos/libgeos.py", line 154 in __call__
File "django/django/contrib/gis/geos/coordseq.py", line 157 in size
File "django/django/contrib/gis/geos/coordseq.py", line 29 in __iter__
}}}

Regression in 138a78ec8cab5e1df0c8ba2a3ee895cb756ff1ae.

--
Ticket URL: <https://code.djangoproject.com/ticket/30274#comment:5>

Django

unread,
Jan 19, 2020, 11:39:06 AM1/19/20
to django-...@googlegroups.com
#30274: Segfault on iteration of GEOS Polygons/LinearRings.
-------------------------------------+-------------------------------------
Reporter: Murray | Owner: Sergey
Christopherson | Fedoseev
Type: Bug | Status: assigned

Component: GIS | Version: master
Severity: Normal | Resolution:
Keywords: geos gis | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sergey Fedoseev):

* owner: nobody => Sergey Fedoseev
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/30274#comment:6>

Django

unread,
Jan 21, 2020, 1:26:47 PM1/21/20
to django-...@googlegroups.com
#30274: Segfault on iteration of GEOS Polygons/LinearRings.
-------------------------------------+-------------------------------------
Reporter: Murray | Owner: Sergey
Christopherson | Fedoseev
Type: Bug | Status: assigned
Component: GIS | Version: master
Severity: Normal | Resolution:
Keywords: geos gis | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sergey Fedoseev):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/12356 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/30274#comment:7>

Django

unread,
Jan 21, 2020, 4:06:42 PM1/21/20
to django-...@googlegroups.com
#30274: Segfault on iteration of GEOS Polygons/LinearRings.
-------------------------------------+-------------------------------------
Reporter: Murray | Owner: Sergey
Christopherson | Fedoseev
Type: Bug | Status: closed
Component: GIS | Version: master
Severity: Normal | Resolution: fixed
Keywords: geos gis | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"f2a725fba3abfef28503600e2fdaab6c30482e56" f2a725fb]:
{{{
#!CommitTicketReference repository=""
revision="f2a725fba3abfef28503600e2fdaab6c30482e56"
Fixed #30274 -- Prevented segmentation fault on LineString iteration.

This reverts commit 138a78ec8cab5e1df0c8ba2a3ee895cb756ff1ae and adds
a test for the regression.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30274#comment:8>

Reply all
Reply to author
Forward
0 new messages