Skip to content

WISH: r_process$cleanup(close_connections = TRUE) #320

Open

Description

@HenrikBengtsson

If one launches too many callr processes while keeping track of them all, one can run out of file descriptors, although the processes have completed. This results in errors on "Too many open files".

Example

$ ( ulimit -n 200; R )

max_fd <- as.integer(system2("ulimit", args = "-n", stdout = TRUE))
cat("Max number of open files:", max_fd, "\n")

n <- ceiling(max_fd / 3)
processes <- list()
for (ii in 1:n) {
  cat(sprintf("\r%04d/%04d", ii, n))
  process <- callr::r_bg(function() NULL, stdout = "|", stderr = "|")
  process$wait()
  void <- process$read_all_output()
  void <- process$read_all_error()
  process$cleanup()
  processes[[ii]] <- process  ## prevent garbage collector from cleaning up
}
cat("\n")

Results in

Max number of open files: 200 
0064/0067Error in `process_initialize(...)`:
! Native call to `processx_exec` failed
Caused by error in `chain_call(...)`:
! processx socketpair (system error 24, Too many open files) @unix/processx.c:433 (processx__make_socketpair)
---
Backtrace:
 1. callr::r_bg(function() NULL, stdout = "|", stderr = "|")
 2. r_process$new(options = options)
 3. local initialize(...)
 4. callr:::rp_init(self, private, super, options)
 5. callr:::with_envvar(...)
 6. base::force(code)
 7. base::do.call(...)
 8. (function (command = NULL, args = character(), stdin = NULL,
 9. processx:::process_initialize(...)
10. processx:::chain_call(...)
11. | base::withCallingHandlers(...)
12. | base::do.call(".Call", list(.NAME, ...))
13. | base::.handleSimpleError(...)
14. | local h(simpleError(msg, call))
15. | processx:::throw_error(err, parent = e)
Execution halted

Wish

Add a method for closing these file connections, e.g. process$cleanup(close_connections = TRUE), similarly to process$kill(close_connections = TRUE).

Activity

  1. gaborcsardi commented on Jul 12, 2026

    @gaborcsardi
    Member

    Workaround:

    if (process$has_input_connection()) close(process$get_input_connection())
    if (process$has_output_connection()) close(process$get_output_connection())
    if (process$has_error_connection()) close(process$get_error_connection())
    if (process$has_poll_connection()) close(process$get_poll_connection())

    EDIT: actually, anything wrong with this?

    process$kill(close_connections = TRUE)
  2. HenrikBengtsson commented on Jul 12, 2026

    @HenrikBengtsson
    Author

    EDIT: actually, anything wrong with this?

    process$kill(close_connections = TRUE)

    Mostly that it feels like a hack and one needs to know what kill() does if the process is already terminated. For instance, it could be an error to call kill on a terminated process, or it could be a silent no-op.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions