mol*_*arm 5 .net c# impersonation winapi mutex
在测试应用程序时,我遇到了一个奇怪的行为.一些测试使用模拟以不同的用户身份运行代码,但它们总是挂起,永远不会完成.
经过一番调查后,问题被缩小到使用互斥量.最初,我们使用了基于MSDN文档的我们自己的模拟代码,但即使使用SimpleImpersonation库,问题仍然存在.以下是重现问题的最小示例:
using (Impersonation.LogonUser(DOMAIN, USER, PASSWORD, LogonType.Interactive))
{
Console.WriteLine("Impersonated");
bool mine;
using (new Mutex(true, "Mutex", out mine))
{
if (!mine)
throw new Exception("Couldn't get mutex");
Console.WriteLine("Got mutex");
}
}
Console.WriteLine("Finished");
Run Code Online (Sandbox Code Playgroud)
这永远不会完成,它与互斥锁创建一致.该文件指出,它要么抛出一个异常,或返回的东西,但没有提到封锁.
其他一些观察结果,可能与也可能没有关系:
可能底层系统资源正在发生,但我们无法弄明白.如何使这项工作?
更新:根据汉斯的评论,我尝试禁用Windows Defender,它没有帮助.这是挂起的地方的堆栈跟踪:
ntdll.dll!_NtWaitForSingleObject@12()
KernelBase.dll!_WaitForSingleObjectEx@12()
mscorlib.ni.dll!719c1867()
[Frames below may be incorrect and/or missing, native debugger attempting to walk managed call stack]
mscorlib.ni.dll!719c1852()
[Managed to Native Transition]
mscorlib.dll!System.Threading.Mutex.CreateMutexHandle(bool initiallyOwned, string name, Microsoft.Win32.Win32Native.SECURITY_ATTRIBUTES securityAttribute, out Microsoft.Win32.SafeHandles.SafeWaitHandle mutexHandle)
mscorlib.dll!System.Threading.Mutex.MutexTryCodeHelper.MutexTryCode(object userData)
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.Threading.Mutex.CreateMutexWithGuaranteedCleanup(bool initiallyOwned, string name, out bool createdNew, Microsoft.Win32.Win32Native.SECURITY_ATTRIBUTES secAttrs)
mscorlib.dll!System.Threading.Mutex.Mutex(bool initiallyOwned, string name, out bool createdNew, System.Security.AccessControl.MutexSecurity mutexSecurity)
mscorlib.dll!System.Threading.Mutex.Mutex(bool initiallyOwned, string name, out bool createdNew)
MutexImpersonationTest.exe!MutexImpersonationTest.Program.Main(string[] args) Line 16
Run Code Online (Sandbox Code Playgroud)
看起来获取Mutex的代码陷入无限循环,在我的测试中,它在调用中将100%的核心固定在一起new Mutex(...).
原因似乎是框架代码首先尝试调用Win32 CreateMutex,如果失败并出现"拒绝访问"错误,则尝试调用OpenMutex.如果OpenMutex调用失败并显示错误指示互斥锁不存在,则它会再次重复整个过程,因此如果互斥锁不存在则会陷入无限循环.
根据CreateMutex文档,这本质上是正确的方法,但似乎没有考虑到初始CreateMutex失败时拒绝访问的情况,这不是由于现有互斥锁的权限.
当我尝试它时似乎有效的一件事是将互斥锁名称加上"Global \"的前缀,希望这对你来说是一个合适的解决方法.
| 归档时间: |
|
| 查看次数: |
335 次 |
| 最近记录: |