diff options
| author | Alex Clayton <alex.clayton@isode.com> | 2016-03-16 16:44:36 (GMT) |
|---|---|---|
| committer | Alex Clayton <alex.clayton@isode.com> | 2016-03-22 11:17:07 (GMT) |
| commit | 59315e2f10c01451115b66b9f285e26735bbacbf (patch) | |
| tree | 7832de56ec846d2652ba15bff93f0b1ffc47f463 | |
| parent | f693c8e0fa9c6a051cdf6260f131db3d9355b49f (diff) | |
| download | stroke-59315e2f10c01451115b66b9f285e26735bbacbf.zip stroke-59315e2f10c01451115b66b9f285e26735bbacbf.tar.bz2 | |
Try to fix possible race condition in Connector and ProxiedConnection.
As per patch 'Fix possible race condition between Connection and
Connectors' (7eec2000d72f8fa597398704121d0b73a84ca284).
The issue occurs with ProxiedConnection that started connecting but do
not have an external reference anymore. As soon as the handlers of
the ProxiedConnection are disconnected from the signals of the
connection_ object, the remaining references to a shared
ProxiedConnection vanish and the ProxiedConnection is deleted, while
it still requires access to its members in
ProxiedConnection::handleConnectFinished().
Test-information: Unit tests pass ok.
Change-Id: I6c81009262dd51cda17b1b93a15edf968f40e464
| -rw-r--r-- | src/com/isode/stroke/network/Connector.java | 9 | ||||
| -rw-r--r-- | src/com/isode/stroke/network/ProxiedConnection.java | 10 |
2 files changed, 16 insertions, 3 deletions
diff --git a/src/com/isode/stroke/network/Connector.java b/src/com/isode/stroke/network/Connector.java index 6033a89..2823679 100644 --- a/src/com/isode/stroke/network/Connector.java +++ b/src/com/isode/stroke/network/Connector.java @@ -1,8 +1,8 @@ /* - * Copyright (c) 2010-2014, Isode Limited, London, England. + * Copyright (c) 2010-2016, Isode Limited, London, England. * All rights reserved. */ /* * Copyright (c) 2010 Remko Tronçon * All rights reserved. */ @@ -59,12 +59,19 @@ public class Connector { else { queryAddress(hostname); } } public void stop() { + if (currentConnectionConnectFinishedConnection != null) { + currentConnectionConnectFinishedConnection.disconnect(); + currentConnectionConnectFinishedConnection = null; + } + if (currentConnection != null) { + currentConnection.disconnect(); + } finish(null); } public final Signal2<Connection, com.isode.stroke.base.Error> onConnectFinished = new Signal2<Connection, com.isode.stroke.base.Error>(); private Connector(String hostname,int port, String serviceLookupPrefix, DomainNameResolver resolver, ConnectionFactory connectionFactory, TimerFactory timerFactory) { diff --git a/src/com/isode/stroke/network/ProxiedConnection.java b/src/com/isode/stroke/network/ProxiedConnection.java index 5eac083..69b4b90 100644 --- a/src/com/isode/stroke/network/ProxiedConnection.java +++ b/src/com/isode/stroke/network/ProxiedConnection.java @@ -8,12 +8,14 @@ * Licensed under the simplified BSD license. * See Documentation/Licenses/BSD-simplified.txt for more information. */ package com.isode.stroke.network; +import java.util.logging.Logger; + import com.isode.stroke.signals.SignalConnection; import com.isode.stroke.signals.Slot2; import com.isode.stroke.signals.Slot1; import com.isode.stroke.base.SafeByteArray; public abstract class ProxiedConnection extends Connection { @@ -27,12 +29,13 @@ public abstract class ProxiedConnection extends Connection { private HostAddressPort server_; private Connector connector_; private Connection connection_; private SignalConnection onDataReadConnection_; private SignalConnection onDisconnectedConnection_; private SignalConnection onConnectFinishedConnection; + private Logger logger_ = Logger.getLogger(this.getClass().getName()); public ProxiedConnection(DomainNameResolver resolver, ConnectionFactory connectionFactory, TimerFactory timerFactory, final String proxyHost, int proxyPort) { this.resolver_ = resolver; this.connectionFactory_ = connectionFactory; this.timerFactory_ = timerFactory; this.proxyHost_ = proxyHost; @@ -46,13 +49,13 @@ public abstract class ProxiedConnection extends Connection { cancelConnector(); if (connection_ != null) { onDataReadConnection_.disconnect(); onDisconnectedConnection_.disconnect(); } if (connected_) { - System.err.println("Warning: Connection was still established."); + logger_.warning("Warning: Connection was still established."); } } finally { super.finalize(); } } @@ -73,14 +76,17 @@ public abstract class ProxiedConnection extends Connection { } }); connector_.start(); } public void disconnect() { + cancelConnector(); connected_ = false; - connection_.disconnect(); + if (connection_ != null) { + connection_.disconnect(); + } } public void write(final SafeByteArray data) { connection_.write(data); } |
Swift