Skip to content

fzd: force calculators=1 for R-function models instead of just warning - #5

Merged
yannrichet merged 2 commits into
mainfrom
force-calculators-1-for-r-function-models
Aug 3, 2026
Merged

fzd: force calculators=1 for R-function models instead of just warning#5
yannrichet merged 2 commits into
mainfrom
force-calculators-1-for-r-function-models

Conversation

@yannrichet-asnr

Copy link
Copy Markdown
Member

Summary

Companion to Funz/fz#80, which makes fzd(model=<Python function>, calculators=N>1) actually evaluate the model concurrently in a worker-thread pool on the fz (Python) side, instead of always running sequentially regardless of calculators as it did before.

That change makes this package's previous behavior unsafe: when calculators > 1 was passed alongside an R function model, fzd() emitted a warning saying the value "has no effect" but still forwarded it unchanged to the fz Python module. Once the Python side honors calculators > 1, that would call the R function back into the R session (via reticulate) from a worker thread — reticulate callbacks are only safe from the main thread, so this would crash the R session.

Change

fzd() now unconditionally resets calculators to 1L whenever model is an R function and a value other than 1 was requested, after emitting a warning that explains why (mentions the crash risk and the forcing, not just "no effect"). calculators = 1 or omitted remains silent, as before. calculators is still forwarded unchanged for file-based (non-function) models.

Also:

  • Regenerated man/fzd.Rd via roxygen2::roxygenise().
  • Updated the calculators = 4L "Direct function model" @examples snippet to 1L (misleading otherwise, now that it's forced back to 1L for R functions).

Testing

Added tests/testthat/test-fzd-calculators-forced.R, mocking get_fz() via testthat::local_mocked_bindings() (no live Python/reticulate needed) to verify:

  • calculators is forced to 1 (with a warning matching "forced to 1") when > 1 is requested for an R-function model.
  • No warning when calculators is omitted or already 1.
  • Invalid calculators values (non-scalar, non-numeric) are still rejected with an error.
  • calculators is forwarded unchanged for file-based (non-function) models.

Full suite: devtools::test() → 46 passed, 2 pre-existing failures unrelated to this change (test-modelica-examples.R, reproduced identically on main before this branch — see PR discussion), 1 skip.

yannrichet and others added 2 commits August 2, 2026 19:37
…arning

Funz/fz#80 makes fzd(model=<Python function>, calculators=N>1) actually
evaluate the model concurrently in a worker-thread pool, instead of
always running sequentially regardless of calculators as it did before.

That change makes the fz.R wrapper's previous behavior unsafe: when
calculators > 1 was passed alongside an R function model, fzd() emitted a
warning saying the value "has no effect" but still forwarded it unchanged
to the fz (Python) module. Once the Python side honors calculators > 1,
that would call the R function back into the R session (via reticulate)
from a worker thread -- reticulate callbacks are only safe from the main
thread, so this would crash the R session.

fzd() now unconditionally resets calculators to 1L whenever model is an R
function and a value other than 1 was requested, after emitting a warning
that explains why (mentions the crash risk and the forcing, not just "no
effect"). calculators=1 or omitted remains silent, as before. calculators
is still forwarded unchanged for file-based (non-function) models.

Updated roxygen docs (man/fzd.Rd regenerated via roxygen2::roxygenise())
and the calculators=4L "Direct function model" example (misleading now
that it's forced back to 1L for R functions).

Added tests/testthat/test-fzd-calculators-forced.R, mocking get_fz() via
testthat::local_mocked_bindings() to verify: calculators is forced to 1
(with a warning) when > 1 is requested for an R-function model; no
warning when omitted or already 1; invalid calculators values are
rejected; and calculators is forwarded unchanged for file-based models.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
R CMD check --as-cran flags non-ASCII characters as a WARNING, which
fails CI since the workflow uses error_on: warning.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@yannrichet
yannrichet merged commit a16e1c7 into main Aug 3, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants