From 9775a3c3d49988373e49628f20323cf91d6627d4 Mon Sep 17 00:00:00 2001 From: Samier Merchant Date: Wed, 12 Feb 2025 20:54:01 -0500 Subject: [PATCH 1/2] Update ogrct.cpp to remove shared shared proj context (m_psLastContext), which can lead to scenarios where one thread destroys the context while another is still using it Undo https://github.com/OSGeo/gdal/commit/94ede75a44d62203bfb4b7d154d94427cacb1230, which causes segfaults when using gdal in a multi-threaded environment such as a thread worker pool. It might be the case that one thread "destroys" the context (m_psLastContext) while another thread is still using it, leading to a segfault. As you can see with the following line: m_psLastContext = OSRGetProjTLSContext(); --- ogr/ogrct.cpp | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/ogr/ogrct.cpp b/ogr/ogrct.cpp index 621a2f69f607..13e978186237 100644 --- a/ogr/ogrct.cpp +++ b/ogr/ogrct.cpp @@ -21,7 +21,6 @@ #include #include #include -#include #include "cpl_conv.h" #include "cpl_error.h" @@ -750,9 +749,6 @@ class OGRProjCT : public OGRCoordinateTransformation double dfThreshold = 0.0; - PJ_CONTEXT *m_psLastContext = nullptr; - std::thread::id m_nLastContextThreadId{}; - PjPtr m_pj{}; bool m_bReversePj = false; @@ -1265,8 +1261,7 @@ OGRProjCT::OGRProjCT(const OGRProjCT &other) m_osTargetSRS(other.m_osTargetSRS), bWebMercatorToWGS84LongLat(other.bWebMercatorToWGS84LongLat), nErrorCount(other.nErrorCount), dfThreshold(other.dfThreshold), - m_psLastContext(nullptr), - m_nLastContextThreadId(std::this_thread::get_id()), m_pj(other.m_pj), + m_pj(other.m_pj), m_bReversePj(other.m_bReversePj), m_bEmitErrors(other.m_bEmitErrors), bNoTransform(other.bNoTransform), m_eStrategy(other.m_eStrategy), m_oTransformations(other.m_oTransformations), @@ -2533,14 +2528,7 @@ int OGRProjCT::TransformWithErrorCodes(size_t nCount, double *x, double *y, /* Select dynamically the best transformation for the data, if */ /* needed. */ /* -------------------------------------------------------------------- */ - PJ_CONTEXT *ctx = m_psLastContext; - const auto nThisThreadId = std::this_thread::get_id(); - if (!ctx || nThisThreadId != m_nLastContextThreadId) - { - m_nLastContextThreadId = nThisThreadId; - m_psLastContext = OSRGetProjTLSContext(); - ctx = m_psLastContext; - } + auto ctx = OSRGetProjTLSContext(); PJ *pj = m_pj; if (!bTransformDone && !pj) From 5cf10a6b73a03e846e52fe673364acbbe9b8808a Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 17 Feb 2025 19:33:59 +0100 Subject: [PATCH 2/2] Formatting fix --- ogr/ogrct.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ogr/ogrct.cpp b/ogr/ogrct.cpp index 13e978186237..4b1ce38041c0 100644 --- a/ogr/ogrct.cpp +++ b/ogr/ogrct.cpp @@ -1261,9 +1261,9 @@ OGRProjCT::OGRProjCT(const OGRProjCT &other) m_osTargetSRS(other.m_osTargetSRS), bWebMercatorToWGS84LongLat(other.bWebMercatorToWGS84LongLat), nErrorCount(other.nErrorCount), dfThreshold(other.dfThreshold), - m_pj(other.m_pj), - m_bReversePj(other.m_bReversePj), m_bEmitErrors(other.m_bEmitErrors), - bNoTransform(other.bNoTransform), m_eStrategy(other.m_eStrategy), + m_pj(other.m_pj), m_bReversePj(other.m_bReversePj), + m_bEmitErrors(other.m_bEmitErrors), bNoTransform(other.bNoTransform), + m_eStrategy(other.m_eStrategy), m_oTransformations(other.m_oTransformations), m_iCurTransformation(other.m_iCurTransformation), m_options(other.m_options)