Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R-package] R session crashes on Windows when {lightgbm} is loaded after tidyverse #4007

Closed
estroger34 opened this issue Feb 21, 2021 · 16 comments · Fixed by #4155
Closed

[R-package] R session crashes on Windows when {lightgbm} is loaded after tidyverse #4007

estroger34 opened this issue Feb 21, 2021 · 16 comments · Fixed by #4155

Comments

@estroger34
Copy link

I am using lightgbm in a tidymodels workflow using the treesnip package. Data loading and wrangling uses functions from the tidyverse.

If library(lightgbm) is loaded after library(tidyverse), the R session in RStudio crashes during model fit without further error.

If library(lightgbm) is loaded before library(tidyverse), this does not happen.

This results in a crash:

library(tidyverse)

library(tidymodels)
library(lightgbm)
library(treesnip)

workflow <- workflow() %>% 
  add_recipe(recipe(formula = Sepal.Length ~ ., data = iris) ) %>% 
  add_model(boost_tree() %>% 
              set_mode("regression") %>% 
              set_engine("lightgbm"))

fit(workflow, data = iris)

This works:

library(tidymodels)
library(lightgbm)
library(treesnip)

library(tidyverse)

workflow <- workflow() %>% 
  add_recipe(recipe(formula = Sepal.Length ~ ., data = iris) ) %>% 
  add_model(boost_tree() %>% 
              set_mode("regression") %>% 
              set_engine("lightgbm"))

fit(workflow, data = iris)

I installed lightgbm v3.1.1 from CRAN.

Session info:

R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] forcats_0.5.1       stringr_1.4.0       readr_1.4.0         tidyverse_1.3.0     treesnip_0.1.0.9000
 [6] lightgbm_3.1.1      R6_2.5.0            yardstick_0.0.7     workflows_0.2.1     tune_0.1.2         
[11] tidyr_1.1.2         tibble_3.0.6        rsample_0.0.9       recipes_0.1.15      purrr_0.3.4        
[16] parsnip_0.1.5       modeldata_0.1.0     infer_0.5.4         ggplot2_3.3.3       dplyr_1.0.4        
[21] dials_0.0.9         scales_1.1.1        broom_0.7.4         tidymodels_0.1.2   

loaded via a namespace (and not attached):
 [1] httr_1.4.2         jsonlite_1.7.2     splines_4.0.3      foreach_1.5.1      modelr_0.1.8       prodlim_2019.11.13
 [7] vip_0.3.2          assertthat_0.2.1   GPfit_1.0-8        cellranger_1.1.0   globals_0.14.0     ipred_0.9-9       
[13] pillar_1.4.7       backports_1.2.1    lattice_0.20-41    glue_1.4.2         pROC_1.17.0.1      digest_0.6.27     
[19] hardhat_0.1.5      rvest_0.3.6        snakecase_0.11.0   colorspace_2.0-0   Matrix_1.2-18      plyr_1.8.6        
[25] timeDate_3043.102  pkgconfig_2.0.3    lhs_1.1.1          DiceDesign_1.9     listenv_0.8.0      haven_2.3.1       
[31] gower_0.2.2        lava_1.6.8.1       generics_0.1.0     tictoc_1.0         ellipsis_0.3.1     withr_2.4.1       
[37] janitor_2.1.0      furrr_0.2.2        nnet_7.3-14        cli_2.3.0          readxl_1.3.1       survival_3.2-7    
[43] magrittr_2.0.1     crayon_1.4.1       fs_1.5.0           future_1.21.0      parallelly_1.23.0  MASS_7.3-53       
[49] xml2_1.3.2         class_7.3-17       tools_4.0.3        data.table_1.13.6  hms_1.0.0          lifecycle_1.0.0   
[55] reprex_1.0.0       munsell_0.5.0      compiler_4.0.3     tinytex_0.29       rlang_0.4.10       grid_4.0.3        
[61] iterators_1.0.13   rstudioapi_0.13    gtable_0.3.0       codetools_0.2-16   DBI_1.1.1          gridExtra_2.3     
[67] lubridate_1.7.9.2  stringi_1.5.3      parallel_4.0.3     Rcpp_1.0.6         vctrs_0.3.6        rpart_4.1-15      
[73] dbplyr_2.1.0       tidyselect_1.1.0   xfun_0.21         
@jameslamb
Copy link
Collaborator

