From 5002c9504880309ffd273690a3e32fd544ea4950 Mon Sep 17 00:00:00 2001
From: Matt Johnston <matt@ucc.asn.au>
Date: Fri, 8 Jul 2005 15:04:06 +0000
Subject: [PATCH] * close a file descriptor after we've shutdown() both sides,
 to avoid a leak

--HG--
extra : convert_revision : 06fda32c634cf16a4c75797441ec2bd533f571c6
---
 common-channel.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/common-channel.c b/common-channel.c
index f03039a1..dfff1b4d 100644
--- a/common-channel.c
+++ b/common-channel.c
@@ -1019,6 +1019,7 @@ static void closechanfd(struct Channel *channel, int fd, int how) {
 
 	/* XXX server */
 	if (channel->type->sepfds) {
+		TRACE(("shutdown((%d), %d)", fd, how))
 		shutdown(fd, how);
 		if (how == 0) {
 			closeout = 1;
@@ -1043,4 +1044,11 @@ static void closechanfd(struct Channel *channel, int fd, int how) {
 	if (closein && (channel->extrabuf != NULL) && (fd == channel->errfd)) {
 		channel->errfd = FD_CLOSED;
 	}
+
+	/* if we called shutdown on it and all references are gone, then we 
+	 * need to close() it to stop it lingering */
+	if (channel->type->sepfds && channel->outfd == FD_CLOSED 
+		&& channel->infd == FD_CLOSED && channel->errfd == FD_CLOSED) {
+		close(fd);
+	}
 }
-- 
GitLab