Handle interrupts that occur before the outgoing session starts.

This commit is contained in:
akwizgran
2020-05-12 17:42:33 +01:00
parent 710b6d18ce
commit 9e6d67f13d
3 changed files with 29 additions and 12 deletions

View File

@@ -21,6 +21,7 @@ import java.io.IOException;
import java.util.concurrent.Executor;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
@@ -33,8 +34,30 @@ abstract class DuplexSyncConnection extends SyncConnection {
final TransportConnectionWriter writer;
final TransportProperties remote;
private final Object interruptLock = new Object();
@GuardedBy("interruptLock")
@Nullable
volatile SyncSession outgoingSession = null;
private SyncSession outgoingSession = null;
@GuardedBy("interruptLock")
private boolean interruptWaiting = false;
void interruptOutgoingSession() {
synchronized (interruptLock) {
if (outgoingSession == null) interruptWaiting = true;
else outgoingSession.interrupt();
}
}
void setOutgoingSession(SyncSession outgoingSession) {
synchronized (interruptLock) {
this.outgoingSession = outgoingSession;
if (interruptWaiting) {
outgoingSession.interrupt();
interruptWaiting = false;
}
}
}
DuplexSyncConnection(KeyManager keyManager,
ConnectionRegistry connectionRegistry,
@@ -57,9 +80,7 @@ abstract class DuplexSyncConnection extends SyncConnection {
void onReadError(boolean recognised) {
disposeOnError(reader, recognised);
disposeOnError(writer);
// Interrupt the outgoing session so it finishes
SyncSession out = outgoingSession;
if (out != null) out.interrupt();
interruptOutgoingSession();
}
void onWriteError() {

View File

@@ -68,9 +68,7 @@ class IncomingDuplexSyncConnection extends DuplexSyncConnection
// Create and run the incoming session
createIncomingSession(ctx, reader).run();
reader.dispose(false, true);
// Interrupt the outgoing session so it finishes cleanly
SyncSession out = outgoingSession;
if (out != null) out.interrupt();
interruptOutgoingSession();
} catch (DbException | IOException e) {
logException(LOG, WARNING, e);
onReadError(true);
@@ -91,7 +89,7 @@ class IncomingDuplexSyncConnection extends DuplexSyncConnection
try {
// Create and run the outgoing session
SyncSession out = createDuplexOutgoingSession(ctx, writer);
outgoingSession = out;
setOutgoingSession(out);
out.run();
writer.dispose(false);
} catch (IOException e) {

View File

@@ -60,7 +60,7 @@ class OutgoingDuplexSyncConnection extends DuplexSyncConnection
try {
// Create and run the outgoing session
SyncSession out = createDuplexOutgoingSession(ctx, writer);
outgoingSession = out;
setOutgoingSession(out);
out.run();
writer.dispose(false);
} catch (IOException e) {
@@ -104,9 +104,7 @@ class OutgoingDuplexSyncConnection extends DuplexSyncConnection
// Create and run the incoming session
createIncomingSession(ctx, reader).run();
reader.dispose(false, true);
// Interrupt the outgoing session so it finishes cleanly
SyncSession out = outgoingSession;
if (out != null) out.interrupt();
interruptOutgoingSession();
} catch (DbException | IOException e) {
logException(LOG, WARNING, e);
onReadError();