Do not call thread attach/detach routines during process exit to avoid
potential deadlocks.

diff --git a/loader/module.c b/loader/module.c
index 2d6b946..c6659f7 100644
--- a/loader/module.c
+++ b/loader/module.c
@@ -28,6 +28,7 @@
 
 static WINE_MODREF *exe_modref;
 static int free_lib_count;   /* recursion depth of FreeLibrary calls */
+static int process_detaching;  /* set on process detach to avoid deadlocks with thread detach */
 
 /*************************************************************************
  *		MODULE32_LookupHMODULE
@@ -215,7 +216,7 @@
     WINE_MODREF *wm;
 
     RtlAcquirePebLock();
-
+    if (bForceDetach) process_detaching = 1;
     do
     {
         for ( wm = MODULE_modref_list; wm; wm = wm->next )
@@ -250,6 +251,10 @@
 {
     WINE_MODREF *wm;
 
+    /* don't do any attach calls if process is exiting */
+    if (process_detaching) return;
+    /* FIXME: there is still a race here */
+
     RtlAcquirePebLock();
 
     for ( wm = MODULE_modref_list; wm; wm = wm->next )
@@ -280,6 +285,10 @@
 {
     WINE_MODREF *wm;
 
+    /* don't do any detach calls if process is exiting */
+    if (process_detaching) return;
+    /* FIXME: there is still a race here */
+
     RtlAcquirePebLock();
 
     for ( wm = MODULE_modref_list; wm; wm = wm->next )