Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions R/tests/attach_sess.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ writeLines(
" connection_test <- new.env(parent=globalenv())",
" connection_test$.sess_env <- new.env(parent=emptyenv())",
" connection_test$.session_id <- function() 'fixture-session'",
" connection_test$`%||%` <- function(x, y) if (is.null(x)) y else x",
" connection_test$.resolve_plot_backend <- function(x) 'standard'",
" connection_test$.resolve_endpoint <- function(endpoint=NULL, ...) {",
" if (is.null(endpoint)) '' else endpoint",
Expand Down Expand Up @@ -176,19 +177,25 @@ writeLines(
" metadata <- connection_test$.session_attach_metadata()",
" stopifnot(!('attachment_id' %in% names(metadata)),",
" !('auto_activate' %in% names(metadata)))",
" Sys.setenv(SESS_DISCOVERY_FILE=file.path(root, 'fixture-discovery'))",
" connection_test$.resolve_endpoint <- function(endpoint=NULL, ...) 'fixture-endpoint'",
" connection_test$.read_discovery <- function(path) list(endpoint='fixture-endpoint')",
" discovery <- file.path(root, 'fixture-discovery')",
" endpoint <- tempfile(fileext='.sock')",
" Sys.setenv(SESS_DISCOVERY_FILE=discovery)",
" Sys.unsetenv('SESS_ENDPOINT')",
" old_startup <- options(vscodeR.terminalStartup=list(",
" file=paste0(discovery, '.startup'), pid=Sys.getpid(), endpoint=endpoint,",
" run=function(endpoint, setup) setup()))",
" connection_test$.read_discovery <- function(path) list(endpoint=endpoint)",
" connection_test$.configure_discovery_jgd <- function(discovery, enabled) NULL",
" result <- connection_test$connect(endpoint='fixture-endpoint',",
" attachment_id='console_8', auto_activate=FALSE)",
" options <- connection_test$.sess_env$reconnect$options",
" stopifnot(identical(options$attachment_id, 'console_8'),",
" identical(options$auto_activate, FALSE))",
" result <- connection_test$connect(endpoint='fixture-endpoint')",
" options <- connection_test$.sess_env$reconnect$options",
" stopifnot(is.null(options$attachment_id), identical(options$auto_activate, TRUE),",
" retry <- NULL",
" connection_test$.schedule_reconnect <- function(settings) retry <<- settings",
" result <- connection_test$connect(attachment_id='console_8', auto_activate=FALSE)",
" stopifnot(identical(result, FALSE), identical(retry$options$attachment_id, 'console_8'),",
" identical(retry$options$auto_activate, FALSE))",
" result <- connection_test$connect()",
" stopifnot(identical(result, FALSE), is.null(retry$options$attachment_id),",
" identical(retry$options$auto_activate, TRUE),",
" is.null(connection_test$.sess_env$attachment_id))",
" options(old_startup)",
" Sys.unsetenv('SESS_DISCOVERY_FILE')",
"}",
"publish_result <- function(value) {",
Expand Down
7 changes: 5 additions & 2 deletions R/tests/prepare-jgd.R
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,13 @@ local({
"environment(connect_env$.schedule_reconnect) <- connect_env;",
"connect_env$.reconnect_with_startup <- sess_ns$.reconnect_with_startup;",
"environment(connect_env$.reconnect_with_startup) <- connect_env;",
"connect_env$poll_connection <- function(...) {",
"connect_env$poll_connection <- function(...) NULL;",
"connect_env$.await_attach <- function(con, id, ...) {",
" processx::poll(list(server), 1000L); processx::conn_accept_unix_socket(server);",
" processx::poll(list(server), 1000L); message <- processx::conn_read_chars(server);",
" stopifnot(grepl('\\\"method\\\":\\\"attach\\\"', message)); invisible(NULL)",
" request <- jsonlite::fromJSON(message);",
" stopifnot(identical(request$method, 'attach'), identical(request$id, id));",
" list(status='accepted', lines=character(), tail='')",
"};",
"answer <- vscode_r_with_jgd('jgd', function(allow_jgd=TRUE)",
" connect_env$connect(endpoint=endpoint, use_rstudioapi=FALSE,",
Expand Down
12 changes: 8 additions & 4 deletions sess/R/dispatch.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ipc_json <- function(data) {

#' Write a JSON object to the IPC pipe as a NDJSON line (internal)
#' @keywords internal
ipc_write <- function(data) {
ipc_write <- function(data, disconnect_on_error = TRUE) {
# Forked workers must never interleave frames on the parent Interactive socket.
if (
isTRUE(.sess_env$interactive_connected) && !identical(.sess_env$interactive_pid, Sys.getpid())
Expand All @@ -36,8 +36,12 @@ ipc_write <- function(data) {
invisible(TRUE)
},
error = function(e) {
warning("[sess] Failed to send IPC message: ", e$message)
.transport_disconnect(silent = TRUE, reconnect = TRUE)
if (disconnect_on_error) {
.transport_lost()
warning("[sess] Failed to send IPC message: ", e$message)
} else {
message("[sess] Failed to send session attachment request: ", e$message)
}
invisible(FALSE)
}
)
Expand Down Expand Up @@ -105,7 +109,7 @@ rpc_send <- function(method, params = list(), request = FALSE) {
#' @param params A list containing the arguments for the command
#' @export
notify_client <- function(method, params = list()) {
if (isTRUE(.sess_env$interactive_connected) && method != "attach") {
if (isTRUE(.sess_env$interactive_connected)) {
return(.interactive_event("notification", list(method = method, params = params)))
}
rpc_send(method, params, request = FALSE)
Expand Down
15 changes: 8 additions & 7 deletions sess/R/handlers.R
Original file line number Diff line number Diff line change
Expand Up @@ -1518,15 +1518,16 @@ handle_dataview_dispose <- function(params) {
if (!owned && !identical(connection$state, "disconnected")) {
return(FALSE)
}
# Callers that cannot wait for a reply (an inspection being answered) pass it in.
if (is.null(address)) {
# While R answers a VS Code request, the reply could not be read. Only a
# session that no other client can take control of keeps the connection
# that sess already has.
if (isTRUE(.sess_env$answering_request)) {
return(owned && !isTRUE(.sess_env$interactive_connected))
if (isTRUE(.sess_env$interactive_connected)) {
# Interactive resolves the current controller's viewer host.
if (isTRUE(.sess_env$answering_request)) {
return(FALSE)
}
address <- request_client("jdv/address")$address
} else {
address <- .sess_env$attach_jdv[["address"]]
}
address <- request_client("jdv/address")$address
}
if (!is.character(address) || length(address) != 1L || is.na(address) || !nzchar(address)) {
return(FALSE)
Expand Down
7 changes: 5 additions & 2 deletions sess/R/interactive.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,11 @@ interactive_start <- function(config, managed = FALSE) {
options(sess.quiet = TRUE)
.sess_env$interactive_token <- cfg$token
Sys.setenv(JGD_SOCKET = cfg$jgd, SESS_ENDPOINT = cfg$sess)
connect(endpoint = cfg$sess, plot_backend = if (isTRUE(cfg$useJgd)) "jgd" else "standard")
if (is.null(.sess_env$con)) {
attached <- connect(
endpoint = cfg$sess,
plot_backend = if (isTRUE(cfg$useJgd)) "jgd" else "standard"
)
if (!isTRUE(attached)) {
stop("Could not connect to the session agent")
}
.sess_env$interactive_pid <- Sys.getpid()
Expand Down
Loading
Loading