Index: main/manager.c
===================================================================
--- main/manager.c	(revision 310992)
+++ main/manager.c	(revision 311141)
@@ -971,6 +971,7 @@
 	struct ast_tcptls_session_instance *tcptls_session;
 	FILE *f;
 	int fd;
+	int write_error:1;
 	struct manager_custom_hook *hook;
 	ast_mutex_t lock;
 };
@@ -1844,6 +1845,10 @@
  */
 static int send_string(struct mansession *s, char *string)
 {
+	int res;
+	FILE *f = s->f ? s->f : s->session->f;
+	int fd = s->f ? s->fd : s->session->fd;
+
 	/* It's a result from one of the hook's action invocation */
 	if (s->hook) {
 		/*
@@ -1852,11 +1857,13 @@
 		 */
 		s->hook->helper(EVENT_FLAG_HOOKRESPONSE, "HookResponse", string);
 		return 0;
-	} else if (s->f) {
-		return ast_careful_fwrite(s->f, s->fd, string, strlen(string), s->session->writetimeout);
-	} else {
-		return ast_careful_fwrite(s->session->f, s->session->fd, string, strlen(string), s->session->writetimeout);
 	}
+       
+	if ((res = ast_careful_fwrite(f, fd, string, strlen(string), s->session->writetimeout))) {
+		s->write_error = 1;
+	}
+
+	return res;
 }
 
 /*!
@@ -4673,7 +4680,7 @@
 	ao2_unlock(session);
 	astman_append(&s, "Asterisk Call Manager/%s\r\n", AMI_VERSION);	/* welcome prompt */
 	for (;;) {
-		if ((res = do_message(&s)) < 0) {
+		if ((res = do_message(&s)) < 0 || s.write_error) {
 			break;
 		}
 	}