@estroger34 thanks for using LightGBM!

Can you please tell me how you installed {treesnip}, and when you last installed it? That project is not on CRAN, so I wonder if you have a version of that project that is doing something non-standard with {lightgbm}. Until very recently, {treesnip} used to install {lightgbm} by downloading a binary manually created by the {treesnip} maintainers. It's possible that as a result, you have two different versions of {lightgbm} installed right now.

You might even be encountering the same issue that the {treesnip} maintainers are facing right now: curso-r/treesnip#35

Are you able to reproduce this problem without using {treesnip}?

@z-feldman
Copy link

z-feldman commented Feb 22, 2021

Came here b/c of similar R session crashing with tidymodels and treesnip. I'm trying @estroger34 examples and for me neither of those worked. Looks like only difference is I'm still on R 3.6.3

R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] forcats_0.5.1       stringr_1.4.0       readr_1.4.0         tidyverse_1.3.0     treesnip_0.1.0.9000 lightgbm_3.1.1      R6_2.5.0            yardstick_0.0.7     workflows_0.2.1    
[10] tune_0.1.2          tidyr_1.1.2         tibble_3.0.6        rsample_0.0.9       recipes_0.1.15      purrr_0.3.4         parsnip_0.1.5       modeldata_0.1.0     infer_0.5.4        
[19] ggplot2_3.3.3       dplyr_1.0.4         dials_0.0.9         scales_1.1.1        broom_0.7.5         tidymodels_0.1.2   

loaded via a namespace (and not attached):
 [1] httr_1.4.2         jsonlite_1.7.2     splines_3.6.3      foreach_1.5.1      prodlim_2019.11.13 modelr_0.1.8       assertthat_0.2.1   GPfit_1.0-8        cellranger_1.1.0   globals_0.14.0    
[11] ipred_0.9-9        pillar_1.5.0       backports_1.2.1    lattice_0.20-41    glue_1.4.2         pROC_1.17.0.1      digest_0.6.27      rvest_0.3.6        colorspace_2.0-0   Matrix_1.2-18     
[21] plyr_1.8.6         timeDate_3043.102  pkgconfig_2.0.3    lhs_1.1.1          DiceDesign_1.9     listenv_0.8.0      haven_2.3.1        gower_0.2.2        lava_1.6.8.1       generics_0.1.0    
[31] tictoc_1.0         ellipsis_0.3.1     withr_2.4.1        furrr_0.2.2        nnet_7.3-15        cli_2.3.0          readxl_1.3.1       survival_3.2-7     magrittr_2.0.1     crayon_1.4.1      
[41] fs_1.5.0           future_1.21.0      fansi_0.4.2        parallelly_1.23.0  MASS_7.3-51.6      xml2_1.3.2         class_7.3-18       tools_3.6.3        data.table_1.14.0  hms_1.0.0         
[51] lifecycle_1.0.0    reprex_1.0.0       munsell_0.5.0      compiler_3.6.3     tinytex_0.29       rlang_0.4.10       grid_3.6.3         iterators_1.0.13   rstudioapi_0.13    gtable_0.3.0      
[61] codetools_0.2-16   DBI_1.1.1          lubridate_1.7.9.2  utf8_1.1.4         stringi_1.5.3      parallel_3.6.3     Rcpp_1.0.6         vctrs_0.3.6        rpart_4.1-15       dbplyr_2.1.0      
[71] tidyselect_1.1.0   xfun_0.21         

@jameslamb
Copy link
Collaborator

thanks for the report! I have the same question for you @z-feldman

  1. how did you install {treesnip} and when did you last install it?
  2. are you able to reproduce this problem in a session where {treesnip} is not involved?

