Skip to content

defaulting CLOSE_CONNECTION=DEFER for rasters leads to large memory consumption with FCGI #7459

Open

Description

@landryb

i have a 12-process FCGI pool w/ supervisord, servicing loads of WMS requests from large COGs (eg ~150 files, most around 40/70Go, some around 250Go). that's running mapserver 8.6 on debian 13, and is used as a backend for mapproxy which serves requests to end users.

i always had a large memory consumption for the mapserver pools, eg i have a supervisor plugin (superlance/memmon) that kills a worker when it reaches 8Gb, and i have workers getting killed every hour, where i have ~50k hits a day.

so far i attributed this large memory consumption to memleaks down the line in the libraries, but found out that setting PROCESSING "CLOSE_CONNECTION=NORMAL" in each layer config stopped the large memory consumption. Measuring the memory consumption of replaying 5000 requests from a production accesslog on the 12 workers, i noted a 80Gb growth with DEFER, and negligible with NORMAL

analysing closer with lsof, i noticed that each worker accumulated open handles (because CLOSE_CONNECTION defaults to DEFER in https://github.com/MapServer/MapServer/blob/main/src/mapraster.c#L697 ?) on all the COGs in my datadirs, not giving back memory to the system (eg not calling GDALClose() ?).

so my question is, would it make sense to default CLOSE_CONNECTION to NORMAL for long-running FCGI processes, when using raster data or vector files (i guess the same issue can happen with a bunch of large shps/gpkgs..) ? for postgis layers it makes sense to default to DEFER so that the connections are kept in the pool, but for files... is there a way in the code to know at runtime that we're running as FCGI and not old-school/traditional CGI ?

Activity

  1. geographika commented on Mar 21, 2026

    @geographika
    Member

    @landryb - I don't believe there is a way to know if you are running through CGI or FastCGI.
    Based on your experience it would make sense to me that the code could be updated to default to DEFER unless explicitly set to CLOSE_CONNECTION ALWAYS for the layer.
    I'm not sure of the logic behind setting to DEFER for rasters though.

  2. landryb commented on Mar 23, 2026

    @landryb
    ContributorAuthor

    I'm not sure of the logic behind setting to DEFER for rasters though.

    i dunno, from my reading of the code history that seems to be historic. at the time, maybe it made sense if the cost of opening a raster was large, compared to having it already opened. right now, it might only make sense if you have a small amount of small rasters, less so if you have many large ones.

  3. geo856 commented on Apr 2, 2026

    @geo856

    I can confirm the problem with the high RAM usage. I compared my previous data structure to a BIGTIFF variant. The difference in RAM usage between MapServer 8.4 (Debian 13) and the previous MapServer 8.0 (Debian 12) is as follows:

    If you have 20,000 TIFF files on disk and create an index using gdaltindex, which is then included in the map file with "TILEINDEX" & TYPE RASTER, MapServer 8.4 uses up to 10 times more RAM. If I calculate a BIGTIFF with an overview from the 20,000 files and specify (PROCESSING "CLOSE_CONNECTION=NORMAL"), then the RAM usage behaves like it did previously with MapServer 8.0.

    I'm also concerned that the response times of MapServer 8.4 with HTTP Apache FCGI aren't as good as they were with MapServer 8.0.

    Is there anything else to consider when migrating MapServer from Debian 12 to Debian 13? For example, should the raster data be rewritten using the newer GDAL version?

  4. jratike80 commented on Apr 2, 2026

    @jratike80

    It seems that "deferred" was not originally planned to be used for tileindex layers
    #2815
    #3077.

    Tileindex exception seems to be there still

    if (close_connection == NULL && layer->tileindex == NULL)

    Does it mean that it does not have an effect in Mapserver 8.4 for some other reason?

    @landryb, do you have 150 separate COG layers instead of a tileindex layer? But @geo856 , you do have a tileindex layer but still suffer from high memory usage.

    Maybe someone should do a bechmark and test if there is any use case when CLOSE_CONNECTION=DEFER is actually good.

  5. landryb commented on Apr 7, 2026

    @landryb
    ContributorAuthor

    i think CLOSE_CONNECTION=DEFER makes sense for data in actual databases or local small files, less so for huge files..

    i do have 150 separate COGs for distinct areas/dates, which were all previously (eg before 2023) separate/distinct tileindex layers in postgis with bazillions of tiles/tiffs/jp2, and the memory consumption when using postgis tileindex was also large but not as large as when using COGs.

    I havent seen an actual memory consumption increase between 8.0 (debian 12) & 8.6 (debian 13 with backports)

    the benchmarks i did with CLOSE_CONNECTION=normal with my COGs didnt show a response time increase/regression, so i'm probably going to switch all my COGs to it.

  6. jratike80 commented on Apr 7, 2026

    @jratike80

    When you used the PostGIS tileindex layers, did you ever experiment if CLOSE_CONNECTION= settings on the PostGIS vector layers, which were then used as TILEINDEX source for the raster layers, had any effect on speed or memory consumption?

  7. landryb commented on Apr 7, 2026

    @landryb
    ContributorAuthor

    i don't think i've played with disabling connection pooling against databases (they're all backed by pgbouncer anyway, but still..)

  8. jratike80 commented on Apr 7, 2026

    @jratike80

    @geo856 wrote

    If you have 20,000 TIFF files on disk and create an index using gdaltindex, which is then included in the map file with "TILEINDEX" & TYPE RASTER, MapServer 8.4 uses up to 10 times more RAM. If I calculate a BIGTIFF with an overview from the 20,000 files and specify (PROCESSING "CLOSE_CONNECTION=NORMAL"), then the RAM usage behaves like it did previously with MapServer 8.0.

    and @landryb wrote

    I havent seen an actual memory consumption increase between 8.0 (debian 12) & 8.6 (debian 13 with backports)

    It feels like some well controlled systematic testing with tileindex would be needed because they are widely used. Unfortunately I do not have proper environment for testing at the moment. @geo856, was your tileindex a shapefile index or a database index?

  9. jratike80 commented on Apr 10, 2026

    @jratike80

    By the way, the documented options in https://mapserver.org/mapfile/layer.html for CLOSE_CONNECTION are
    PROCESSING "CLOSE_CONNECTION=DEFER"
    PROCESSING "CLOSE_CONNECTION=ALWAYS"

    Why did you use "NORMAL"? Have you been reading https://mapserver.org/input/vector/ogr.html or what?
    By looking at the code

    The life span indicator is controlled by the CLOSE_CONNECTION PROCESSING
    the NORMAL option indeed exists but I do not quite understand the difference between NORMAL and ALWAYS. If there is a difference, then perhaps it should be explained in the user documentation as well.

  10. landryb commented on Apr 10, 2026

    @landryb
    ContributorAuthor

    By the way, the documented options in https://mapserver.org/mapfile/layer.html for CLOSE_CONNECTION are PROCESSING "CLOSE_CONNECTION=DEFER" PROCESSING "CLOSE_CONNECTION=ALWAYS"

    Why did you use "NORMAL"? Have you been reading https://mapserver.org/input/vector/ogr.html or what? By looking at the code

    MapServer/src/mappool.c

    Line 57 in eb2d8af
    The life span indicator is controlled by the CLOSE_CONNECTION PROCESSING
    the NORMAL option indeed exists but I do not quite understand the difference between NORMAL and ALWAYS. If there is a difference, then perhaps it should be explained in the user documentation as well.

    i don't exactly remember right now, but when i looked at the code and the doc i think i concluded that 'anything but DEFER will close it'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions