mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 04:39:54 +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 java.util.concurrent.Executor;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
import javax.annotation.concurrent.GuardedBy;
|
||||||
|
|
||||||
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
|
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
|
||||||
|
|
||||||
@@ -33,8 +34,30 @@ abstract class DuplexSyncConnection extends SyncConnection {
|
|||||||
final TransportConnectionWriter writer;
|
final TransportConnectionWriter writer;
|
||||||
final TransportProperties remote;
|
final TransportProperties remote;
|
||||||
|
|
||||||
|
private final Object interruptLock = new Object();
|
||||||
|
|
||||||
|
@GuardedBy("interruptLock")
|
||||||
@Nullable
|
@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,
|
DuplexSyncConnection(KeyManager keyManager,
|
||||||
ConnectionRegistry connectionRegistry,
|
ConnectionRegistry connectionRegistry,
|
||||||
@@ -57,9 +80,7 @@ abstract class DuplexSyncConnection extends SyncConnection {
|
|||||||
void onReadError(boolean recognised) {
|
void onReadError(boolean recognised) {
|
||||||
disposeOnError(reader, recognised);
|
disposeOnError(reader, recognised);
|
||||||
disposeOnError(writer);
|
disposeOnError(writer);
|
||||||
// Interrupt the outgoing session so it finishes
|
interruptOutgoingSession();
|
||||||
SyncSession out = outgoingSession;
|
|
||||||
if (out != null) out.interrupt();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void onWriteError() {
|
void onWriteError() {
|
||||||
|
|||||||
@@ -68,9 +68,7 @@ class IncomingDuplexSyncConnection extends DuplexSyncConnection
|
|||||||
// Create and run the incoming session
|
// Create and run the incoming session
|
||||||
createIncomingSession(ctx, reader).run();
|
createIncomingSession(ctx, reader).run();
|
||||||
reader.dispose(false, true);
|
reader.dispose(false, true);
|
||||||
// Interrupt the outgoing session so it finishes cleanly
|
interruptOutgoingSession();
|
||||||
SyncSession out = outgoingSession;
|
|
||||||
if (out != null) out.interrupt();
|
|
||||||
} catch (DbException | IOException e) {
|
} catch (DbException | IOException e) {
|
||||||
logException(LOG, WARNING, e);
|
logException(LOG, WARNING, e);
|
||||||
onReadError(true);
|
onReadError(true);
|
||||||
@@ -91,7 +89,7 @@ class IncomingDuplexSyncConnection extends DuplexSyncConnection
|
|||||||
try {
|
try {
|
||||||
// Create and run the outgoing session
|
// Create and run the outgoing session
|
||||||
SyncSession out = createDuplexOutgoingSession(ctx, writer);
|
SyncSession out = createDuplexOutgoingSession(ctx, writer);
|
||||||
outgoingSession = out;
|
setOutgoingSession(out);
|
||||||
out.run();
|
out.run();
|
||||||
writer.dispose(false);
|
writer.dispose(false);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ class OutgoingDuplexSyncConnection extends DuplexSyncConnection
|
|||||||
try {
|
try {
|
||||||
// Create and run the outgoing session
|
// Create and run the outgoing session
|
||||||
SyncSession out = createDuplexOutgoingSession(ctx, writer);
|
SyncSession out = createDuplexOutgoingSession(ctx, writer);
|
||||||
outgoingSession = out;
|
setOutgoingSession(out);
|
||||||
out.run();
|
out.run();
|
||||||
writer.dispose(false);
|
writer.dispose(false);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -104,9 +104,7 @@ class OutgoingDuplexSyncConnection extends DuplexSyncConnection
|
|||||||
// Create and run the incoming session
|
// Create and run the incoming session
|
||||||
createIncomingSession(ctx, reader).run();
|
createIncomingSession(ctx, reader).run();
|
||||||
reader.dispose(false, true);
|
reader.dispose(false, true);
|
||||||
// Interrupt the outgoing session so it finishes cleanly
|
interruptOutgoingSession();
|
||||||
SyncSession out = outgoingSession;
|
|
||||||
if (out != null) out.interrupt();
|
|
||||||
} catch (DbException | IOException e) {
|
} catch (DbException | IOException e) {
|
||||||
logException(LOG, WARNING, e);
|
logException(LOG, WARNING, e);
|
||||||
onReadError();
|
onReadError();
|
||||||
|
|||||||
Reference in New Issue
Block a user