|
34 | 34 | import java.util.List; |
35 | 35 | import java.util.Map; |
36 | 36 | import java.util.Optional; |
| 37 | +import java.util.concurrent.CancellationException; |
37 | 38 | import java.util.concurrent.CompletableFuture; |
38 | | -import java.util.concurrent.CompletionException; |
39 | 39 | import java.util.concurrent.ExecutorService; |
40 | 40 | import java.util.concurrent.Executors; |
41 | 41 | import java.util.concurrent.ScheduledExecutorService; |
@@ -90,6 +90,9 @@ final class ServerProcesses { |
90 | 90 | /** Stops idle servers. */ |
91 | 91 | private final ScheduledExecutorService reaper; |
92 | 92 |
|
| 93 | + /** Whether all servers were stopped, for good (see {@link #shutdown}). */ |
| 94 | + private boolean shutdown; |
| 95 | + |
93 | 96 | /** |
94 | 97 | * Servers stopped after ten minutes unused, four at most (or as the system |
95 | 98 | * properties {@code scijava.lsp.idle}, in ms, and {@code scijava.lsp.max} |
@@ -136,16 +139,21 @@ synchronized Slot slot(final Environment env) { |
136 | 139 | return known != null ? known : new Slot(env); |
137 | 140 | } |
138 | 141 |
|
139 | | - /** Stops all servers. */ |
| 142 | + /** Stops all servers, for good: none are started after. */ |
140 | 143 | synchronized void shutdown() { |
| 144 | + shutdown = true; |
141 | 145 | reaper.shutdownNow(); |
142 | | - for (final Slot slot : slots.values()) { |
| 146 | + // NB: A copy, as failing a start forgets its slot (see Slot.start). |
| 147 | + for (final Slot slot : new ArrayList<>(slots.values())) { |
143 | 148 | final CompletableFuture<ServerProcess> s = slot.server; |
144 | 149 | if (s == null) continue; |
145 | | - // NB: Not via the starter, which stops next (dropping what it has not |
146 | | - // begun): those started, now; those starting, once started. |
| 150 | + // NB: Those started, now. Those starting fail at once, as their waiters |
| 151 | + // must not wait on the starter, which stops next (dropping what it has |
| 152 | + // not begun); a start that finishes anyway shuts its process down |
| 153 | + // itself (see Slot.start). |
147 | 154 | if (s.isDone()) s.thenAccept(ServerProcess::shutdown); |
148 | | - else s.thenAcceptAsync(ServerProcess::shutdown); |
| 155 | + else s.completeExceptionally(new CancellationException( |
| 156 | + "Shutting down all servers")); |
149 | 157 | slot.server = null; |
150 | 158 | } |
151 | 159 | slots.clear(); |
@@ -220,10 +228,16 @@ private Slot(final Environment env) { |
220 | 228 | /** |
221 | 229 | * Gets the process, starting it if needed. Never blocks. |
222 | 230 | * |
223 | | - * @return The process, once started; fails if it could not be. |
| 231 | + * @return The process, once started; fails if it could not be, or if all |
| 232 | + * servers were shut down. |
224 | 233 | */ |
225 | 234 | CompletableFuture<ServerProcess> process() { |
226 | 235 | synchronized (ServerProcesses.this) { |
| 236 | + // NB: Shut down? Then no starter to start one, nor slots to keep it. |
| 237 | + if (shutdown) { |
| 238 | + return CompletableFuture.failedFuture(new CancellationException( |
| 239 | + "All servers were shut down")); |
| 240 | + } |
227 | 241 | // NB: Replaced while this one had no process? Ask that one. |
228 | 242 | final Slot known = slots.get(env); |
229 | 243 | if (known != null && known != this) return known.process(); |
@@ -256,30 +270,52 @@ private void stop() { |
256 | 270 |
|
257 | 271 | private void start() { |
258 | 272 | failed = -1; |
259 | | - final CompletableFuture<ServerProcess> s = CompletableFuture |
260 | | - .supplyAsync(() -> { |
261 | | - try { |
262 | | - log.debug("Starting " + launcher.language() + |
263 | | - " language server for " + env); |
264 | | - return ServerProcess.start(launcher, env, log, INIT_TIMEOUT); |
| 273 | + final CompletableFuture<ServerProcess> s = new CompletableFuture<>(); |
| 274 | + server = s; |
| 275 | + starter.execute(() -> { |
| 276 | + try { |
| 277 | + log.debug("Starting " + launcher.language() + |
| 278 | + " language server for " + env); |
| 279 | + final ServerProcess p = ServerProcess.start(launcher, env, log, |
| 280 | + INIT_TIMEOUT); |
| 281 | + if (!s.complete(p)) { |
| 282 | + // Already failed by a shutdown, so no one else will stop it. |
| 283 | + p.shutdown(); |
265 | 284 | } |
266 | | - catch (final Exception exc) { |
| 285 | + } |
| 286 | + catch (final Throwable t) { |
| 287 | + if (noteFailure(s)) { |
| 288 | + // Could not start (e.g. the process failed, or did not |
| 289 | + // initialize in time): say so (it waits before trying again). |
| 290 | + s.completeExceptionally(t); |
267 | 291 | log.warn("Could not start " + launcher.language() + |
268 | | - " language server for " + env, exc); |
269 | | - throw new CompletionException(exc); |
| 292 | + " language server for " + env, t); |
270 | 293 | } |
271 | | - }, starter); |
272 | | - // NB: Before the callback, which runs at once if the start failed. |
273 | | - server = s; |
274 | | - s.whenComplete((started, error) -> { |
275 | | - if (error == null) return; |
276 | | - synchronized (ServerProcesses.this) { |
277 | | - if (server != s) return; |
| 294 | + else { |
| 295 | + // NB: Interrupted by a shutdown, which failed it already: |
| 296 | + // nothing to warn of, nor note (its slot is gone). |
| 297 | + } |
| 298 | + } |
| 299 | + }); |
| 300 | + } |
| 301 | + |
| 302 | + /** |
| 303 | + * Notes that the given start failed, before its users can see it (lest |
| 304 | + * they ask again too soon), unless replaced since. |
| 305 | + * |
| 306 | + * @return Whether it failed by itself; false if a shutdown failed it |
| 307 | + * first. |
| 308 | + */ |
| 309 | + private boolean noteFailure(final CompletableFuture<ServerProcess> s) { |
| 310 | + synchronized (ServerProcesses.this) { |
| 311 | + if (s.isDone()) return false; |
| 312 | + if (server == s) { |
278 | 313 | failed = clock.getAsLong(); |
279 | 314 | // NB: Holds no process. Its users still wait out RETRY_DELAY. |
280 | 315 | slots.remove(env, this); |
281 | 316 | } |
282 | | - }); |
| 317 | + return true; |
| 318 | + } |
283 | 319 | } |
284 | 320 |
|
285 | 321 | /** The process, if started (successfully); null otherwise. */ |
|
0 commit comments