From a7abdcf4cfb17cd661d8601b77f1ee15ca71e8ad Mon Sep 17 00:00:00 2001 From: Tal Hadad Date: Wed, 2 Aug 2023 15:12:52 +0300 Subject: [PATCH] expose detach function to R API --- R/session/init_late.R | 1 + R/session/vsc.R | 16 ++++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/R/session/init_late.R b/R/session/init_late.R index 0e544328..2b8f061d 100644 --- a/R/session/init_late.R +++ b/R/session/init_late.R @@ -17,6 +17,7 @@ source(file.path(dir_init, "vsc.R"), local = .vsc) exports <- local({ .vsc <- .vsc .vsc.attach <- .vsc$attach +.vsc.detach <- .vsc$detach .vsc.view <- .vsc$show_dataview .vsc.browser <- .vsc$show_browser .vsc.viewer <- .vsc$show_viewer diff --git a/R/session/vsc.R b/R/session/vsc.R index bcb8d3a0..4f4743ff 100644 --- a/R/session/vsc.R +++ b/R/session/vsc.R @@ -655,8 +655,7 @@ attach <- function(host = "127.0.0.1", port = NA) { rstudioapi_util_env$update_addin_registry(addin_registry) } if (!is.na(request_tcp_connection)) { - close(request_tcp_connection) - request_tcp_connection <<- NA + detach() } if (!is.na(port)) { request_tcp_connection <<- socketConnection( @@ -685,6 +684,14 @@ attach <- function(host = "127.0.0.1", port = NA) { ) } +detach <- function() { + request("detach") + if (!is.na(request_tcp_connection)) { + close(request_tcp_connection) + request_tcp_connection <<- NA + } +} + path_to_uri <- function(path) { if (length(path) == 0) { return(character()) @@ -965,9 +972,6 @@ reg.finalizer(.GlobalEnv, function(e) { # TODO: When exiting radian by EOF("CTRL+D") when coonecting to vsc by TCP, # the TCP connection is getting closed before we're able to call detach... tryCatch({ - .vsc$request("detach") - if (!is.na(request_tcp_connection)) { - close(request_tcp_connection) - } + detach() }, error = function(e) NULL) }, onexit = TRUE)