diff --git a/src/com/subgraph/orchid/circuits/CircuitIO.java b/src/com/subgraph/orchid/circuits/CircuitIO.java index e53cccac..bbba064d 100644 --- a/src/com/subgraph/orchid/circuits/CircuitIO.java +++ b/src/com/subgraph/orchid/circuits/CircuitIO.java @@ -38,7 +38,9 @@ public class CircuitIO implements DashboardRenderable { private final Map streamMap; private final Object relaySendLock = new Object(); + /** LOCKING: streamMap */ private boolean isMarkedForClose; + /** LOCKING: streamMap */ private boolean isClosed; CircuitIO(CircuitImpl circuit, Connection connection, int circuitId) { @@ -233,19 +235,22 @@ void sendCell(Cell cell) { } void markForClose() { + boolean shouldClose; synchronized (streamMap) { if(isMarkedForClose) { return; } isMarkedForClose = true; - if(streamMap.isEmpty()) { - closeCircuit(); - } + shouldClose = streamMap.isEmpty(); } + if (shouldClose) + closeCircuit(); } boolean isMarkedForClose() { - return isMarkedForClose; + synchronized (streamMap) { + return isMarkedForClose; + } } private void closeCircuit() { @@ -253,7 +258,9 @@ private void closeCircuit() { sendDestroyCell(Cell.ERROR_NONE); connection.removeCircuit(circuit); circuit.setStateDestroyed(); - isClosed = true; + synchronized(streamMap) { + isClosed = true; + } } void sendDestroyCell(int reason) { @@ -295,12 +302,13 @@ StreamImpl createNewStream(boolean autoclose) { } void removeStream(StreamImpl stream) { + boolean shouldClose; synchronized(streamMap) { streamMap.remove(stream.getStreamId()); - if(streamMap.isEmpty() && isMarkedForClose) { - closeCircuit(); - } + shouldClose = streamMap.isEmpty() && isMarkedForClose; } + if (shouldClose) + closeCircuit(); } List getActiveStreams() {