@z-feldman
Copy link

z-feldman commented Feb 22, 2021

  1. I installed with remotes::install_github("curso-r/treesnip"), which is what's on their README. I last installed it a few hours ago.

  2. It looks like I get the error if I load any of {tidymodels}, {treesnip}, or {tidyverse} before {lightgbm}

To test following examples start new R sessions for each...

This works:

library(lightgbm)

data(agaricus.train, package='lightgbm')
train <- agaricus.train
dtrain <- lgb.Dataset(train$data, label = train$label)
model <- lgb.cv(
  params = list(
    objective = "regression"
    , metric = "l2"
  )
  , data = dtrain
)

This crashes:

library(tidymodels) # will crash with library(treesnip) or library(tidyverse) instead of {tidymodels}
library(lightgbm)

data(agaricus.train, package='lightgbm')
train <- agaricus.train
dtrain <- lgb.Dataset(train$data, label = train$label)
model <- lgb.cv(
  params = list(
    objective = "regression"
    , metric = "l2"
  )
  , data = dtrain
)

This is also consistent when using treesnip and tidymodels.

This works:

library(lightgbm)
library(tidymodels)
library(treesnip)
library(tidyverse)

workflow <- workflow() %>% 
  add_recipe(recipe(formula = Sepal.Length ~ ., data = iris) ) %>% 
  add_model(boost_tree() %>% 
              set_mode("regression") %>% 
              set_engine("lightgbm"))

fit(workflow, data = iris)

This crashes:

library(tidymodels) # again, if any of the three are loaded before lightgbm it crashes
library(treesnip)
library(lightgbm)
library(tidyverse)

workflow <- workflow() %>% 
  add_recipe(recipe(formula = Sepal.Length ~ ., data = iris) ) %>% 
  add_model(boost_tree() %>% 
              set_mode("regression") %>% 
              set_engine("lightgbm"))

fit(workflow, data = iris)

@jameslamb
Copy link
Collaborator

wow that's wild haha! Ok thank you both for the very detailed reproducible examples. I'll look into this as soon as I can.

I can rule out one thing, for the benefit of anyone else trying to debug this.

{lightgbm} does not use any of the "Hooks for Namespace Events" like .onLoad() or .onDetach()

@jameslamb
Copy link
Collaborator

jameslamb commented Feb 22, 2021

Ok I'm testing right now on R 4.0.3, in a fresh R session in a clean container.

docker run -it rocker/verse /bin/bash

I installed the dependencies you mentioned above.

Rscript -e 'remotes::install_github("curso-r/treesnip")'
Rscript -e 'install.packages(c("tidymodels", "tidyverse", "lightgbm"))'

I then ran each of the examples from #4007 (comment), even trying each of {treesnip}, {tidyverse}, and {tidymodels} for those where you left a comment like # if any of these...

I created a new session each time by running the following command, which ensures that nothing is being preserved from session to session or read in from config files.

R --vanilla --no-restore

And I quit each session with q().

Every one of those examples succeeded, and I could not reproduce the crashes.


Given this, I don't think there is an issue where {lightgbm} conflicts with those projects.

Did either of you previously install {lightgbm} following the old {treesnip} guidance? Those maintainers used to suggest you use another project called {rightgbm}, which downloaded some custom package binaries they uploaded: https://github.com/curso-r/rightgbm/tree/4ecb810c9a1b887888ea97169caa3c4839839cb2. That suggestion was removed in curso-r/treesnip@0577b2c, after {lightgbm} made it to CRAN.

I'm happy to keep this discussion here so you don't have to bounce back and forth between the projects, but my theory right now is that that non-standard installation with {rightgbm} left behind a conflicting copy of {lightgbm}, and that that is causing issues for you.

@Athospd @dfalbel could you help out here?

@z-feldman
Copy link

I did use rightgbm at one point. I'll try to see if there's something lingering from that

@estroger34
Copy link
Author

