Skip to content

Commit

Permalink
resolve "no applicable method for 'mutate'" error
Browse files Browse the repository at this point in the history
saw:

```
> Error in UseMethod("mutate") :
  no applicable method for 'mutate' applied to an object of class "NULL"
```

...intermittently for some calls. turns out this was because the session had a longer-than-expected delay before its results were able to be `read()`--just iteratively query with additional delay until we get results.
  • Loading branch information
simonpcouch committed Jul 8, 2024
1 parent d5c4639 commit 7aeef59
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
20 changes: 2 additions & 18 deletions R/syrup.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,28 +120,12 @@ syrup <- function(expr, interval = .5, peak = FALSE, env = caller_env()) {

# tell `sesh` to stop taking snapshots
file.remove(keep_going_file)
Sys.sleep(interval + .1)
Sys.sleep(interval + .2)

# grab the result from sesh and close it
sesh_res <- sesh$read()
sesh$close()
res <- retrieve_results(sesh)

withr::deferred_clear()

# return the memory usage information
res <- sesh_res$result

if (is.null(res)) {
if (!is.null(sesh_res$error)) {
stop(gsub("\n", " ", conditionMessage(sesh_res$error)))
} else {
stop(paste0("Class: ", paste0(class(sesh_res), collapse = ""),
"Code: ", sesh_res$code,
" Message: ", sesh_res$message,
" stderr: ", sesh_res$stderr))
}
}

if (identical(res$id[length(res$id)], 1) && !isTRUE(peak)) {
rlang::warn(c(
"!" = "`expr` evaluated fully before syrup could take a snapshot of memory usage.",
Expand Down
24 changes: 24 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,27 @@ calculate_pct_cpu <- function(time, user, system) {

c(NA_real_, (user_diffs + system_diffs) * 100 / intervals)
}

# grab the result from sesh and close it.
# may be a slightly longer delay before sesh is able to return, so iteratively
# query until we get a result back.
retrieve_results <- function(sesh, call = caller_env()) {
sesh_res <- sesh$read()
cnt <- 1
while (is.null(sesh_res) & cnt < 10) {
Sys.sleep(.2)
sesh_res <- sesh$read()
cnt <- cnt + 1
}

sesh$close()

if (cnt == 10) {
cli::cli_abort(
"Unable to retrieve resource usage results from the temporary session.",
.internal = TRUE
)
}

sesh_res$result
}

0 comments on commit 7aeef59

Please sign in to comment.