Skip to content

Commit cb5ae42

Browse files
authored
Fix disabling send/receive timers after send/receive operations completed. (#5138)
1 parent d1c53ef commit cb5ae42

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/System.Private.ServiceModel/tests/Scenarios/Binding/Tcp/NetTcpBindingTests.4.0.0.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.ServiceModel;
7+
using System.Threading;
68
using System.Threading.Tasks;
79
using Infrastructure.Common;
810
using Xunit;
@@ -79,4 +81,41 @@ public static async Task SecurityModeNone_Echo_RoundTrips_String_SyncAfterAsync(
7981
ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
8082
}
8183
}
84+
85+
// Test for https://github.com/dotnet/wcf/issues/5134
86+
[WcfFact]
87+
[OuterLoop]
88+
public static void ReceiveTimeout_Applied()
89+
{
90+
string testString = "Hello";
91+
ChannelFactory<IWcfService> factory = null;
92+
IWcfService serviceProxy = null;
93+
94+
try
95+
{
96+
// *** SETUP *** \\
97+
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
98+
binding.OpenTimeout = TimeSpan.FromSeconds(10);
99+
binding.SendTimeout = binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
100+
factory = new ChannelFactory<IWcfService>(binding, new EndpointAddress(Endpoints.Tcp_NoSecurity_Address));
101+
serviceProxy = factory.CreateChannel();
102+
103+
// *** EXECUTE *** \\
104+
((IClientChannel)serviceProxy).Open();
105+
Thread.Sleep(binding.OpenTimeout + TimeSpan.FromSeconds(5));
106+
string result = serviceProxy.Echo(testString);
107+
108+
// *** VALIDATE *** \\
109+
Assert.Equal(testString, result);
110+
111+
// *** CLEANUP *** \\
112+
((ICommunicationObject)serviceProxy).Close();
113+
factory.Close();
114+
}
115+
finally
116+
{
117+
// *** ENSURE CLEANUP *** \\
118+
ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
119+
}
120+
}
82121
}

src/System.ServiceModel.NetTcp/src/System/ServiceModel/Channels/SocketConnection.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ private async ValueTask<int> ReadCoreAsync(Memory<byte> buffer, TimeSpan timeout
233233
}
234234
finally
235235
{
236+
CancelReceiveTimer();
236237
// Restore the current ExecutionContext
237238
if (restoreFlow)
238239
ExecutionContext.RestoreFlow();
@@ -319,6 +320,7 @@ public async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, bool immediate, T
319320
finally
320321
{
321322
_asyncWritePending = false;
323+
CancelSendTimer();
322324
// Restore the current ExecutionContext
323325
if (restoreFlow)
324326
ExecutionContext.RestoreFlow();
@@ -635,13 +637,19 @@ private bool ShouldUpdateTimeout(DateTime oldTimeoutDeadline, TimeSpan newTimeou
635637
private static void OnReceiveTimeout(object state)
636638
{
637639
SocketConnection thisPtr = (SocketConnection)state;
638-
thisPtr.Abort(SR.Format(SR.SocketAbortedReceiveTimedOut, thisPtr._asyncReceiveTimeout), TransferOperation.Read);
640+
if (thisPtr._receiveTimerEnabled)
641+
{
642+
thisPtr.Abort(SR.Format(SR.SocketAbortedReceiveTimedOut, thisPtr._asyncReceiveTimeout), TransferOperation.Read);
643+
}
639644
}
640645

641646
private static void OnSendTimeout(object state)
642647
{
643648
SocketConnection thisPtr = (SocketConnection)state;
644-
thisPtr.Abort(TraceEventType.Warning, SR.Format(SR.SocketAbortedSendTimedOut, thisPtr._asyncSendTimeout), TransferOperation.Write);
649+
if (thisPtr._sendTimerEnabled)
650+
{
651+
thisPtr.Abort(TraceEventType.Warning, SR.Format(SR.SocketAbortedSendTimedOut, thisPtr._asyncSendTimeout), TransferOperation.Write);
652+
}
645653
}
646654

647655
private Exception ConvertObjectDisposedException(ObjectDisposedException originalException, TransferOperation transferOperation)

0 commit comments

Comments
 (0)