Skip to content

Commit 174e05e

Browse files
committed
Merge branch 'autodispose'
Let SciJava Contexts dispose themselves automatically at JVM shutdown. Closes #458.
2 parents 545caf0 + 7daf667 commit 174e05e

File tree

3 files changed

+87
-11
lines changed

3 files changed

+87
-11
lines changed

src/main/java/org/scijava/Context.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ public class Context implements Disposable, AutoCloseable {
9797
*/
9898
private boolean strict;
9999

100+
/**
101+
* False if the context is currently active; true if the context
102+
* has already been disposed, or is in the process of being disposed.
103+
*/
104+
private boolean disposed;
105+
100106
/**
101107
* Creates a new SciJava application context with all available services.
102108
*
@@ -284,6 +290,9 @@ public Context(final Collection<Class<? extends Service>> serviceClasses,
284290
new ServiceHelper(this, serviceClasses, strict);
285291
serviceHelper.loadServices();
286292
}
293+
294+
// If JVM shuts down with context still active, clean up after ourselves.
295+
Runtime.getRuntime().addShutdownHook(new Thread(() -> doDispose(false)));
287296
}
288297

289298
// -- Context methods --
@@ -418,16 +427,8 @@ public boolean isInjectable(final Class<?> type) {
418427

419428
@Override
420429
public void dispose() {
421-
final EventService eventService = getService(EventService.class);
422-
if (eventService != null) eventService.publish(new ContextDisposingEvent());
423-
424-
// NB: Dispose services in reverse order.
425-
// This may or may not actually be necessary, but seems safer, since
426-
// dependent services will be disposed *before* their dependencies.
427-
final List<Service> services = serviceIndex.getAll();
428-
for (int s = services.size() - 1; s >= 0; s--) {
429-
services.get(s).dispose();
430-
}
430+
if (disposed) return;
431+
doDispose(true);
431432
}
432433

433434
// -- AutoCloseable methods --
@@ -580,6 +581,23 @@ private String createMissingServiceMessage(
580581
return msg.toString();
581582
}
582583

584+
private synchronized void doDispose(final boolean announce) {
585+
if (disposed) return;
586+
disposed = true;
587+
if (announce) {
588+
final EventService eventService = getService(EventService.class);
589+
if (eventService != null) eventService.publish(new ContextDisposingEvent());
590+
}
591+
592+
// NB: Dispose services in reverse order.
593+
// This may or may not actually be necessary, but seems safer, since
594+
// dependent services will be disposed *before* their dependencies.
595+
final List<Service> services = serviceIndex.getAll();
596+
for (int s = services.size() - 1; s >= 0; s--) {
597+
services.get(s).dispose();
598+
}
599+
}
600+
583601
private static PluginIndex plugins(final boolean empty) {
584602
return empty ? new PluginIndex(null) : null;
585603
}

src/main/java/org/scijava/thread/DefaultThreadService.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,14 @@ public synchronized void dispose() {
173173
@Override
174174
public Thread newThread(final Runnable r) {
175175
final String threadName = contextThreadPrefix() + nextThread++;
176-
return new Thread(r, threadName);
176+
final Thread thread = new Thread(r, threadName);
177+
// NB: Use daemon threads for the thread pool, so that idling threads do
178+
// not prevent the JVM shutdown sequence from starting. The application
179+
// context, and therefore the thread service, will try to dispose itself
180+
// upon JVM shutdown, which will invoke executor.shutdown(), so there
181+
// will be a chance for these threads to complete any pending work.
182+
thread.setDaemon(true);
183+
return thread;
177184
}
178185

179186
// -- Helper methods --
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2023 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package org.scijava;
31+
32+
import org.junit.Test;
33+
34+
/**
35+
* Tests disposal of {@link Context}s.
36+
*
37+
* @author Curtis Rueden
38+
*/
39+
public class ContextDisposalTest {
40+
41+
/**
42+
* Tests that a {@link Context} can be disposed more than once without
43+
* throwing an exception.
44+
*/
45+
@Test
46+
public void testDoubleDisposal() {
47+
final Context context = new Context();
48+
context.dispose();
49+
context.dispose();
50+
}
51+
}

0 commit comments

Comments
 (0)