Skip to content

grass.script: Make worker processes work with any multiprocessing start method - #8286

Open
echoix wants to merge 17 commits into
OSGeo:mainfrom
echoix:claude/laughing-lovelace-44g9uv
Open

echoix wants to merge 17 commits into
OSGeo:mainfrom
echoix:claude/laughing-lovelace-44g9uv

Conversation

@echoix

@echoix echoix commented Nov 28, 2026

Copy link
Copy Markdown
Member

Description

The RPC and message servers (grass.pygrass, grass.temporal) and the t.rast.mapcalc and t.rast.extract workers no longer force the fork start method. They use the platform default (spawn on Windows and macOS, forkserver on Linux since Python 3.14) through a context that does not fix the application's default start method. This removes the temporary workaround from #7724.

A new _environ_process() in grass.script.utils passes the session environment to the worker explicitly, because a forkserver worker otherwise gets the environment from when the fork server started. It takes the context, so the process, locks, pipes and queues always use the same one. MultiModule uses it too.

Tests:

  • The server start-method test runs with every available start method instead of being skipped (fork is skipped on macOS).
  • The GridModule tests no longer xfail with spawn, since they use a picklable function, and they check the exit code of the child process.
  • New tests check that workers see the current environment and use the given context with each start method.

Docs: the temporal framework documentation explains the if __name__ == "__main__": guard and the examples use it.

The new helpers and the tests changed with them have type annotations, checked with mypy, ty, pyrefly and zuban: no new diagnostics, and fewer than before.

Motivation and context

Python 3.14 made forkserver the default start method on Linux. #7724 ("grass.pygrass: Pin fork start method for RPC servers on Python 3.14") worked around the failure of the servers by pinning fork where it was the previous default, using a helper _get_multiprocessing_context() in grass.script.utils which is documented as a workaround until GRASS supports the spawn and forkserver start methods. This PR is that support: the servers and workers work with all start methods, so the pin is not needed anymore and the helper is replaced by one that does not force a method.

Python 3.12+ also warns when fork() is used in a multi-threaded process, as seen in the Python 3.13 CI logs (xdist workers and the RPC checker threads are multi-threaded). The fork warnings on 3.12 and 3.13 Linux remain, since fork is still the default there.

Known limits:

  • With forkserver or spawn, scripts without if __name__ == "__main__": fail, because the main module is re-imported in the worker. This was already so on Windows and macOS and is now also so on Linux with Python 3.14 and later.
  • Each forkserver worker re-imports grass.temporal, so the t.rast.mapcalc and t.rast.extract workers start slower.
  • The GridModule and t.rast.univar pool workers keep the fork server's environment, apart from GISRC, after a session switch.

How has this been tested?

On Linux, with Python 3.11.15, 3.13.18 and 3.15.2 (default start method forkserver), in grass.script, grass.temporal, grass.pygrass rpc, messages and modules tests: all pass (429 passed, 1 xfailed). The jupyter tests pass except 3 reprojection_renderer_test tests, which need cs2cs (not installed). The MultiModule doctests pass with gunittest on 3.13 and 3.15. The same tests also pass with the default start method forced to spawn and to forkserver. pygrass MultiModule, t.rast.univar, t.rast.mapcalc, t.rast.extract, jupyter SeriesMap/TimeSeriesMap and i.colors.enhance were run with all three start methods. prek passes on the changed files. Not run on Windows, macOS or the wxPython GUI.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing
    functionality to not work as before)

Checklist

  • PR title provides summary of the changes and starts with one of the
    pre-defined prefixes
  • My code follows the code style
    of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
Suggested squash commit message
grass.script: Make worker processes work with any multiprocessing start method

Stop forcing the fork start method for the RPC and message servers and
the t.rast.mapcalc and t.rast.extract workers, which was a temporary
workaround added in #7724. Use the platform default (spawn on Windows
and macOS, forkserver on Linux since Python 3.14) through a context that
does not fix the application's default start method, so applications can
still choose one.

Pass the session environment to the worker explicitly, because a
forkserver worker otherwise gets the environment from the time the fork
server started. Do the same for the MultiModule processes, and use the
same context for the process, locks, pipes and queues.

Run the server start-method test with every available start method
instead of skipping it, drop the spawn xfail of the GridModule tests by
using a picklable function, and test that workers see the current
environment. Document that scripts need a main guard with these start
methods and add type annotations to the new helpers and changed tests.

Written with the help of an AI assistant (Claude); the changes were reviewed and tested as described above.

Claude added 8 commits November 27, 2026 03:32
…rt method

Stop forcing the fork start method for the RPC and message servers and
the t.rast.mapcalc and t.rast.extract workers. Use the platform default
(spawn on Windows and macOS, forkserver on Linux since Python 3.14) and
pass the session environment to the worker explicitly, because a
forkserver worker otherwise gets the environment from the time the fork
server started.

