Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/osm_base_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* This file is part of pyosmium. (https://osmcode.org/pyosmium/)
*
* Copyright (C) 2024 Sarah Hoffmann <lonvia@denofr.de> and others.
* Copyright (C) 2026 Sarah Hoffmann <lonvia@denofr.de> and others.
* For a full list of authors see the git log.
*/
#ifndef PYOSMIUM_OSM_BASE_OBJECTS_HPP
Expand Down Expand Up @@ -73,7 +73,6 @@ class PyOSMObject
pybind11::object get_or_create_python_object()
{
if (!m_valid) {
m_valid = true;
if constexpr (std::is_same_v<T, osmium::Node>) {
m_pyobj = pybind11::module_::import("osmium.osm.types").attr("Node")(COSMNode{m_obj});
} else if constexpr (std::is_same_v<T, osmium::Way>) {
Expand All @@ -85,6 +84,7 @@ class PyOSMObject
} else if constexpr (std::is_same_v<T, osmium::Changeset>) {
m_pyobj = pybind11::module_::import("osmium.osm.types").attr("Changeset")(COSMChangeset{m_obj});
}
m_valid = true;
}

return m_pyobj;
Expand Down
54 changes: 54 additions & 0 deletions test/test_constructor_interruption.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# This file is part of pyosmium. (https://osmcode.org/pyosmium/)
# Copyright (C) 2026 Sarah Hoffmann <lonvia@denofr.de> and others.
# For a full list of authors see the git log.
import multiprocessing
import sys

import pytest
import osmium


def run_interrupted_read(pipe, object_type, opl, exception_type):
constructor = object_type.__init__.__code__

def fail_constructor(frame, event, arg):
if event == 'call' and frame.f_code is constructor:
sys.settrace(None)
raise exception_type('injected constructor failure')
return fail_constructor

sys.settrace(fail_constructor)
try:
list(osmium.FileProcessor(osmium.io.FileBuffer(opl.encode('utf-8'), 'opl')))
except exception_type:
sys.settrace(None)
pipe.send(str(exception_type))
else:
pipe.send('constructor was not reached')


@pytest.mark.thread_unsafe
@pytest.mark.parametrize('object_type', [osmium.osm.Node, osmium.osm.Way, osmium.osm.Relation])
@pytest.mark.parametrize('exception', [KeyboardInterrupt, MemoryError, RuntimeError])
def test_constructor_exception_does_not_crash_cleanup(to_opl, object_type, exception):
data = to_opl("""\
n1 x5 y5
w1 Nn1,n2
r1 Mn1@
""")

parent_conn, child_conn = multiprocessing.Pipe()
ctx = multiprocessing.get_context('spawn')
p = ctx.Process(target=run_interrupted_read,
args=[child_conn, object_type, data, exception])
p.start()

assert parent_conn.poll(2)
assert parent_conn.recv() == str(exception)

p.join(2)

assert p.exitcode == 0, f"Segmentation fault ({-p.exitcode})" if p.exitcode < 0 \
else f"Test exited with error {p.exitcode}"
Loading