estroger34 commented Feb 23, 2021

I did not use rightgbm before. I had installed treesnip just a day before opening this issue.

Just now, I removed treesnip with remove.packages("treesnip") and re-installed with remotes::install_github("curso-r/treesnip").

This leads to exactly the same behaviour as described by @z-feldman on my machine. Now, also the second example of my first post, which did work a few days ago, crashes. Next to re-installing treesnip, I updated pillar to v1.5.0 from CRAN, as suggested during package.install.

Even the non-treesnip example fails, if tidyverse is loaded before lightgbm, i.e. this does not work:

library(tidyverse) 
library(lightgbm)

data(agaricus.train, package='lightgbm')
train <- agaricus.train
dtrain <- lgb.Dataset(train$data, label = train$label)
model <- lgb.cv(
  params = list(
    objective = "regression"
    , metric = "l2"
  )
  , data = dtrain
)

@estroger34
Copy link
Author

I did some more testing and found that in the above non-treesnip example the crash happens with any of the following packages from the tidyverse , if loaded before lightgbm:

library(parsnip)
library(dplyr)
library(pillar)
library(tibble)

For others, like Rlang, vctrs, generics, magrittr, there is no crash.

For the packages causing the crash, it can always be avoided by loading the package after lightgbm.

In between the tests, I cleared the environment (incl. hidden objects) in RStudio and restarted the R session.

@tonyk7440
Copy link
Contributor

I did a little digging on a fresh new windows server machine and I can reproduce the non-treesnip example above.

I tried the tidyverse packages individually and also some non-tidyverse packages listed above, below are some results.
The results are from loading one of the packages below then loading lightgbm and running the example code after that

# Tidyverse packages
# library(tidyverse) # crash
# library(dplyr) #crash
# library(ggplot2) # crash
# library(tidyr) # crash
# library(readr) # crash
# library(tibble) # crash
# library(purrr) # fine
# library(stringr) # fine
# library(forcats) # fine

# Non tidyverse
# library(parsnip) # crash
# library(pillar) # crash
# library(tibble) # crash
data(agaricus.train, package='lightgbm')
train <- agaricus.train
dtrain <- lgb.Dataset(train$data, label = train$label)
model <- lgb.cv(
  params = list(
    objective = "regression"
    , metric = "l2"
  )
  , data = dtrain
)

Session info below when loading sessioninfo then dplyr then lightgbm and running the example to just before the crash

> session_info()
- Session info --------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 4.0.4 (2021-02-15)
 os       Windows Server x64          
 system   x86_64, mingw32             
 ui       RStudio                     
 language (EN)                        
 collate  English_United States.1252  
 ctype    English_United States.1252  
 tz       GMT                         
 date     2021-02-23                  

- Packages ------------------------------------------------------------------------------------------------------------
 package     * version date       lib source        
 assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.3)
 cli           2.3.0   2021-01-31 [1] CRAN (R 4.0.3)
 crayon        1.4.1   2021-02-08 [1] CRAN (R 4.0.3)
 data.table    1.13.6  2020-12-30 [1] CRAN (R 4.0.3)
 DBI           1.1.1   2021-01-15 [1] CRAN (R 4.0.3)
 dplyr       * 1.0.4   2021-02-02 [1] CRAN (R 4.0.3)
 ellipsis      0.3.1   2020-05-15 [1] CRAN (R 4.0.3)
 fansi         0.4.2   2021-01-15 [1] CRAN (R 4.0.3)
 generics      0.1.0   2020-10-31 [1] CRAN (R 4.0.3)
 glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.3)
 jsonlite      1.7.2   2020-12-09 [1] CRAN (R 4.0.3)
 lattice       0.20-41 2020-04-02 [2] CRAN (R 4.0.4)
 lifecycle     1.0.0   2021-02-15 [1] CRAN (R 4.0.4)
 lightgbm    * 3.1.1   2020-12-08 [1] CRAN (R 4.0.3)
 magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.0.3)
 Matrix        1.3-2   2021-01-06 [2] CRAN (R 4.0.4)
 pillar        1.5.0   2021-02-22 [1] CRAN (R 4.0.4)
 pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.0.3)
 purrr         0.3.4   2020-04-17 [1] CRAN (R 4.0.3)
 R6          * 2.5.0   2020-10-28 [1] CRAN (R 4.0.3)
 rlang         0.4.10  2020-12-30 [1] CRAN (R 4.0.3)
 sessioninfo * 1.1.1   2018-11-05 [1] CRAN (R 4.0.3)
 tibble        3.0.6   2021-01-29 [1] CRAN (R 4.0.3)
 tidyselect    1.1.0   2020-05-11 [1] CRAN (R 4.0.3)
 tinytex       0.29    2021-01-21 [1] CRAN (R 4.0.3)
 utf8          1.1.4   2018-05-24 [1] CRAN (R 4.0.3)
 vctrs         0.3.6   2020-12-17 [1] CRAN (R 4.0.3)
 withr         2.4.1   2021-01-26 [1] CRAN (R 4.0.3)
 xfun          0.21    2021-02-10 [1] CRAN (R 4.0.3)

