Index: generic/tkCmds.c
==================================================================
--- generic/tkCmds.c
+++ generic/tkCmds.c
@@ -99,10 +99,11 @@
 	"-displayof", "-nice", NULL
     };
     enum options { TK_BELL_DISPLAYOF, TK_BELL_NICE };
     Tk_Window tkwin = clientData;
     int i, index, nice = 0;
+    Tk_ErrorHandler handler;
 
     if (objc > 4) {
     wrongArgs:
 	Tcl_WrongNumArgs(interp, 1, objv, "?-displayof window? ?-nice?");
 	return TCL_ERROR;
@@ -126,15 +127,17 @@
 	case TK_BELL_NICE:
 	    nice = 1;
 	    break;
 	}
     }
+    handler = Tk_CreateErrorHandler(Tk_Display(tkwin), -1, -1, -1, NULL, NULL);
     XBell(Tk_Display(tkwin), 0);
     if (!nice) {
 	XForceScreenSaver(Tk_Display(tkwin), ScreenSaverReset);
     }
     XFlush(Tk_Display(tkwin));
+    Tk_DeleteErrorHandler(handler);
     return TCL_OK;
 }
 
 /*
  *----------------------------------------------------------------------

Index: unix/tkUnixSend.c
==================================================================
--- unix/tkUnixSend.c
+++ unix/tkUnixSend.c
@@ -259,14 +259,17 @@
     NameRegistry *regPtr;
     int result, actualFormat;
     unsigned long bytesAfter;
     Atom actualType;
     char **propertyPtr;
+    Tk_ErrorHandler handler;
 
     if (dispPtr->commTkwin == NULL) {
 	SendInit(interp, dispPtr);
     }
+
+    handler = Tk_CreateErrorHandler(dispPtr->display, -1, -1, -1, NULL, NULL);
 
     regPtr = ckalloc(sizeof(NameRegistry));
     regPtr->dispPtr = dispPtr;
     regPtr->locked = 0;
     regPtr->modified = 0;
@@ -304,11 +307,14 @@
 	    regPtr->property = NULL;
 	}
 	XDeleteProperty(dispPtr->display,
 		RootWindow(dispPtr->display, 0),
 		dispPtr->registryProperty);
+	XSync(dispPtr->display, False);
     }
+
+    Tk_DeleteErrorHandler(handler);
 
     /*
      * Xlib placed an extra null byte after the end of the property, just to
      * make sure that it is always NULL-terminated. Be sure to include this
      * byte in our count if it's needed to ensure null termination (note: as
@@ -512,10 +518,15 @@
 static void
 RegClose(
     NameRegistry *regPtr)	/* Pointer to a registry opened with a
 				 * previous call to RegOpen. */
 {
+    Tk_ErrorHandler handler;
+
+    handler = Tk_CreateErrorHandler(regPtr->dispPtr->display, -1, -1, -1,
+		    NULL, NULL);
+
     if (regPtr->modified) {
 	if (!regPtr->locked && !localData.sendDebug) {
 	    Tcl_Panic("The name registry was modified without being locked!");
 	}
 	XChangeProperty(regPtr->dispPtr->display,
@@ -537,10 +548,12 @@
      * ungrab command is still sitting in our output buffer, we could
      * deadlock.
      */
 
     XFlush(regPtr->dispPtr->display);
+
+    Tk_DeleteErrorHandler(handler);
 
     if (regPtr->property != NULL) {
 	if (regPtr->allocedByX) {
 	    XFree(regPtr->property);
 	} else {
@@ -1093,10 +1106,35 @@
     Tcl_DStringAppend(&request, Tcl_GetString(objv[firstArg]), -1);
     for (i = firstArg+1; i < objc; i++) {
 	Tcl_DStringAppend(&request, " ", 1);
 	Tcl_DStringAppend(&request, Tcl_GetString(objv[i]), -1);
     }
+
+    if (!async) {
+	/*
+	 * Register the fact that we're waiting for a command to complete
+	 * (this is needed by SendEventProc and by AppendErrorProc to pass
+	 * back the command's results). Set up a timeout handler so that
+	 * we can check during long sends to make sure that the destination
+	 * application is still alive.
+	 *
+	 * We prepare the pending struct here in order to catch potential
+	 * early X errors from AppendPropCarefully() due to XSync().
+	 */
+
+	pending.serial = localData.sendSerial;
+	pending.dispPtr = dispPtr;
+	pending.target = destName;
+	pending.commWindow = commWindow;
+	pending.interp = interp;
+	pending.result = NULL;
+	pending.errorInfo = NULL;
+	pending.errorCode = NULL;
+	pending.gotResponse = 0;
+	pending.nextPtr = tsdPtr->pendingCommands;
+	tsdPtr->pendingCommands = &pending;
+    }
     (void) AppendPropCarefully(dispPtr->display, commWindow,
 	    dispPtr->commProperty, Tcl_DStringValue(&request),
 	    Tcl_DStringLength(&request) + 1, (async ? NULL : &pending));
     Tcl_DStringFree(&request);
     if (async) {
@@ -1106,30 +1144,10 @@
 	 */
 
 	return TCL_OK;
     }
 
-    /*
-     * Register the fact that we're waiting for a command to complete (this is
-     * needed by SendEventProc and by AppendErrorProc to pass back the
-     * command's results). Set up a timeout handler so that we can check
-     * during long sends to make sure that the destination application is
-     * still alive.
-     */
-
-    pending.serial = localData.sendSerial;
-    pending.dispPtr = dispPtr;
-    pending.target = destName;
-    pending.commWindow = commWindow;
-    pending.interp = interp;
-    pending.result = NULL;
-    pending.errorInfo = NULL;
-    pending.errorCode = NULL;
-    pending.gotResponse = 0;
-    pending.nextPtr = tsdPtr->pendingCommands;
-    tsdPtr->pendingCommands = &pending;
-
     /*
      * Enter a loop processing X events until the result comes in or the
      * target is declared to be dead. While waiting for a result, look only at
      * send-related events so that the send is synchronous with respect to
      * other events in the application.
@@ -1949,28 +1967,32 @@
     };
     static const char *const testsendOptions[] = {
 	"bogus",   "prop",   "serial",  NULL
     };
     TkWindow *winPtr = clientData;
+    Tk_ErrorHandler handler;
     int index;
 
     if (objc < 2) {
 	Tcl_WrongNumArgs(interp, 1, objv,
 		"option ?arg ...?");
 	return TCL_ERROR;
     }
 
-	if (Tcl_GetIndexFromObjStruct(interp, objv[1], testsendOptions,
+    if (Tcl_GetIndexFromObjStruct(interp, objv[1], testsendOptions,
 		sizeof(char *), "option", 0, &index) != TCL_OK) {
-	    return TCL_ERROR;
-	}
-	if (index == TESTSEND_BOGUS) {
+	return TCL_ERROR;
+    }
+    if (index == TESTSEND_BOGUS) {
+	handler = Tk_CreateErrorHandler(winPtr->dispPtr->display, -1, -1, -1,
+			NULL, NULL);
 	XChangeProperty(winPtr->dispPtr->display,
 		RootWindow(winPtr->dispPtr->display, 0),
 		winPtr->dispPtr->registryProperty, XA_INTEGER, 32,
 		PropModeReplace,
 		(unsigned char *) "This is bogus information", 6);
+	Tk_DeleteErrorHandler(handler);
     } else if (index == TESTSEND_PROP) {
 	int result, actualFormat;
 	unsigned long length, bytesAfter;
 	Atom actualType, propName;
 	char *property, **propertyPtr = &property, *p, *end;
@@ -2005,11 +2027,14 @@
 	    }
 	    if (property != NULL) {
 		XFree(property);
 	    }
 	} else if (Tcl_GetString(objv[4])[0] == 0) {
+	    handler = Tk_CreateErrorHandler(winPtr->dispPtr->display,
+			    -1, -1, -1, NULL, NULL);
 	    XDeleteProperty(winPtr->dispPtr->display, w, propName);
+	    Tk_DeleteErrorHandler(handler);
 	} else {
 	    Tcl_DString tmp;
 
 	    Tcl_DStringInit(&tmp);
 	    for (p = Tcl_DStringAppend(&tmp, Tcl_GetString(objv[4]),
@@ -2016,14 +2041,16 @@
 		    (int) strlen(Tcl_GetString(objv[4]))); *p != 0; p++) {
 		if (*p == '\n') {
 		    *p = 0;
 		}
 	    }
-
+	    handler = Tk_CreateErrorHandler(winPtr->dispPtr->display,
+			    -1, -1, -1, NULL, NULL);
 	    XChangeProperty(winPtr->dispPtr->display, w, propName, XA_STRING,
 		    8, PropModeReplace, (unsigned char*)Tcl_DStringValue(&tmp),
 		    p-Tcl_DStringValue(&tmp));
+	    Tk_DeleteErrorHandler(handler);
 	    Tcl_DStringFree(&tmp);
 	}
     } else if (index == TESTSEND_SERIAL) {
 	Tcl_SetObjResult(interp, Tcl_NewIntObj(localData.sendSerial+1));
     }

