Index: glib/giowin32.c
===================================================================
RCS file: /cvs/gnome/glib/glib/giowin32.c,v
retrieving revision 1.60
diff -u -p -r1.60 giowin32.c
--- glib/giowin32.c	6 Dec 2004 23:43:43 -0000	1.60
+++ glib/giowin32.c	6 Jan 2005 23:29:52 -0000
@@ -340,7 +340,9 @@ create_thread (GIOWin32Channel     *chan
     g_warning (G_STRLOC ": Error closing thread handle: %s\n",
 	       g_win32_error_message (GetLastError ()));
 
+  UNLOCK (channel->mutex);
   WaitForSingleObject (channel->space_avail_event, INFINITE);
+  LOCK (channel->mutex);
 }
 
 static void
@@ -482,6 +484,9 @@ select_thread (void *parameter)
   int n;
   char buffer[8];
 
+  // Locking: hold mutex unless in select call
+  LOCK(channel->mutex);
+
   g_io_channel_ref ((GIOChannel *)channel);
 
   if (channel->debug)
@@ -496,14 +501,13 @@ select_thread (void *parameter)
 
   SetEvent (channel->space_avail_event);
   
-  while (channel->running)
+  while (channel->running && !channel->needs_close && channel->watches != NULL)
     {
       FD_ZERO (&read_fds);
       FD_ZERO (&write_fds);
       FD_ZERO (&except_fds);
       FD_SET (channel->reset_recv, &read_fds);
 
-      LOCK (channel->mutex);
       tmp = channel->watches;
       while (tmp)
 	{
@@ -518,7 +522,6 @@ select_thread (void *parameter)
 	  
 	  tmp = tmp->next;
 	}
-      UNLOCK (channel->mutex);
 
       if (channel->debug)
 	g_print ("select_thread %#x: calling select() for%s%s%s\n",
@@ -527,15 +530,11 @@ select_thread (void *parameter)
 		 (FD_ISSET (channel->fd, &write_fds) ? " OUT" : ""),
 		 (FD_ISSET (channel->fd, &except_fds) ? " ERR" : ""));
       
+      UNLOCK (channel->mutex);
       n = select (1, &read_fds, &write_fds, &except_fds, NULL);
-      
       LOCK (channel->mutex);
       if (channel->needs_close)
-	{
-	  UNLOCK (channel->mutex);
-	  break;
-	}
-      UNLOCK (channel->mutex);
+	break;
 
       if (n == SOCKET_ERROR)
 	{
@@ -545,60 +544,55 @@ select_thread (void *parameter)
 	  break;
 	}
 
-    if (FD_ISSET (channel->reset_recv, &read_fds))
-    {
-      if (channel->debug)
-        g_print ("select_thread %#x: re-looping\n",
-            channel->thread_id);
-      recv (channel->reset_recv,  (char *)&buffer, (int) sizeof (buffer), 0);
-      continue;
-    }
+      if (FD_ISSET (channel->reset_recv, &read_fds))
+	{
+	  if (channel->debug)
+	    g_print ("select_thread %#x: re-looping\n", channel->thread_id);
+	  recv (channel->reset_recv,  (char *)&buffer, (int) sizeof (buffer), 0);
+	  continue;
+	}
 
-    if (channel->debug)
-      g_print ("select_thread %#x: got%s%s%s\n",
-	       channel->thread_id,
-	       (FD_ISSET (channel->fd, &read_fds) ? " IN" : ""),
-	       (FD_ISSET (channel->fd, &write_fds) ? " OUT" : ""),
-	       (FD_ISSET (channel->fd, &except_fds) ? " ERR" : ""));
+      if (channel->debug)
+	g_print ("select_thread %#x: got%s%s%s\n",
+		 channel->thread_id,
+		 (FD_ISSET (channel->fd, &read_fds) ? " IN" : ""),
+		 (FD_ISSET (channel->fd, &write_fds) ? " OUT" : ""),
+		 (FD_ISSET (channel->fd, &except_fds) ? " ERR" : ""));
     
-    if (FD_ISSET (channel->fd, &read_fds))
-      channel->revents |= G_IO_IN;
-    if (FD_ISSET (channel->fd, &write_fds))
-      channel->revents |= G_IO_OUT;
-    if (FD_ISSET (channel->fd, &except_fds))
-      channel->revents |= G_IO_ERR;
-
-    if (channel->debug)
-      g_print ("select_thread %#x: resetting data_avail_noticed, setting data_avail\n",
-	       channel->thread_id);
-
-    LOCK (channel->mutex);
-    ResetEvent (channel->data_avail_noticed_event);
-    SetEvent (channel->data_avail_event);
-    if (channel->needs_close)
-      {
-	UNLOCK (channel->mutex);
+      if (FD_ISSET (channel->fd, &read_fds))
+	channel->revents |= G_IO_IN;
+      if (FD_ISSET (channel->fd, &write_fds))
+	channel->revents |= G_IO_OUT;
+      if (FD_ISSET (channel->fd, &except_fds))
+	channel->revents |= G_IO_ERR;
+  
+      if (channel->debug)
+	g_print ("select_thread %#x: resetting data_avail_noticed, setting data_avail\n",
+		 channel->thread_id);
+  
+      ResetEvent (channel->data_avail_noticed_event);
+      SetEvent (channel->data_avail_event);
+      if (channel->needs_close)
 	break;
-      }
-    UNLOCK (channel->mutex);
 
-    if (channel->debug)
-      g_print ("select_thread %#x: waiting for data_avail_noticed\n",
-        channel->thread_id);
-
-    WaitForSingleObject (channel->data_avail_noticed_event, INFINITE);
-    if (channel->debug)
-      g_print ("select_thread %#x: got data_avail_noticed\n",
-		 channel->thread_id);
+      if (channel->debug)
+	g_print ("select_thread %#x: waiting for data_avail_noticed\n",
+	  channel->thread_id);
+  
+      WaitForSingleObject (channel->data_avail_noticed_event, INFINITE);
+      if (channel->debug)
+	g_print ("select_thread %#x: got data_avail_noticed\n",
+		   channel->thread_id);
     }
 
