Skip to content

Commit ec3f176

Browse files
committed
Disable setting rlimit by default
1 parent 6795df0 commit ec3f176

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

inline-r/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## 1.1.x — (unreleased, placeholder)
4+
5+
* Setting limit in the interactive mode was disabled on darwin.
6+
In order to completely disable it use `H_DISABLE_INCREASE_STACK_SIZE` should
7+
be set.
8+
39
## 1.0.2 - 2025-07-11
410

511
* Support R 4.5

inline-r/src/Language/R/Instance.hs

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import Control.Monad.Primitive (PrimMonad(..))
4242
import Control.Monad.R.Class
4343
import Control.Monad.ST.Unsafe (unsafeSTToIO)
4444
import qualified Data.Semigroup as Sem
45+
import Data.Maybe (isJust)
4546
import Data.Monoid
4647
import Data.Default.Class (Default(..))
4748
import qualified Foreign.R as R
@@ -232,35 +233,42 @@ initLock = unsafePerformIO $ newMVar ()
232233
-- initialize multiple times concurrently, but there is nothing stopping the
233234
-- compiler from doing so when compiling quasiquotes.
234235

235-
-- | Create a new embedded instance of the R interpreter. Only works from the
236-
-- main thread of the program. That is, from the same thread of execution that
237-
-- the program's @main@ function is running on. In GHCi, use @-fno-ghci-sandbox@
238-
-- to achieve this.
239-
initialize :: Config -> IO ()
240-
initialize Config{..} = do
241-
#ifndef mingw32_HOST_OS
242-
#if defined(darwin_HOST_OS) || defined(freebsd_HOST_OS)
243-
-- NOTE: OS X and FreeBSD does not allow removing the stack size limit completely,
244-
-- instead forcing a hard limit of just under 64MB.
245-
let stackLimit = ResourceLimit 67104768
246-
#else
247-
let stackLimit = ResourceLimitUnknown
236+
#if !defined(mingw32_HOST_OS) & !defined(darwin_HOST_OS)
237+
explainStackIncrease :: [String] -> String
238+
explainStackIncrease cmds = unlines $
239+
[ "Language.R.Interpreter: cannot increase stack size limit."
240+
, "Try increasing your stack size limit manually:"
241+
] ++ cmds
248242
#endif
249-
setResourceLimit ResourceStackSize (ResourceLimits stackLimit stackLimit)
250-
`onException` (hPutStrLn stderr $
251-
"Language.R.Interpreter: "
252-
++ "Cannot increase stack size limit."
253-
++ "Try increasing your stack size limit manually:"
243+
244+
setLimit :: IO ()
254245
#ifdef darwin_HOST_OS
255-
++ "$ launchctl limit stack 67104768"
256-
++ "$ ulimit -s 65532"
246+
setLimit = return ()
247+
#elif defined(mingw32_HOST_OS)
248+
setLimit = return ()
257249
#elif defined(freebsd_HOST_OS)
258-
++ "$ ulimit -s 67104768"
250+
setLimit = do
251+
let stackLimit = ResourceLimit 67104768
252+
setResourceLimit ResourceStackSize (ResourceLimits stackLimit stackLimit)
253+
`onException` hPutStrLn stderr (explainStackIncrease ["$ ulimit -s 67104768"])
259254
#else
260-
++ "$ ulimit -s unlimited"
261-
#endif
262-
)
255+
setLimit = do
256+
let stackLimit = ResourceLimitUnknown
257+
setResourceLimit ResourceStackSize (ResourceLimits stackLimit stackLimit)
258+
`onException` hPutStrLn stderr (explainStackIncrease ["$ ulimit -s unlimited"])
263259
#endif
260+
261+
262+
-- | Create a new embedded instance of the R interpreter. Only works from the
263+
-- main thread of the program. That is, from the same thread of execution that
264+
-- the program's @main@ function is running on. In GHCi, use @-fno-ghci-sandbox@
265+
-- to achieve this.
266+
--
267+
-- Set `H_DISABLE_INCREASE_STACK_SIZE` to disable changing rlimit.
268+
initialize :: Config -> IO ()
269+
initialize Config{..} = do
270+
shouldSetLimit <- isJust <$> lookupEnv "H_DISABLE_INCREASE_STACK_SIZE"
271+
unless shouldSetLimit setLimit
264272
initialized <- fmap (==1) $ peek isRInitializedPtr
265273
-- See note [Concurrent initialization]
266274
unless initialized $ withMVar initLock $ const $ do

0 commit comments

Comments
 (0)