← All posts

Build log

Why our FEM test was flaky: Gmsh threads

A test that missed by a fraction of a percent pointed to non-deterministic meshing.

Why we needed it: A test failed on one run and passed on the re-run. GC wanted the merge and the flakiness fixed: “merge it and fix the flaky test.”

A CI run on PR #336 failed one test out of 146. test_fem_decomposed_cantilever builds the same beam twice and requires the two maximum displacements to agree within 8%. This run they came out at 0.0541 mm and 0.0497 mm, just over the limit. A re-run passed, but a test that only sometimes fails hides real regressions, so GC’s call was “merge it and fix the flaky test.”

Claude Code found the cause. Two meshing paths let Gmsh use every core, and as the session put it, “Gmsh’s multi-threaded 3D mesher doesn’t give the same mesh twice, so results drifted between runs.” The codebase already had a helper that forces single-threaded meshing; these two call sites just didn’t use it.

The session ran the demo six times each way:

  • Multi-threaded: 162 or 163 nodes, and displacement from 0.04973 to 0.05121 mm.
  • Single-threaded: all six runs identical.

On a mesh of about 440k tetrahedra, single-threaded meshing took 12.2 s against 11.8 s, so the cost is small. PR #344 routes every Gmsh call in the worker through the helper and tightens the determinism tests from 8% displacement and 15% stress to 2% and 2%. Three back-to-back runs then differed by 0.0%, and the PR merged.

PR #344