diff --git a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs index 62cb2e18dde..e79afbd68bc 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs @@ -32,30 +32,23 @@ namespace System.IO return; } - // Open an inotify file descriptor. Ideally this would be a constrained execution region, but we don't have access to - // PrepareConstrainedRegions. We still use a finally block to house the code that opens the handle and stores it in - // hopes of making it as non-interruptible as possible. Ideally the SafeFileHandle would be allocated before the block, - // but SetHandle is protected and SafeFileHandle is sealed, so we're stuck doing the allocation here. - SafeFileHandle handle; - try { } finally + // Open an inotify file descriptor. + SafeFileHandle handle = Interop.Sys.INotifyInit(); + if (handle.IsInvalid) { - handle = Interop.Sys.INotifyInit(); - if (handle.IsInvalid) + Interop.ErrorInfo error = Interop.Sys.GetLastErrorInfo(); + switch (error.Error) { - Interop.ErrorInfo error = Interop.Sys.GetLastErrorInfo(); - switch (error.Error) - { - case Interop.Error.EMFILE: - string maxValue = ReadMaxUserLimit(MaxUserInstancesPath); - string message = !string.IsNullOrEmpty(maxValue) ? - SR.Format(SR.IOException_INotifyInstanceUserLimitExceeded_Value, maxValue) : - SR.IOException_INotifyInstanceUserLimitExceeded; - throw new IOException(message, error.RawErrno); - case Interop.Error.ENFILE: - throw new IOException(SR.IOException_INotifyInstanceSystemLimitExceeded, error.RawErrno); - default: - throw Interop.GetExceptionForIoErrno(error); - } + case Interop.Error.EMFILE: + string maxValue = ReadMaxUserLimit(MaxUserInstancesPath); + string message = !string.IsNullOrEmpty(maxValue) ? + SR.Format(SR.IOException_INotifyInstanceUserLimitExceeded_Value, maxValue) : + SR.IOException_INotifyInstanceUserLimitExceeded; + throw new IOException(message, error.RawErrno); + case Interop.Error.ENFILE: + throw new IOException(SR.IOException_INotifyInstanceSystemLimitExceeded, error.RawErrno); + default: + throw Interop.GetExceptionForIoErrno(error); } } diff --git a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.Windows.cs b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.Windows.cs index c4d227e6184..23d84a8fda2 100644 --- a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.Windows.cs +++ b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.Windows.cs @@ -226,23 +226,18 @@ namespace System.IO.Pipes { ExecuteHelper execHelper = (ExecuteHelper)helper; - RuntimeHelpers.PrepareConstrainedRegions(); - try { } - finally + if (Interop.Advapi32.ImpersonateNamedPipeClient(execHelper._handle)) { - if (Interop.Advapi32.ImpersonateNamedPipeClient(execHelper._handle)) - { - execHelper._mustRevert = true; - } - else - { - execHelper._impersonateErrorCode = Marshal.GetLastWin32Error(); - } - + execHelper._mustRevert = true; + } + else + { + execHelper._impersonateErrorCode = Marshal.GetLastWin32Error(); } if (execHelper._mustRevert) - { // impersonate passed so run user code + { + // impersonate passed so run user code execHelper._userCode(); } } diff --git a/src/libraries/System.Security.Cryptography.Cng/src/Microsoft/Win32/SafeHandles/NCryptSafeHandles.cs b/src/libraries/System.Security.Cryptography.Cng/src/Microsoft/Win32/SafeHandles/NCryptSafeHandles.cs index cae30b3c031..1e2adc19644 100644 --- a/src/libraries/System.Security.Cryptography.Cng/src/Microsoft/Win32/SafeHandles/NCryptSafeHandles.cs +++ b/src/libraries/System.Security.Cryptography.Cng/src/Microsoft/Win32/SafeHandles/NCryptSafeHandles.cs @@ -239,16 +239,9 @@ namespace Microsoft.Win32.SafeHandles bool addedRef = false; T duplicate = new T(); - // We need to do this operation in a CER, since we need to make sure that if the AddRef occurs - // that the duplicated handle will always point back to the Holder, otherwise the Holder will leak - // since it will never have its ref count reduced to zero. - try { } - finally - { - Holder.DangerousAddRef(ref addedRef); - duplicate.SetHandle(Holder.DangerousGetHandle()); - duplicate.Holder = Holder; // Transitions to OwnershipState.Duplicate - } + Holder.DangerousAddRef(ref addedRef); + duplicate.SetHandle(Holder.DangerousGetHandle()); + duplicate.Holder = Holder; // Transitions to OwnershipState.Duplicate return duplicate; } @@ -272,35 +265,28 @@ namespace Microsoft.Win32.SafeHandles T holder = new T(); T duplicate = new T(); - // We need to do this operation in a CER in order to ensure that everybody's state stays consistent - // with the current view of the world. If the state of the various handles gets out of sync, then - // we'll end up leaking since reference counts will not be set up properly. - try { } - finally + // Setup a holder safe handle to ref count the native handle + holder._ownershipState = OwnershipState.Holder; + holder.SetHandle(DangerousGetHandle()); + GC.SuppressFinalize(holder); + + + // Move the parent handle to the Holder + if (_parentHandle != null) { - // Setup a holder safe handle to ref count the native handle - holder._ownershipState = OwnershipState.Holder; - holder.SetHandle(DangerousGetHandle()); - GC.SuppressFinalize(holder); - - - // Move the parent handle to the Holder - if (_parentHandle != null) - { - holder._parentHandle = _parentHandle; - _parentHandle = null; - } - - // Transition into the duplicate state, referencing the holder. The initial reference count - // on the holder will refer to the original handle so there is no need to AddRef here. - Holder = holder; // Transitions to OwnershipState.Duplicate - - // The duplicate handle will also reference the holder - holder.DangerousAddRef(ref addRef); - duplicate.SetHandle(holder.DangerousGetHandle()); - duplicate.Holder = holder; // Transitions to OwnershipState.Duplicate + holder._parentHandle = _parentHandle; + _parentHandle = null; } + // Transition into the duplicate state, referencing the holder. The initial reference count + // on the holder will refer to the original handle so there is no need to AddRef here. + Holder = holder; // Transitions to OwnershipState.Duplicate + + // The duplicate handle will also reference the holder + holder.DangerousAddRef(ref addRef); + duplicate.SetHandle(holder.DangerousGetHandle()); + duplicate.Holder = holder; // Transitions to OwnershipState.Duplicate + return duplicate; }