There was an issue with copying and freeing rngcontext when using BBS as the rollout RNG.
In RolloutLoopMT(), CopyRNGContext() is called for each worker thread to create a copy of the rollout RNG context. However, the conventional CopyRNGContext() performed a simple assignment of the rngcontext structure, resulting in a shallow copy even for GMP's mpz_t members. BBS uses mpz_t variables such as zModulus, zSeed, zZero, and zOne as the RNG state. Since a simple copy of mpz_t does not replicate its internally allocated memory, the source and destination copies end up referencing the same GMP internal data. Consequently, despite copying the RNG context for each worker thread, there was a possibility of sharing the internal RNG state data across threads when using BBS. During rollout, each worker sets seeds and updates the BBS state on its own context, making this state incorrect. The fix explicitly initializes and copies the GMP values in CopyRNGContext(), ensuring that each worker thread has an independent BBS state. Additionally, free_rngctx() previously only called mpz_clear() on nz, and did not free zModulus, zSeed, zZero, and zOne initialized for BBS. These are now also cleared using mpz_clear() when fZInit is true. Along with this, the destruction of the contexts created by CopyRNGContext() in RolloutLoopMT() was changed from a simple g_free() to using free_rngctx(). This correctly frees the deeply copied GMP objects as well. With this fix, RNG contexts between worker threads are now truly independent in BBS rollouts, and the memory leak caused by failing to free GMP objects in the BBS context has also been resolved. Best regards, -- TAKAHASHI Kaoru
0001-Fix-copying-and-cleanup-of-BBS-RNG-contexts.patch.gz
Description: application/gzip
