To: vim_dev@googlegroups.com
Subject: Patch 7.3.060
Fcc: outbox
From: Bram Moolenaar <Bram@moolenaar.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
------------

Patch 7.3.060
Problem:    Netbeans: crash when socket is disconnected unexpectedly.
Solution:   Don't cleanup when a read fails, put a message in the queue and
	    disconnect later. (Xavier de Gaye)
Files:	    src/netbeans.c


*** ../vim-7.3.059/src/netbeans.c	2010-11-16 15:04:51.000000000 +0100
--- src/netbeans.c	2010-11-16 15:48:36.000000000 +0100
***************
*** 135,148 ****
  static int needupdate = 0;
  static int inAtomic = 0;
  
      static void
! netbeans_close(void)
  {
-     if (!NETBEANS_OPEN)
- 	return;
- 
-     netbeans_send_disconnect();
- 
  #ifdef FEAT_GUI_X11
      if (inputHandler != (XtInputId)NULL)
      {
--- 135,146 ----
  static int needupdate = 0;
  static int inAtomic = 0;
  
+ /*
+  * Close the socket and remove the input handlers.
+  */
      static void
! nb_close_socket(void)
  {
  #ifdef FEAT_GUI_X11
      if (inputHandler != (XtInputId)NULL)
      {
***************
*** 167,179 ****
  # endif
  #endif
  
  #ifdef FEAT_BEVAL
      bevalServers &= ~BEVAL_NETBEANS;
  #endif
  
-     sock_close(nbsock);
-     nbsock = -1;
- 
      needupdate = 0;
      inAtomic = 0;
      nb_free();
--- 165,191 ----
  # endif
  #endif
  
+     sock_close(nbsock);
+     nbsock = -1;
+ }
+ 
+ /*
+  * Close the connection and cleanup.
+  * May be called when nb_close_socket() was called earlier.
+  */
+     static void
+ netbeans_close(void)
+ {
+     if (NETBEANS_OPEN)
+     {
+ 	netbeans_send_disconnect();
+ 	nb_close_socket();
+     }
+ 
  #ifdef FEAT_BEVAL
      bevalServers &= ~BEVAL_NETBEANS;
  #endif
  
      needupdate = 0;
      inAtomic = 0;
      nb_free();
***************
*** 632,640 ****
      char_u	*p;
      queue_T	*node;
  
-     if (!NETBEANS_OPEN)
- 	return;
- 
      while (head.next != NULL && head.next != &head)
      {
  	node = head.next;
--- 644,649 ----
***************
*** 720,725 ****
--- 729,736 ----
  }
  #endif
  
+ #define DETACH_MSG "DETACH\n"
+ 
      void
  netbeans_read()
  {
***************
*** 780,801 ****
  	    break;	/* did read everything that's available */
      }
  
      if (readlen <= 0)
      {
! 	/* read error or didn't read anything */
! 	netbeans_close();
! 	nbdebug(("messageFromNetbeans: Error in read() from socket\n"));
  	if (len < 0)
  	{
  	    nbdebug(("read from Netbeans socket\n"));
  	    PERROR(_("read from Netbeans socket"));
  	}
- 	return; /* don't try to parse it */
      }
  
  #if defined(NB_HAS_GUI) && defined(FEAT_GUI_GTK)
      if (NB_HAS_GUI && gtk_main_level() > 0)
!         gtk_main_quit();
  #endif
  }
  
--- 791,822 ----
  	    break;	/* did read everything that's available */
      }
  
+     /* Reading a socket disconnection (readlen == 0), or a socket error. */
      if (readlen <= 0)
      {
! 	/* Queue a "DETACH" netbeans message in the command queue in order to
! 	 * terminate the netbeans session later. Do not end the session here
! 	 * directly as we may be running in the context of a call to
! 	 * netbeans_parse_messages():
! 	 *	netbeans_parse_messages
! 	 *	    -> autocmd triggered while processing the netbeans cmd
! 	 *		-> ui_breakcheck
! 	 *		    -> gui event loop or select loop
! 	 *			-> netbeans_read()
! 	 */
! 	save((char_u *)DETACH_MSG, strlen(DETACH_MSG));
! 	nb_close_socket();
! 
  	if (len < 0)
  	{
  	    nbdebug(("read from Netbeans socket\n"));
  	    PERROR(_("read from Netbeans socket"));
  	}
      }
  
  #if defined(NB_HAS_GUI) && defined(FEAT_GUI_GTK)
      if (NB_HAS_GUI && gtk_main_level() > 0)
! 	gtk_main_quit();
  #endif
  }
  
***************
*** 1164,1169 ****
--- 1185,1194 ----
  
      nbdebug(("REP %d: <none>\n", cmdno));
  
+     /* Avoid printing an annoying error message. */
+     if (!NETBEANS_OPEN)
+ 	return;
+ 
      sprintf(reply, "%d\n", cmdno);
      nb_send(reply, "nb_reply_nil");
  }
***************
*** 2753,2763 ****
  {
  #ifdef FEAT_GUI
  # if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)  \
!                 && !defined(FEAT_GUI_W32)
      if (gui.in_use)
      {
!         EMSG(_("E838: netbeans is not supported with this GUI"));
!         return;
      }
  # endif
  #endif
--- 2778,2788 ----
  {
  #ifdef FEAT_GUI
  # if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)  \
! 		&& !defined(FEAT_GUI_W32)
      if (gui.in_use)
      {
! 	EMSG(_("E838: netbeans is not supported with this GUI"));
! 	return;
      }
  # endif
  #endif
*** ../vim-7.3.059/src/version.c	2010-11-16 15:04:51.000000000 +0100
--- src/version.c	2010-11-16 15:22:39.000000000 +0100
***************
*** 716,717 ****
--- 716,719 ----
  {   /* Add new patch number below this line */
+ /**/
+     60,
  /**/

-- 
   Another bucket of what can only be described as human ordure hits ARTHUR.
ARTHUR: ... Right!  (to the KNIGHTS) That settles it!
                 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///