-  LOCK (channel->mutex);
   channel->running = FALSE;
   if (channel->debug)
     g_print ("select_thread %#x: got error, setting data_avail\n",
 	     channel->thread_id);
   SetEvent (channel->data_avail_event);
+  channel->thread_id = 0;  
   g_io_channel_unref ((GIOChannel *)channel);
+
   UNLOCK (channel->mutex);
 
   /* No need to call _endthreadex(), the actual thread starter routine
@@ -731,8 +725,6 @@ g_io_win32_finalize (GSource *source)
   SetEvent (channel->data_avail_noticed_event);
   if (channel->type == G_IO_WIN32_SOCKET)
   {
-    /* Tell select_thread() to exit */
-    channel->needs_close = 1;
     /* Wake up select_thread() from its blocking select() */
     send (channel->reset_send, send_buffer, sizeof (send_buffer), 0);
   }
Index: tests/gio-test.c
===================================================================
RCS file: /cvs/gnome/glib/tests/gio-test.c,v
retrieving revision 1.11
diff -u -p -r1.11 gio-test.c
--- tests/gio-test.c	15 Oct 2002 22:16:57 -0000	1.11
+++ tests/gio-test.c	6 Jan 2005 23:29:53 -0000
@@ -212,6 +212,54 @@ recv_message (GIOChannel  *channel,
     }
   return retval;
 }
+void
+g_io_channel_win32_set_debug (GIOChannel *channel,
+			      gboolean    flag);
+
+
+static gboolean
+test_socket_watch(void)
+{
+  GIOChannel* channel;
+  GSource *src1 = NULL, *src2 = NULL;
+  int sock;
+  struct sockaddr_in addr = { AF_INET, 0, { htonl(INADDR_ANY)}, };
+  WSADATA wsaData;
+  int iResult;
+  int i;
+
+  iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
+  if ( iResult != NO_ERROR )
+    return FALSE;
+
+  sock = socket(AF_INET, SOCK_STREAM, 0);
+  if (sock == 0)
+    return FALSE;
+  if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) 
+    return FALSE;
+  
+  channel = g_io_channel_unix_new (sock);
+  g_io_channel_win32_set_debug (channel, TRUE);
+
+  for (i = 0; i < 300; i++) {
+    src1 = g_io_create_watch (channel, G_IO_IN);
+    g_source_unref(src1);
+    
+    if ( i % 2 == 0 ) {
+      if (src2 == NULL)
+        src2 = g_io_create_watch (channel, G_IO_IN);
+      else {
+	g_source_unref(src2);
+	src2 = NULL;
+      }
+    }
+  }
+  
+  g_io_channel_close (channel);
+  g_io_channel_unref (channel);
+
+  return TRUE;
+}  
 
 #ifdef G_OS_WIN32
 
@@ -285,6 +333,9 @@ main (int    argc,
       seqtab = g_malloc (nkiddies * 2 * sizeof (int));
 
 #ifdef G_OS_WIN32
+      if (!test_socket_watch())
+	return -1;
+	
       wcl.style = 0;
       wcl.lpfnWndProc = window_procedure;
       wcl.cbClsExtra = 0;
