From xemacs-m  Wed Jun  4 23:28:03 1997
Received: from crystal.WonderWorks.COM (crystal.WonderWorks.com [192.203.206.1])
	by xemacs.org (8.8.5/8.8.5) with ESMTP id XAA21173
	for <xemacs-beta@xemacs.org>; Wed, 4 Jun 1997 23:28:01 -0500 (CDT)
Received: by crystal.WonderWorks.COM 
	id QQcson06731; Thu, 5 Jun 1997 00:28:02 -0400 (EDT)
Date: Thu, 5 Jun 1997 00:28:02 -0400 (EDT)
Message-Id: <QQcson06731.199706050428@crystal.WonderWorks.COM>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
From: Kyle Jones <kyle_jones@wonderworks.com>
To: XEmacs Developers <xemacs-beta@xemacs.org>
Subject: [PATCH] 20.3-b3 for Re: gnuserv bug?
In-Reply-To: <kign2p5ravk.fsf@jagor.srce.hr>
References: <bcivi3toqk8.fsf@corp.Sun.COM>
	<kigsoyxric7.fsf@jagor.srce.hr>
	<kign2p5ravk.fsf@jagor.srce.hr>
X-Mailer: VM 6.33 under 20.3 XEmacs Lucid (beta3)
X-Face: /cA45WHG7jWq>(O3&Z57Y<"WsX5ddc,4c#w0F*zrV#=M
        0@~@,s;b,aMtR5Sqs"+nU.z^CSFQ9t`z2>W,S,]:[+2^
        Nbf6v4g>!&,7R4Ot4Wg{&tm=WX7P["9%a)_da48-^tGy
        ,qz]Z,Zz\{E.,]'EO+F)@$KtF&V

Hrvoje Niksic writes:
 > The problem is that when you try create a TTY device, a console
 > is also created.  However, when the creation of device fails
 > for some reason, the console is not deleted.
 > 
 > Now we are left with a device-less console -- and a problematic
 > one, at that.  Now every time we try to create a device at that
 > TTY, XEmacs cleverly tries to reuse the same console -- and
 > fails, for the same reason.
 > 
 > I believe the correct thing would be for `make-device' to
 > remember whether it created the console, and to make sure that
 > if there is an error, to remove the console.  The task of
 > coding this exceeds my knowledge of the XEmacs internals.

I'm surprised you haven't fiddled with record_unwind_protect by
now.  record_unwind_protect is used for this kind of cleanup work
all over the code.

Here's a patch.  Seems to work.

--- 1.1	1997/06/05 03:44:25
+++ src/device.c	1997/06/05 04:12:16
@@ -495,6 +495,14 @@
   return device;
 }
 
+static Lisp_Object
+delete_deviceless_console(Lisp_Object console)
+{
+  if (NILP (XCONSOLE (console)->device_list))
+    Fdelete_console(console, Qnil);
+  return Qnil;
+}
+
 DEFUN ("make-device", Fmake_device, 2, 3, 0, /*
 Create a new device of type TYPE, attached to connection CONNECTION.
 
@@ -521,6 +529,7 @@
   Lisp_Object console = Qnil;
   Lisp_Object name = Qnil;
   struct console_methods *conmeths;
+  int speccount = specpdl_depth();
 
   struct gcpro gcpro1, gcpro2, gcpro3;
 #ifdef HAVE_X_WINDOWS
@@ -563,6 +572,8 @@
     console = create_console (name, type, conconnect, props);
   }
 
+  record_unwind_protect(delete_deviceless_console, console);
+
   con = XCONSOLE (console);
   d = allocate_device (console);
   XSETDEVICE (device, d);
@@ -602,6 +613,7 @@
   setup_device_initial_specifier_tags (d);
 
   UNGCPRO;
+  unbind_to(speccount, Qnil);
   return device;
 }
 