Run the server start-method test with every available start method
instead of skipping it, drop the spawn xfail of the GridModule tests by
using a picklable function, and test that workers see the current
environment.

Written with the help of an AI assistant; the logic was reviewed and
tested with the spawn and forkserver start methods on Linux and with
Python 3.13 and 3.15.
Create the worker processes, locks, pipes and queues from a context
which uses the current default start method without setting it, so
applications can still choose a start method after using grass.pygrass
or grass.temporal. Also start the MultiModule processes with the session
environment, which is needed with the forkserver start method after the
session changed.

Skip the fork start method on macOS in the start-method test.

Written with the help of an AI assistant; tested with Python 3.13 and
3.15.
Annotate the helpers for starting worker processes and the tests
changed with them. Declare the type of RPCServerBase.server. Count a
missing exit code as zero in t.rast.mapcalc and t.rast.extract, because
the exit code of a process is optional.

Written with the help of an AI assistant; checked with mypy, ty, pyrefly
and zuban, which report no new problems and fewer than before.
Check the process returned by MultiModule.run() against BaseProcess,
because the processes of a multiprocessing context are not instances of
multiprocessing.Process. Check the exit code of the process in the
GridModule tests, since failures of the module were not reported.
Exclude fork on macOS from the start methods of the environment test.

Written with the help of an AI assistant; tested with Python 3.13 and
3.15.
…text

Scripts which use grass.temporal or the grass.pygrass servers need to run
their code under a main guard with the spawn and forkserver start
methods, which is now also needed on Linux with Python 3.14 and later.
Describe it in the temporal framework documentation and add the guard to
its examples.

Let _environ_process() take the context, so that the process, the locks,
the pipes and the queues use the same one even when the application
changes the start method in between.

Written with the help of an AI assistant; tested with Python 3.11, 3.13
and 3.15.
Without the main guard, Messenger does not raise an error, its messages
are just not printed, so the note did not cover all failures. Also call
Messenger and DataProvider classes, not tools, which in GRASS are the
r.*, v.*, etc. commands.

Written with the help of an AI assistant; tested by running scripts
without the main guard using tgis.init(), Messenger, and DataProvider
with Python 3.15 on Linux and by building the page with Sphinx.
…hod test

A message server which fails to start loses the messages without an
error, so the test passed even if the messages were not printed.

Written with the help of an AI assistant; tested with pytest on Python
3.11, 3.13, and 3.15 and checked that the test fails when no message is
sent.
The argument is already a Path, so wrapping it again is not needed and
Path is only used for type annotations.

Written with the help of an AI assistant; tested with pytest on Python
3.11 and 3.15.
@github-actions github-actions Bot added Python Related code is in Python libraries docs tests Related to Test Suite labels Nov 28, 2026
@echoix
echoix marked this pull request as ready for review November 28, 2026 22:04
@echoix

echoix commented Nov 28, 2026

Copy link
Copy Markdown
Member Author

Good news, it fixed 23+10 xfails on Windows,
23+9 xfails on macOS, and no xfail change on Ubuntu (pytest).

Before and after

Job Main PR
Windows pytest 2221 passed, 26 xfailed 2246 passed, 3 xfailed
Windows second pytest pass 36 passed, 10 xfailed 46 passed, 0 xfailed
macOS pytest 2241 passed, 24 xfailed 2269 passed, 1 xfailed
macOS second pytest pass 28 passed, 9 xfailed 37 passed, 0 xfailed
Ubuntu (3 jobs) 2267 passed, 1 xfailed 2274 passed, 1 xfailed
Details

Status: the "before" run is main (15:39) and the "after" run is the PR merge ref (19:04). On Windows, 33 xfailed grid tests now pass. I'm still comparing the macOS, Ubuntu and gunittest reports.

Short version: the PR does what it appears to be meant to do. It makes the Windows and macOS pygrass grid tests run instead of being marked xfail. I found no regression. The same Windows failures show up before and after, and they come from main.

I took the older runs (15:29 to 15:39, …1248721…) as main and the newer runs (19:04, …1249232…) as the PR merge ref. The Windows log confirms this: the old run checks out main, and the new one is at "Merge f7fb2a0 into fb9c8f4". I couldn't confirm which commit is which, because neither SHA is in my clone.

Before and after

