Is MapServer Thread-safe?
Up to table of contentsQ: Is MapServer thread-safe?
A: Generally, no (but see the next question). Many components of MapServer use static or global data that could potentially be modified by another thread. Under heavy load these unlikely events become inevitable, and could result in sporadic errors.
Q: Is it possible to safely use any of MapServer in a multi-threaded application?
A: Some of it, yes, with care. Or with Python :) Programmers must either avoid using the unsafe components of MapServer or carefully place locks around them. Python's global interpreter lock immunizes against MapServer threading problems; since no mapscript code ever releases the GIL all mapscript functions or methods are effectively atomic. Users of mapscript and Java, .NET, mod_perl, or mod_php do not have this extra layer of protection.
A: Which components are to be avoided?
Q: Below are lists of unsafe and unprotected components and unsafe but locked components.
Unsafe:
- OGR layers: use unsafe CPL services
- Cartoline rendering: static data
- Imagemap output: static data
- SWF output: static data and use of unsafe msGetBasename()
- SVG output: static data
- WMS/WFS server: static data used for state of dispatcher
- Forcing a temporary file base (an obscure feature): static data
- MyGIS: some static data
Unsafe, but locked:
- Map config file loading: global parser
- Setting class and and layer filter expressions (global parser)
- Class expression evaluation (global parser)
- Setting map and layer projections (PROJ)
- Raster layer rendering and querying (GDAL)
- Database Connections (mappool.c)
- PostGIS support
- Oracle Spatial (use a single environment handle for connection)
- SDE support (global layer cache)
- Error handling (static repository of the error objects)
- WMS/WFS client connections: potential race condition in Curl initialization
- Plugin layers (static repository of the loaded dll-s)
Rather coarse locks are in place for the above. Only a single thread can use the global parser at a time, and only one thread can access GDAL raster data at a time. Performance is exchanged for safety.
This FAQ applies to: MapServer 4.6, MapServer 4.8
MapScript for web clients
Above answer need some examples of code (e.g. C# with mapscript_csharp lib)
We have this kind of situation.
We have ASP.NET application and we are using mapscript_csharp dll.
Each of browser clients will have its own map object instance stored in session cache. So each client request is creating new thread on server.
So, new client must wait for its map object if mapscript is just creating map object for other client (other thread), even if our server has many processors?
Becouse of Map config file loading its global parser and it is locked.
I Am wrong?
The same situation after some time. Two clients (two threads on server) requests for image calling map.draw(). This is unsafe situation becouse of 'some static data in Imagemap output'?