Skip to content
Snippets Groups Projects
Commit 1f4d8011 authored by akwizgran's avatar akwizgran
Browse files

Interrupt all messaging sessions when the app starts shutting down.

This makes it more likely that connections will be closed cleanly.
However, the interrupt() method is currently ineffective for incoming
sessions as it won't interrupt a blocking read, e.g. when the packet
reader is waiting for a packet.
parent 852a618c
No related branches found
No related tags found
No related merge requests found
package org.briarproject.api.event;
/** An event that is broadcast when the app is shutting down. */
public class ShutdownEvent extends Event {
}
...@@ -19,6 +19,8 @@ import javax.inject.Inject; ...@@ -19,6 +19,8 @@ import javax.inject.Inject;
import org.briarproject.api.db.DatabaseComponent; import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException; import org.briarproject.api.db.DbException;
import org.briarproject.api.event.EventBus;
import org.briarproject.api.event.ShutdownEvent;
import org.briarproject.api.lifecycle.LifecycleManager; import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.lifecycle.Service; import org.briarproject.api.lifecycle.Service;
import org.briarproject.api.system.Clock; import org.briarproject.api.system.Clock;
...@@ -30,6 +32,7 @@ class LifecycleManagerImpl implements LifecycleManager { ...@@ -30,6 +32,7 @@ class LifecycleManagerImpl implements LifecycleManager {
private final Clock clock; private final Clock clock;
private final DatabaseComponent db; private final DatabaseComponent db;
private final EventBus eventBus;
private final Collection<Service> services; private final Collection<Service> services;
private final Collection<ExecutorService> executors; private final Collection<ExecutorService> executors;
private final Semaphore startStopSemaphore = new Semaphore(1); private final Semaphore startStopSemaphore = new Semaphore(1);
...@@ -38,9 +41,10 @@ class LifecycleManagerImpl implements LifecycleManager { ...@@ -38,9 +41,10 @@ class LifecycleManagerImpl implements LifecycleManager {
private final CountDownLatch shutdownLatch = new CountDownLatch(1); private final CountDownLatch shutdownLatch = new CountDownLatch(1);
@Inject @Inject
LifecycleManagerImpl(Clock clock, DatabaseComponent db) { LifecycleManagerImpl(Clock clock, DatabaseComponent db, EventBus eventBus) {
this.clock = clock; this.clock = clock;
this.db = db; this.db = db;
this.eventBus = eventBus;
services = new CopyOnWriteArrayList<Service>(); services = new CopyOnWriteArrayList<Service>();
executors = new CopyOnWriteArrayList<ExecutorService>(); executors = new CopyOnWriteArrayList<ExecutorService>();
} }
...@@ -111,6 +115,7 @@ class LifecycleManagerImpl implements LifecycleManager { ...@@ -111,6 +115,7 @@ class LifecycleManagerImpl implements LifecycleManager {
} }
try { try {
LOG.info("Stopping services"); LOG.info("Stopping services");
eventBus.broadcast(new ShutdownEvent());
for(Service s : services) { for(Service s : services) {
boolean stopped = s.stop(); boolean stopped = s.stop();
if(LOG.isLoggable(INFO)) { if(LOG.isLoggable(INFO)) {
......
...@@ -30,6 +30,7 @@ import org.briarproject.api.event.MessageToRequestEvent; ...@@ -30,6 +30,7 @@ import org.briarproject.api.event.MessageToRequestEvent;
import org.briarproject.api.event.RemoteRetentionTimeUpdatedEvent; import org.briarproject.api.event.RemoteRetentionTimeUpdatedEvent;
import org.briarproject.api.event.RemoteSubscriptionsUpdatedEvent; import org.briarproject.api.event.RemoteSubscriptionsUpdatedEvent;
import org.briarproject.api.event.RemoteTransportsUpdatedEvent; import org.briarproject.api.event.RemoteTransportsUpdatedEvent;
import org.briarproject.api.event.ShutdownEvent;
import org.briarproject.api.event.TransportRemovedEvent; import org.briarproject.api.event.TransportRemovedEvent;
import org.briarproject.api.messaging.Ack; import org.briarproject.api.messaging.Ack;
import org.briarproject.api.messaging.MessagingSession; import org.briarproject.api.messaging.MessagingSession;
...@@ -169,6 +170,8 @@ class DuplexOutgoingSession implements MessagingSession, EventListener { ...@@ -169,6 +170,8 @@ class DuplexOutgoingSession implements MessagingSession, EventListener {
(RemoteTransportsUpdatedEvent) e; (RemoteTransportsUpdatedEvent) e;
if(r.getContactId().equals(contactId)) if(r.getContactId().equals(contactId))
dbExecutor.execute(new GenerateTransportAcks()); dbExecutor.execute(new GenerateTransportAcks());
} else if(e instanceof ShutdownEvent) {
interrupt();
} else if(e instanceof TransportRemovedEvent) { } else if(e instanceof TransportRemovedEvent) {
TransportRemovedEvent t = (TransportRemovedEvent) e; TransportRemovedEvent t = (TransportRemovedEvent) e;
if(t.getTransportId().equals(transportId)) interrupt(); if(t.getTransportId().equals(transportId)) interrupt();
......
...@@ -17,6 +17,7 @@ import org.briarproject.api.event.ContactRemovedEvent; ...@@ -17,6 +17,7 @@ import org.briarproject.api.event.ContactRemovedEvent;
import org.briarproject.api.event.Event; import org.briarproject.api.event.Event;
import org.briarproject.api.event.EventBus; import org.briarproject.api.event.EventBus;
import org.briarproject.api.event.EventListener; import org.briarproject.api.event.EventListener;
import org.briarproject.api.event.ShutdownEvent;
import org.briarproject.api.event.TransportRemovedEvent; import org.briarproject.api.event.TransportRemovedEvent;
import org.briarproject.api.messaging.Ack; import org.briarproject.api.messaging.Ack;
import org.briarproject.api.messaging.Message; import org.briarproject.api.messaging.Message;
...@@ -108,8 +109,7 @@ class IncomingSession implements MessagingSession, EventListener { ...@@ -108,8 +109,7 @@ class IncomingSession implements MessagingSession, EventListener {
} }
public void interrupt() { public void interrupt() {
// This won't interrupt a blocking read, but the read will throw an // FIXME: This won't interrupt a blocking read
// exception when the transport connection is closed
interrupted = true; interrupted = true;
} }
...@@ -117,6 +117,8 @@ class IncomingSession implements MessagingSession, EventListener { ...@@ -117,6 +117,8 @@ class IncomingSession implements MessagingSession, EventListener {
if(e instanceof ContactRemovedEvent) { if(e instanceof ContactRemovedEvent) {
ContactRemovedEvent c = (ContactRemovedEvent) e; ContactRemovedEvent c = (ContactRemovedEvent) e;
if(c.getContactId().equals(contactId)) interrupt(); if(c.getContactId().equals(contactId)) interrupt();
} else if(e instanceof ShutdownEvent) {
interrupt();
} else if(e instanceof TransportRemovedEvent) { } else if(e instanceof TransportRemovedEvent) {
TransportRemovedEvent t = (TransportRemovedEvent) e; TransportRemovedEvent t = (TransportRemovedEvent) e;
if(t.getTransportId().equals(transportId)) interrupt(); if(t.getTransportId().equals(transportId)) interrupt();
......
...@@ -21,6 +21,7 @@ import org.briarproject.api.event.ContactRemovedEvent; ...@@ -21,6 +21,7 @@ import org.briarproject.api.event.ContactRemovedEvent;
import org.briarproject.api.event.Event; import org.briarproject.api.event.Event;
import org.briarproject.api.event.EventBus; import org.briarproject.api.event.EventBus;
import org.briarproject.api.event.EventListener; import org.briarproject.api.event.EventListener;
import org.briarproject.api.event.ShutdownEvent;
import org.briarproject.api.event.TransportRemovedEvent; import org.briarproject.api.event.TransportRemovedEvent;
import org.briarproject.api.messaging.Ack; import org.briarproject.api.messaging.Ack;
import org.briarproject.api.messaging.MessagingSession; import org.briarproject.api.messaging.MessagingSession;
...@@ -121,6 +122,8 @@ class SimplexOutgoingSession implements MessagingSession, EventListener { ...@@ -121,6 +122,8 @@ class SimplexOutgoingSession implements MessagingSession, EventListener {
if(e instanceof ContactRemovedEvent) { if(e instanceof ContactRemovedEvent) {
ContactRemovedEvent c = (ContactRemovedEvent) e; ContactRemovedEvent c = (ContactRemovedEvent) e;
if(c.getContactId().equals(contactId)) interrupt(); if(c.getContactId().equals(contactId)) interrupt();
} else if(e instanceof ShutdownEvent) {
interrupt();
} else if(e instanceof TransportRemovedEvent) { } else if(e instanceof TransportRemovedEvent) {
TransportRemovedEvent t = (TransportRemovedEvent) e; TransportRemovedEvent t = (TransportRemovedEvent) e;
if(t.getTransportId().equals(transportId)) interrupt(); if(t.getTransportId().equals(transportId)) interrupt();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment