Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
OGR provider: do not try to open a new file ifwe are close to the lim…
…it of simultaneously opened files (fixes #43620)
  • Loading branch information
rouault authored and nyalldawson committed Sep 23, 2021
1 parent d69d8e5 commit 74e4c27
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/core/providers/ogr/qgsogrproviderutils.cpp
Expand Up @@ -25,6 +25,7 @@ email : nyall dot dawson at gmail dot com
#include "qgsproviderregistry.h"
#include "qgsgeopackageproviderconnection.h"
#include "qgsogrdbconnection.h"
#include "qgsfileutils.h"

#include <ogr_srs_api.h>
#include <cpl_port.h>
Expand Down Expand Up @@ -965,6 +966,23 @@ GDALDatasetH QgsOgrProviderUtils::GDALOpenWrapper( const char *pszPath, bool bUp
{
CPLErrorReset();

// Avoid getting too close to the limit of files allowed in the process,
// to avoid potential crashes such as in https://github.com/qgis/QGIS/issues/43620
// This heuristics is not perfect because during rendering, files will be opened again
// Typically for a GPKG file we need 6 file descriptors: 3 for the .gpkg,
// .gpkg-wal, .gpkg-shm for the provider, and 2 for the .gpkg + for the .gpkg-wal
// used by feature iterator for rendering
// So if we hit the limit, some layers will not be displayable.
if ( QgsFileUtils::isCloseToLimitOfOpenedFiles() )
{
#ifdef Q_OS_UNIX
QgsMessageLog::logMessage( QObject::tr( "Too many files opened (%1). Cannot open %2. You may raise the limit with the 'ulimit -n number_of_files' command before starting QGIS." ).arg( QgsFileUtils::openedFileCount() ).arg( QString( pszPath ) ), QObject::tr( "OGR" ) );
#else
QgsMessageLog::logMessage( QObject::tr( "Too many files opened (%1). Cannot open %2" ).arg( QgsFileUtils::openedFileCount() ).arg( QString( pszPath ) ), QObject::tr( "OGR" ) );
#endif
return nullptr;
}

char **papszOpenOptions = CSLDuplicate( papszOpenOptionsIn );

QString filePath( QString::fromUtf8( pszPath ) );
Expand Down

0 comments on commit 74e4c27

Please sign in to comment.