Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Don't capture AsyncLocals into SQL global timers (#26065)
Browse files Browse the repository at this point in the history
* Don't capture AsyncLocals into SQL global timers

* Feedback
  • Loading branch information
benaadams authored and stephentoub committed Jan 30, 2018
1 parent 36ae610 commit 093126c
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 13 deletions.
35 changes: 35 additions & 0 deletions src/Common/src/System/Data/Common/AdapterUtil.Drivers.cs
@@ -0,0 +1,35 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Threading;

namespace System.Data.Common
{
internal static partial class ADP
{

internal static Timer UnsafeCreateTimer(TimerCallback callback, object state, int dueTime, int period)
{
// Don't capture the current ExecutionContext and its AsyncLocals onto
// a global timer causing them to live forever
bool restoreFlow = false;
try
{
if (!ExecutionContext.IsFlowSuppressed())
{
ExecutionContext.SuppressFlow();
restoreFlow = true;
}

return new Timer(callback, state, dueTime, period);
}
finally
{
// Restore the current ExecutionContext
if (restoreFlow)
ExecutionContext.RestoreFlow();
}
}
}
}
3 changes: 3 additions & 0 deletions src/System.Data.SqlClient/src/System.Data.SqlClient.csproj
Expand Up @@ -55,6 +55,9 @@
<Compile Include="$(CommonPath)\System\Data\Common\AdapterUtil.cs">
<Link>System\Data\Common\AdapterUtil.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\System\Data\Common\AdapterUtil.Drivers.cs">
<Link>System\Data\Common\AdapterUtil.Drivers.cs</Link>
</Compile>
<Compile Include="System\Data\Common\AdapterUtil.SqlClient.cs" />
<Compile Include="System\Data\Common\SR.cs" />
<Compile Include="System\Data\Common\DbConnectionOptions.cs" />
Expand Down
Expand Up @@ -123,11 +123,12 @@ virtual internal DbConnectionPoolGroupProviderInfo CreateConnectionPoolGroupProv
return null;
}

private Timer CreatePruningTimer()
{
TimerCallback callback = new TimerCallback(PruneConnectionPoolGroups);
return new Timer(callback, null, PruningDueTime, PruningPeriod);
}
private Timer CreatePruningTimer() =>
ADP.UnsafeCreateTimer(
new TimerCallback(PruneConnectionPoolGroups),
null,
PruningDueTime,
PruningPeriod);

protected DbConnectionOptions FindConnectionOptions(DbConnectionPoolKey key)
{
Expand Down
Expand Up @@ -664,10 +664,12 @@ internal void Clear()
ReclaimEmancipatedObjects();
}

private Timer CreateCleanupTimer()
{
return (new Timer(new TimerCallback(this.CleanupCallback), null, _cleanupWait, _cleanupWait));
}
private Timer CreateCleanupTimer() =>
ADP.UnsafeCreateTimer(
new TimerCallback(CleanupCallback),
null,
_cleanupWait,
_cleanupWait);

private DbConnectionInternal CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
{
Expand Down Expand Up @@ -728,6 +730,7 @@ private DbConnectionInternal CreateObject(DbConnection owningObject, DbConnectio

// timer allocation has to be done out of CER block
Timer t = new Timer(new TimerCallback(this.ErrorCallback), null, Timeout.Infinite, Timeout.Infinite);

bool timerIsNotDisposed;
try { }
finally
Expand Down
Expand Up @@ -612,7 +612,12 @@ private void Restart(object unused)
_timeoutParam.Value = _defaultWaitforTimeout; // If success, reset to default for re-queue.
AsynchronouslyQueryServiceBrokerQueue();
_errorState = false;
_retryTimer = null;
Timer retryTimer = _retryTimer;
if (retryTimer != null)
{
_retryTimer = null;
retryTimer.Dispose();
}
}
}

Expand Down Expand Up @@ -1470,4 +1475,4 @@ private void AppDomainUnloading(object state)

return stopped;
}
}
}
Expand Up @@ -80,7 +80,11 @@ private SqlDependencyPerAppDomainDispatcher()
_notificationIdToDependenciesHash = new Dictionary<string, DependencyList>();
_commandHashToNotificationId = new Dictionary<string, string>();

_timeoutTimer = new Timer(new TimerCallback(TimeoutTimerCallback), null, Timeout.Infinite, Timeout.Infinite);
_timeoutTimer = ADP.UnsafeCreateTimer(
new TimerCallback(TimeoutTimerCallback),
null,
Timeout.Infinite,
Timeout.Infinite);

SubscribeToAppDomainUnload();
}
Expand Down
Expand Up @@ -2295,7 +2295,11 @@ internal void ReadSni(TaskCompletionSource<object> completion)

if (_networkPacketTimeout == null)
{
_networkPacketTimeout = new Timer(OnTimeout, null, Timeout.Infinite, Timeout.Infinite);
_networkPacketTimeout = ADP.UnsafeCreateTimer(
new TimerCallback(OnTimeout),
null,
Timeout.Infinite,
Timeout.Infinite);
}

// -1 == Infinite
Expand Down

0 comments on commit 093126c

Please sign in to comment.