NetServerEnum block when thread is terminated exte

2019-08-13 04:24发布

(Working in Win32 api , in C environment with VS2010)

I have a two thread app. The first thread forks the second and waits for a given interval - 'TIMEOUT', and then calls TerminateThread() on it. Meanwhile, second thread calls NetServerEnum().

It appears that when timeout is reached , whether NetServerEnum returned successfully or not, the first thread get deadlocked. I've already noticed that NetServerEnum creates worker threads of it's own.

I ultimately end up with one of those threads in deadlock, typically on ntdll.dll!RtlInitializeExceptionChain, unable to exit my process gracefully.

1条回答
手持菜刀,她持情操
2楼-- · 2019-08-13 04:51

As this to too long for a comment:

Verbatim from MSDN, allow me to use te answer form (emphasis by me):

TerminateThread is a dangerous function that should only be used in the most extreme cases. You should call TerminateThread only if you know exactly what the target thread is doing, and you control all of the code that the target thread could possibly be running at the time of the termination. For example, TerminateThread can result in the following problems:

  • If the target thread owns a critical section, the critical section will not be released.
  • If the target thread is allocating memory from the heap, the heap lock will not be released. *If the target thread is executing certain kernel32 calls when it is terminated, the kernel32 state for the thread's process could be inconsistent.
  • If the target thread is manipulating the global state of a shared DLL, the state of the DLL could be destroyed, affecting other users of the DLL.

From reading this it is easy to understanf why it is a bad idea to cancel (terminate) a thread stucking in a system call.


A possible alternative approach to the OP's design might be to spawn off a thread calling NetServerEnum() and simply let it run until the system call returned.

In the mean while the main thread could do other things like for example informing the user that scanning the net takes longer as expected.

查看更多
登录 后发表回答