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

File not closed on timeout or SIGINT #28

Open
kdee-korreyd opened this issue Sep 14, 2020 · 4 comments
Open

File not closed on timeout or SIGINT #28

kdee-korreyd opened this issue Sep 14, 2020 · 4 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@kdee-korreyd
Copy link

Issue

filelock-unix.c; file descriptor "filedes" is not closed if no lock is received (due to timeout or SIGINT). This can cause a bunch of files to be left open if using filelock through an interactive shell (R, rstudio, etc..)

Reproduce

session 1:

$ R --vanilla
> library(filelock)
> lock("/tmp/test.lck")
Lock on ‘/tmp/test.lck’

lsof:

$ lsof | grep "test.lck"
R          2504               jacobs    3u      REG   0,53        0    2393006 /tmp/test.lck

session 2:

$ R --vanilla
> library(filelock)
> lock("/tmp/test.lck",timeout=1)
NULL #<-- timeout, as expected

lsof:

# at this point I'd expect there to still only be 1 open file but...:
$ lsof | grep "test.lck"
R         11476               jacobs    3u      REG   0,53        0    2393006 /tmp/test.lck
R         17162               jacobs    3u      REG   0,53        0    2393006 /tmp/test.lck

Workaround

The workaround I am currently using for this can be found here:
#master...kdee-korreyd:closefile

@gaborcsardi gaborcsardi added the bug an unexpected problem or unintended behavior label Sep 14, 2020
@gaborcsardi
Copy link
Member

We could use r-lib/cleancall to close the FD.

@gaborcsardi
Copy link
Member

Reprex:

tmp <- tempfile()

l <- filelock::lock(tmp)

rs <- callr::r_session$new()
rs$call(function(path) .GlobalEnv$lock <- filelock::lock(path, timeout = 1), list(tmp))
rs$poll_io(3000)
#>   output    error  process 
#> "silent" "silent"  "ready"
rs$read()
#> $code
#> [1] 200
#> 
#> $message
#> [1] "done callr-rs-result-5bde6b93fe4a"
#> 
#> $result
#> NULL
#> 
#> $stdout
#> [1] ""
#> 
#> $stderr
#> [1] ""
#> 
#> $error
#> NULL
#> 
#> attr(,"class")
#> [1] "callr_session_result"

ps::ps_open_files(ps::ps_handle(rs$get_pid()))
#> # A tibble: 1 x 2
#>      fd path                                                                    
#>   <int> <chr>                                                                   
#> 1     4 /private/var/folders/59/0gkmw1yj2w7bf2dfc3jznv5w0000gn/T/RtmpnQ6TKj/fil…

Created on 2020-09-14 by the reprex package (v0.3.0)

@kdee-korreyd
Copy link
Author

yes that's a better solution, and clears up the issue.

@kdee-korreyd
Copy link
Author

is the change you used for the example above something that can be committed to master?

these are the changes i used to implement the cleancall solution:
kdee-korreyd@182fd88

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants