Skip to content

WCF Client Throws WebSocketException when the Service Abruptly Closes the Handshake #5819

@afifi-ins

Description

@afifi-ins

Describe the bug
When WCF service unexpectedly closes a WebSocket connection, WCF client throws WebSocketException.
System.Net.WebSockets.WebSocketException : The remote party closed the WebSocket connection without completing the close handshake.

To Reproduce
Steps to reproduce the behavior:

  1. Start SelfHostedCoreWCF service
  2. Run any WebSocket test on Linux such as WebSocket_Http_RequestReply_Streamed
  3. Close the connection from service side.
    The client throws System.Net.WebSockets.WebSocketException.

Expected behavior
When the service unexpectedly closes a WebSocket connection, the client is expected to throw CommunicationException instead of WebSocketException.
To workaround the bug, we provide a temporary fix and catch WebSocketException while closing the channel factory objects. See PR: #5802

  catch (System.Net.WebSockets.WebSocketException)
  {
      // WCF Client has a bug which throws WebSocketException when the server closes the connection.
      // The remote party closed the WebSocket connection without completing the close handshake.
      comObj.Abort();
  }

After this issue is resolved, we need to revert the code changes (remove the above code from ScenarioTestHelpers.cs) and do not catch WebSocketException when closing the channel.

Stack log
WebSocketTests.WebSocket_Http_RequestReply_Streamed(messageEncoding: Text) [FAIL]
System.Net.WebSockets.WebSocketException : The remote party closed the WebSocket connection without completing the close handshake.
Stack Trace:
at System.ServiceModel.Channels.WebSocketTransportDuplexSessionChannel.ThrowOnPendingException(Exception& pendingException)
at System.ServiceModel.Channels.WebSocketTransportDuplexSessionChannel.WebSocketMessageSource.ReceiveAsync(TimeSpan timeout)
at System.ServiceModel.Channels.SynchronizedMessageSource.ReceiveAsync(TimeSpan timeout)
at System.ServiceModel.Channels.TransportDuplexSessionChannel.EnsureInputClosedAsync(TimeSpan timeout)
at System.ServiceModel.Channels.TransportDuplexSessionChannel.OnCloseAsync(TimeSpan timeout)
at System.ServiceModel.Channels.WebSocketTransportDuplexSessionChannel.OnCloseAsync(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.OnCloseAsyncInternal(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.System.ServiceModel.IAsyncCommunicationObject.CloseAsync(TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.OnCloseAsync(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.OnCloseAsyncInternal(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.System.ServiceModel.IAsyncCommunicationObject.CloseAsync(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.CloseAsyncInternal(TimeSpan timeout)
at System.Runtime.TaskHelpers.WaitForCompletion(Task task)
at System.ServiceModel.Channels.CommunicationObject.Close(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Close()
at System.ServiceModel.Channels.ServiceChannelProxy.System.ServiceModel.ICommunicationObject.Close()
//src/System.Private.ServiceModel/tests/Common/Scenarios/ScenarioTestHelpers.cs(197,0): at ScenarioTestHelpers.CloseCommunicationObjects(ICommunicationObject[] objects)
/
/src/System.Private.ServiceModel/tests/Scenarios/Extensibility/WebSockets/WebSocketTests.4.1.0.cs(484,0):

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions