Windows/R and Jupyter issue with FFI
Summary
Write a brief description of the issue. When using Windows or Jupyter/Ubuntu, there seems to be a race condition where the FFI peek function gets the value of a pointer before R has had a chance to initialize it. As I explained in ghc-devs, when fetching an address from the R side we get a bad address, but inserting a trivial 'trace "PEEK"' before the peek resolves the problem. I guess this introduces some delay and fixes the race condition?
Steps to reproduce
See https://github.com/djsamperi/hR.git, where I have updated an interface to R that was written back in 2012 by Dylan Simon. To use, clone this repo and checkout branch update2023. Note that the file Foreign/R/Sexp.hs will have to be deleted before checkout under Windows (because the original repo contained Sexp.hs and SEXP.hs, which are indistinguishable under Windows).
Make sure R is in your PATH, and that R_HOME is set. For Windows also set RTOOLS_HOME. Install using 'stack install' in root of package, then start the REPL using 'stack exec hr'. You should see the word 'PEEK' after which you can enter an R expression, like 'sqrt(2)', say. The response should be the corresponding R AST, followed by an eval of this tree (1.414).
This is the expected behavior. To trigger the bug (under Windows, or in Jupyter under Ubuntu), comment out the line 'cpp-options: -DDELAYPEEK' in hR.cabal. In this case you will see a seg fault under Windows when starting the REPL, or a bad address in Jupyter when executing this code in a cell:
x <- rNilValue
x
What is happening is the R_NilValue from R is not properly fetched (via peek) unless there is a tiny delay before the peek, introduced by a debug trace: 'trace "PEEK"' before the peek. This trace is inserted when DELAYPEEK is defined, and when it is not defined we get a bad address.
The code where R_NilValue is fetched into rNilValue is in src/Foreign/HR/Internals.hsc. Search for DELAYPEEK in this file.
Expected behavior
Expected behavior (no crash) is what we see when the trace is inserted. The user is also confused by the appearance of the word "PEEK" for no apparent reason.
What do you expect the reproducer described above to do? The reproducer will get a bad address from R under Windows. The first thing the REPL does is fetch the value of rNilValue and print it out. This fails under Windows. Something similar happens when rNilValue is fetched in a Jupyter session under Ubuntu. But note that there are no problems using the REPL under Ubuntu outside of Jupyter.
Environment
- GHC version used: 9.2.7 (Stack lts-20.15)
Optional:
-
Operating System: Windows 11 (always fails without trace), and Ubuntu (Jupyter)
-
System Architecture: Intel i9 workstation.