Debian Codegraph @

R/watchdog.R

raw
1
2 # whether or not the user has enabled the renv watchdog in this session
3 the$watchdog_enabled <- FALSE
4
5 # metadata related to the running watchdog process, if any
6 the$watchdog_process <- NULL
7
8 renv_watchdog_init <- function() {
9 the$watchdog_enabled <- renv_watchdog_enabled_impl()
10 }
11
12 renv_watchdog_enabled <- function() {
13 the$watchdog_enabled
14 }
15
16 renv_watchdog_check <- function() {
17
18 if (!renv_watchdog_enabled())
19 return(FALSE)
20
21 if (renv_watchdog_running())
22 return(TRUE)
23
24 renv_watchdog_start()
25
26 }
27
28 renv_watchdog_enabled_impl <- function() {
29
30 # skip in older versions of R; we require newer APIs
31 if (getRversion() < "4.0.0")
32 return(FALSE)
33
34 # allow override via environment variable
35 enabled <- Sys.getenv("RENV_WATCHDOG_ENABLED", unset = NA)
36 if (!is.na(enabled))
37 return(truthy(enabled))
38
39 # disable on Windows; need to understand CI test failures
40 # https://github.com/rstudio/renv/actions/runs/5273668333/jobs/9537353788#step:6:242
41 if (renv_platform_windows())
42 return(FALSE)
43
44 # disable in child processes launched by parallel frameworks
45 # https://github.com/rstudio/renv/issues/2223
46 if (renv_envvar_exists("CALLR_CHILD_R_LIBS"))
47 return(FALSE)
48
49 args <- commandArgs()
50 for (arg in args)
51 if (grepl("parallel:::.workRSOCK|parallel:::.slaveRSOCK", arg))
52 return(FALSE)
53
54 # skip during R CMD check (but not when running tests)
55 checking <- renv_envvar_exists("_R_CHECK_PACKAGE_NAME_")
56 if (checking && !testing())
57 return(FALSE)
58
59 # skip during R CMD build or R CMD INSTALL
60 # ... unless we are running tests on CI
61 building <-
62 renv_envvar_exists("R_PACKAGE_NAME") ||
63 renv_envvar_exists("R_PACKAGE_DIR")
64
65 if (building) {
66 ci <- Sys.getenv("CI", unset = "FALSE")
67 if (!truthy(ci))
68 return(FALSE)
69 }
70
71 # ok, we're enabled
72 TRUE
73
74 }
75
76 renv_watchdog_start <- function() {
77
78 the$watchdog_enabled <- tryCatch(
79 renv_watchdog_start_impl(),
80 error = function(e) {
81 warning(conditionMessage(e))
82 FALSE
83 }
84 )
85
86 }
87
88 renv_watchdog_start_impl <- function() {
89
90 # create a socket server -- this is used so the watchdog process
91 # can communicate what port it'll be listening on for messages
92 dlog("watchdog", "launching watchdog")
93 server <- renv_socket_server()
94 socket <- server$socket
95 defer(close(socket))
96
97 # generate script to invoke watchdog
98 script <- renv_scope_tempfile("renv-watchdog-", fileext = ".R")
99
100 # figure out library path -- need to dodge devtools::load_all()
101 nspath <- renv_namespace_path(.packageName)
102 library <- if (file.exists(file.path(nspath, "Meta/package.rds")))
103 dirname(nspath)
104 else
105 renv_libpaths_default()
106
107 code <- expr({
108 client <- list(pid = !!Sys.getpid(), port = !!server$port)
109 host <- loadNamespace(!!.packageName, lib.loc = !!library)
110 renv <- if (!is.null(host$renv)) host$renv else host
111 renv$renv_watchdog_server_start(client)
112 })
113
114 writeLines(deparse(code), con = script)
115
116 # debug logging
117 debugging <- Sys.getenv("RENV_WATCHDOG_DEBUG", unset = "FALSE")
118 stdout <- stderr <- if (truthy(debugging)) "" else FALSE
119
120 # launch the watchdog
121 local({
122 renv_scope_envvars(RENV_PROCESS_TYPE = "watchdog-server")
123 system2(
124 command = R(),
125 args = c("--vanilla", "-s", "-f", renv_shell_path(script)),
126 stdout = stdout,
127 stderr = stderr,
128 wait = FALSE
129 )
130 })
131
132 # wait for connection from watchdog server
133 dlog("watchdog", "watchdog process launched; waiting for message")
134 conn <- catch(renv_socket_accept(socket, open = "rb", timeout = 10L))
135 if (inherits(conn, "error")) {
136 dlog("watchdog", paste("error connecting to watchdog:", conditionMessage(conn)))
137 return(FALSE)
138 }
139
140 # store information about the running process
141 defer(close(conn))
142 the$watchdog_process <- unserialize(conn)
143
144 # return TRUE to indicate process was started
145 dlog("watchdog", "watchdog message received [pid == %i]", the$watchdog_process$pid)
146 TRUE
147
148 }
149
150 renv_watchdog_notify <- function(method, data = list()) {
151
152 tryCatch(
153 renv_watchdog_notify_impl(method, data),
154 error = warnify
155 )
156
157 }
158
159 renv_watchdog_notify_impl <- function(method, data = list()) {
160
161 # make sure the watchdog is running
162 if (!renv_watchdog_check())
163 return(FALSE)
164
165 # connect to the running server
166 port <- renv_watchdog_port()
167 conn <- renv_socket_connect(port, open = "wb")
168
169 # close the connection on exit
170 defer(close(conn))
171
172 # write message
173 message <- list(method = method, data = data)
174 serialize(message, connection = conn)
175
176 # TRUE indicates message was written
177 TRUE
178
179 }
180
181 renv_watchdog_request <- function(method, data = list()) {
182 tryCatch(
183 renv_watchdog_request_impl(method, data),
184 error = warnify
185 )
186 }
187
188 renv_watchdog_request_impl <- function(method, data = list()) {
189
190 # make sure the watchdog is running
191 if (!renv_watchdog_check())
192 return(FALSE)
193
194 # connect to the running server
195 port <- renv_watchdog_port()
196 outgoing <- renv_socket_connect(port, open = "wb")
197 defer(close(outgoing))
198
199 # create our own socket server
200 server <- renv_socket_server()
201 defer(close(server$socket))
202
203 # write message
204 message <- list(method = method, data = data, port = server$port)
205 serialize(message, connection = outgoing)
206
207 # now, open a new connection to get the response
208 incoming <- renv_socket_accept(server$socket, open = "rb")
209 defer(close(incoming))
210
211 # read the response
212 unserialize(connection = incoming)
213
214 }
215
216 renv_watchdog_pid <- function() {
217 the$watchdog_process$pid
218 }
219
220 renv_watchdog_port <- function() {
221 the$watchdog_process$port
222 }
223
224 renv_watchdog_running <- function() {
225 pid <- renv_watchdog_pid()
226 !is.null(pid) && renv_process_exists(pid)
227 }
228
229 renv_watchdog_unload <- function() {
230 renv_watchdog_shutdown()
231 }
232
233 renv_watchdog_terminate <- function() {
234 pid <- renv_watchdog_pid()
235 renv_process_kill(pid)
236 }
237
238 renv_watchdog_shutdown <- function() {
239
240 # nothing to do if watchdog isn't running
241 if (!renv_watchdog_running())
242 return(TRUE)
243
244 # tell watchdog to shutdown
245 renv_watchdog_notify("Shutdown")
246
247 # wait for process to exit (avoid RStudio bomb)
248 clock <- timer()
249 wait_until(function() {
250 !renv_watchdog_running() || clock$elapsed() > 1
251 })
252
253 if (!renv_watchdog_running())
254 return(TRUE)
255
256 # if it's still running, explicitly terminate it
257 renv_watchdog_terminate()
258
259 # wait for process to exit (avoid RStudio bomb)
260 clock <- timer()
261 wait_until(function() {
262 !renv_watchdog_running() || clock$elapsed() > 1
263 })
264
265 }