mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 02:39:05 +01:00
Handle interrupts that occur before the outgoing session starts.
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user