[1] C:/Users/akenny/Documents/R/win-library/4.0
[2] C:/Program Files/R/R-4.0.4/library

@jameslamb
Copy link
Collaborator

@tonyk7440 or @estroger34 could you try in a non-RStudio R session, like the R --vanilla --no-restore I used in #4007 (comment)?

It's also interesting that all three of you are using Windows. I'll have to try to reproduce this tonight on Windows.

@tonyk7440
Copy link
Contributor

Hi @jameslamb , just tried what you suggested but unfortunately it also seems to have errored, screenshot attached below
I noticed too that all the examples are using Windows, I think I've had this problem with a while but just put it down to my own little computer problem

image

@jameslamb
Copy link
Collaborator

Ok thanks! That's really helpful. I'm going to change the image title to reflect that. I'm glad we could rule out RStudio as a source of the problem, that would have been really tough to debug 😂

I won't be able to take a look at this until later tonight, but in case anyone gets to it sooner, I have two thoughts that might help.

  1. I'd be surprised to learn that masking a function from {dplyr} could crash R, but I suppose it's worth looking at:
Attaching package: 'lightgbm'

The following object is masked from 'package:dplyr'

        slice
  1. As I mentioned in [R-package] R session crashes on Windows when {lightgbm} is loaded after tidyverse #4007 (comment), {lightgbm} doesn't use any of the hooks that allow packages to optionally run code on events like a library being attached or detached. But I imagine some of the tidyverse packages mentioned in this issue do, based on the loading messages I've seen from them. So when I'm able to investigate, I'm going to check those packages' sources to see if they define .OnLoad() or .OnAttach() and, if so, I'm going to check if the code they run in those methods changes something about the global environment in a way that would impact {lightgbm}.

@jameslamb jameslamb changed the title [R-package] R session crashes when lightgbm is loaded after tidyverse [R-package] R session crashes on Windows when {lightgbm} is loaded after tidyverse Feb 23, 2021
@jameslamb jameslamb mentioned this issue May 20, 2021
21 tasks
@verajosemanuel
Copy link

verajosemanuel commented Jun 25, 2021

same here. Crashes after treesnip is installed in a clean new vanilla environment in windows 10. All packages are latest version (renv initialized project)

treesnip installed with

remotes::install_github("curso-r/treesnip")

I had to load the packages in this order to make it work:


library(lightgbm)

library(tidyverse)
library(tidymodels)

@jameslamb
Copy link
Collaborator

@verajosemanuel This issue has been fixed on master but has not been released to CRAN yet.

Please see #4259 (comment) for steps to install from source and subscribe to #4310 to be notified when a new release is published.

@jameslamb
Copy link
Collaborator

I'm locking conversation on this issue. Please comment on #4464 if you'd like to join the conversation about this problem.

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants