1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
//------------------------------------------------------------------------------
// <copyright file="LegacyAspNetSynchronizationContext.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Runtime.ExceptionServices;
using System.Security.Permissions;
using System.Threading;
using System.Web;
using System.Web.Util;
namespace System.Web {
// Represents a SynchronizationContext that has legacy behavior (<= FX 4.0) when it comes to asynchronous operations.
// Characterized by locking on the HttpApplication to synchronize work, dispatching Posts as Sends.
internal sealed class LegacyAspNetSynchronizationContext : AspNetSynchronizationContextBase {
private HttpApplication _application;
private Action<bool> _appVerifierCallback;
private bool _disabled;
private bool _syncCaller;
private bool _invalidOperationEncountered;
private int _pendingCount;
private ExceptionDispatchInfo _error;
private WaitCallback _lastCompletionCallback;
private object _lastCompletionCallbackLock;
internal LegacyAspNetSynchronizationContext(HttpApplication app) {
_application = app;
_appVerifierCallback = AppVerifier.GetSyncContextCheckDelegate(app);
_lastCompletionCallbackLock = new object();
}
private void CheckForRequestStateIfRequired() {
if (_appVerifierCallback != null) {
_appVerifierCallback(false);
}
}
private void CallCallback(SendOrPostCallback callback, Object state) {
CheckForRequestStateIfRequired();
// don't take app lock for sync caller to avoid deadlocks in case they poll for result
if (_syncCaller) {
CallCallbackPossiblyUnderLock(callback, state);
}
else {
lock (_application) {
CallCallbackPossiblyUnderLock(callback, state);
}
}
}
private void CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state) {
ThreadContext threadContext = null;
try {
threadContext = _application.OnThreadEnter();
try {
callback(state);
}
catch (Exception e) {
_error = ExceptionDispatchInfo.Capture(e);
}
}
finally {
if (threadContext != null) {
threadContext.DisassociateFromCurrentThread();
}
}
}
// this property no-ops using the legacy sync context
internal override bool AllowAsyncDuringSyncStages {
get;
set;
}
internal override int PendingOperationsCount {
get { return _pendingCount; }
}
internal override ExceptionDispatchInfo ExceptionDispatchInfo {
get { return _error; }
}
internal override void ClearError() {
_error = null;
}
// Dev11 Bug 70908: Race condition involving SynchronizationContext allows ASP.NET requests to be abandoned in the pipeline
//
// When the last completion occurs, the _pendingCount is decremented and then the _lastCompletionCallbackLock is acquired to get
// the _lastCompletionCallback. If the _lastCompletionCallback is non-null, then the last completion will invoke the callback;
// otherwise, the caller of PendingCompletion will handle the completion.
internal override bool PendingCompletion(WaitCallback callback) {
Debug.Assert(_lastCompletionCallback == null); // only one at a time
bool pending = false;
if (_pendingCount != 0) {
lock (_lastCompletionCallbackLock) {
if (_pendingCount != 0) {
pending = true;
_lastCompletionCallback = callback;
}
}
}
return pending;
}
public override void Send(SendOrPostCallback callback, Object state) {
#if DBG
Debug.Trace("Async", "Send");
Debug.Trace("AsyncStack", "Send from:\r\n" + GetDebugStackTrace());
#endif
CallCallback(callback, state);
}
public override void Post(SendOrPostCallback callback, Object state) {
#if DBG
Debug.Trace("Async", "Post");
Debug.Trace("AsyncStack", "Post from:\r\n" + GetDebugStackTrace());
#endif
CallCallback(callback, state);
}
#if DBG
[EnvironmentPermission(SecurityAction.Assert, Unrestricted=true)]
private void CreateCopyDumpStack() {
Debug.Trace("Async", "CreateCopy");
Debug.Trace("AsyncStack", "CreateCopy from:\r\n" + GetDebugStackTrace());
}
#endif
public override SynchronizationContext CreateCopy() {
#if DBG
CreateCopyDumpStack();
#endif
LegacyAspNetSynchronizationContext context = new LegacyAspNetSynchronizationContext(_application);
context._disabled = _disabled;
context._syncCaller = _syncCaller;
context.AllowAsyncDuringSyncStages = AllowAsyncDuringSyncStages;
return context;
}
public override void OperationStarted() {
if (_invalidOperationEncountered || (_disabled && _pendingCount == 0)) {
_invalidOperationEncountered = true;
throw new InvalidOperationException(SR.GetString(SR.Async_operation_disabled));
}
Interlocked.Increment(ref _pendingCount);
#if DBG
Debug.Trace("Async", "OperationStarted(count=" + _pendingCount + ")");
Debug.Trace("AsyncStack", "OperationStarted(count=" + _pendingCount + ") from:\r\n" + GetDebugStackTrace());
#endif
}
public override void OperationCompleted() {
if (_invalidOperationEncountered || (_disabled && _pendingCount == 0)) {
// throw from operation started could cause extra operation completed
return;
}
int pendingCount = Interlocked.Decrement(ref _pendingCount);
#if DBG
Debug.Trace("Async", "OperationCompleted(pendingCount=" + pendingCount + ")");
Debug.Trace("AsyncStack", "OperationCompleted(pendingCount=" + pendingCount + ") from:\r\n" + GetDebugStackTrace());
#endif
// notify (once) about the last completion to resume the async work
if (pendingCount == 0) {
WaitCallback callback = null;
lock (_lastCompletionCallbackLock) {
callback = _lastCompletionCallback;
_lastCompletionCallback = null;
}
if (callback != null) {
Debug.Trace("Async", "Queueing LastCompletionWorkItemCallback");
ThreadPool.QueueUserWorkItem(callback);
}
}
}
internal override bool Enabled {
get { return !_disabled; }
}
internal override void Enable() {
_disabled = false;
}
internal override void Disable() {
_disabled = true;
}
internal override void SetSyncCaller() {
_syncCaller = true;
}
internal override void ResetSyncCaller() {
_syncCaller = false;
}
internal override void AssociateWithCurrentThread() {
Monitor.Enter(_application);
}
internal override void DisassociateFromCurrentThread() {
Monitor.Exit(_application);
}
#if DBG
[EnvironmentPermission(SecurityAction.Assert, Unrestricted = true)]
private static string GetDebugStackTrace() {
return Environment.StackTrace;
}
#endif
}
}
|