Job Main PR
Windows pytest 2221 passed, 26 xfailed 2246 passed, 3 xfailed
Windows second pytest pass 36 passed, 10 xfailed 46 passed, 0 xfailed
macOS pytest 2241 passed, 24 xfailed 2269 passed, 1 xfailed
macOS second pytest pass 28 passed, 9 xfailed 37 passed, 0 xfailed
Ubuntu (3 jobs) 2267 passed, 1 xfailed 2274 passed, 1 xfailed
  • Xfails removed: all 33 Windows xfails in grass_pygrass_grid_test.py now pass (the spawn start method / "requires pickable functions" skips). macOS shows the same change. The second pytest pass went from 10 xfailed to 0 on both platforms.
  • Xfails left: the same 3 on Windows as before.
    • test_getl_cr and test_getl2_cr
    • test_bytes_garbage_in_out, in two classes
    • test_mapcalc_rand_with_invalid_seed
    • test_verbose_listing_output
    • test_grass_lib_usable_after_init
  • Test counts: Ubuntu gained 7 tests, which probably come from main moving between the two runs. Windows and macOS gained a few tests but not the same number as Ubuntu. I didn't track down why.
  • Speed: Windows pytest ran in 17 minutes instead of 19, though the CI runners vary too much to read much into that.

Things to be aware of

  1. Six Windows gunittest failures, identical on main and the PR. I compared all 351 test results in the two Windows reports, and every status and return code matches.

    • raster_md5test.sh
    • test_grass_tmp_mapset.py
    • map3d_test.py
    • test_pygrass_doctests.py
    • test_pygrass_vector_doctests.py
    • test_addons_download.py

    Don't attribute these to the PR.

  2. Heap corruption on Windows. test_pygrass_vector_doctests.py exits with return code 3221226356, which is 0xC0000374, a Windows heap-corruption error. The other failures exit with code 1. This is the only failure that looks like a crash rather than an ordinary test failure.

  3. g.extension download failures on Windows. g.extension extension=db.join and extension=r.gdd fail in test_addons_download.py. The log lines I saw don't say why. Both runs show the same 5 failures out of 9 tests.

  4. Noise that appears in both runs and doesn't matter:

    • The macOS TargetConditionals.h: unknown compiler configure message.
    • No module named 'wx' during the Ubuntu builds.
    • A real-install Error 1 (ignored) line.
  5. Weak spot for the PR to check: the PR now runs these grid tests under spawn on Windows and macOS. They passed here on both platforms. Because multiprocessing under spawn can be timing-sensitive, I'd re-run Windows once to check they stay green.

@petrasovaa petrasovaa added the conflicts/needs rebase Rebase to or merge with the latest base branch is needed label Dec 1, 2026
Claude added 4 commits December 1, 2026 19:03
…a failure

Summing the exit codes let a negative code (a tool killed by a signal)
cancel out a positive one, so the failure went unnoticed.
The fake tools create no maps, so a later step fails too and the
SystemExit alone does not show that the exit codes were handled.
@petrasovaa

Copy link
Copy Markdown
Contributor

Good news, it fixed 23+10 xfails on Windows, 23+9 xfails on macOS, and no xfail change on Ubuntu (pytest).

Before and after

Job Main PR
Windows pytest 2221 passed, 26 xfailed 2246 passed, 3 xfailed
Windows second pytest pass 36 passed, 10 xfailed 46 passed, 0 xfailed
macOS pytest 2241 passed, 24 xfailed 2269 passed, 1 xfailed
macOS second pytest pass 28 passed, 9 xfailed 37 passed, 0 xfailed
Ubuntu (3 jobs) 2267 passed, 1 xfailed 2274 passed, 1 xfailed
Details

So Claude complains that it passes sort of by accident:

The workflows set PYTHONPATH="...:${PYTHONPATH}" with PYTHONPATH unset, which leaves a trailing colon. Python reads an empty entry as the current directory, CI runs from the repo root, so the import works by accident. Anyone with PYTHONPATH already set, or running from another directory, sees the tests fail.

@echoix

echoix commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

So Claude complains that it passes sort of by accident:

The workflows set PYTHONPATH="...:${PYTHONPATH}" with PYTHONPATH unset, which leaves a trailing colon. Python reads an empty entry as the current directory, CI runs from the repo root, so the import works by accident. Anyone with PYTHONPATH already set, or running from another directory, sees the tests fail.

I don't see why there's something different about workflows, that PR doesn't touch it. I ré-merged main into it in case there were other commits of main shown here. Otherwise, could you tell me what you meant

@echoix echoix removed the conflicts/needs rebase Rebase to or merge with the latest base branch is needed label Dec 2, 2026
@github-actions github-actions Bot added temporal Related to temporal data processing module labels Dec 2, 2026
…rkers

With --import-mode=importlib, pytest imports test modules by their path
relative to the root directory without adding the root to sys.path. The
worker processes then could not import the test module to get the target
function, unless the current directory happened to be the root and was
on sys.path, as with the trailing colon in PYTHONPATH in CI.
@echoix
echoix force-pushed the claude/laughing-lovelace-44g9uv branch from 8653af5 to 1756438 Compare December 2, 2026 11:08
petrasovaa
petrasovaa previously approved these changes Dec 2, 2026
@echoix
echoix enabled auto-merge (squash) December 2, 2026 22:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs libraries module Python Related code is in Python temporal Related to temporal data processing tests Related to Test Suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants