Skip to content
This repository was archived by the owner on Jun 18, 2024. It is now read-only.

Issue #425: Fix hanging StreamingSubscriptionConnection.close() #496

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectStreamException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownServiceException;
import java.util.ArrayList;
Expand Down Expand Up @@ -93,6 +94,8 @@ public interface IHandleResponseObject {
*/
private HttpWebRequest response;

private volatile boolean disconnecting;

/**
* Expected minimum frequency in response, in milliseconds.
*/
Expand Down Expand Up @@ -222,15 +225,11 @@ private void parseResponses() {
} catch (SocketTimeoutException ex) {
// The connection timed out.
this.disconnect(HangingRequestDisconnectReason.Timeout, ex);
} catch (UnknownServiceException ex) {
// Stream is closed, so disconnect.
this.disconnect(HangingRequestDisconnectReason.Exception, ex);
} catch (ObjectStreamException ex) {
// Stream is closed, so disconnect.
this.disconnect(HangingRequestDisconnectReason.Exception, ex);
} catch (IOException ex) {
// Stream is closed, so disconnect.
this.disconnect(HangingRequestDisconnectReason.Exception, ex);
if (!(disconnecting && isSocketClosed(ex))) {
// Stream was closed due to an error
this.disconnect(HangingRequestDisconnectReason.Exception, ex);
}
} catch (UnsupportedOperationException ex) {
LOG.error(ex);
// This is thrown if we close the stream during a
Expand All @@ -239,13 +238,26 @@ private void parseResponses() {
//simply results in a long-running connection.
this.disconnect(HangingRequestDisconnectReason.UserInitiated, null);
} catch (Exception ex) {
// Stream is closed, so disconnect.
this.disconnect(HangingRequestDisconnectReason.Exception, ex);
if (!(disconnecting && isSocketClosed(ex))) {
// Stream was closed due to an error
this.disconnect(HangingRequestDisconnectReason.Exception, ex);
}

} finally {
IOUtils.closeQuietly(responseCopy);
}
}

private static boolean isSocketClosed(Throwable e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this about getting smth like the rootcause?

while (e != null) {
if (e instanceof SocketException) {
return true;
}
e = e.getCause();
}
return false;
}

private boolean isConnected;

/**
Expand All @@ -266,7 +278,8 @@ private void setIsConnected(boolean value) {
*/
public void disconnect() {
synchronized (this) {
IOUtils.closeQuietly(this.response);
disconnecting = true;
this.response.releaseConnection();
this.disconnect(HangingRequestDisconnectReason.UserInitiated, null);
}
}
Expand All @@ -279,7 +292,8 @@ public void disconnect() {
*/
public void disconnect(HangingRequestDisconnectReason reason, Exception exception) {
if (this.isConnected()) {
IOUtils.closeQuietly(this.response);
disconnecting = true;
this.response.releaseConnection();
this.internalOnDisconnect(reason, exception);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ public void close() throws IOException {
httpPost = null;
}

@Override
public void releaseConnection() {
if (httpPost != null) {
httpPost.releaseConnection();
}
}

/**
* Prepares the request by setting appropriate headers, authentication, timeouts, etc.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,8 @@ public void setCredentials(String domain, String user, String pwd) {
*/
public abstract void close() throws IOException;

public abstract void releaseConnection();

/**
* Prepare connection.
*/
Expand Down