# [PATCH] Fix start_command closing cmd->out/in regardless of cmd->close_out/in

4 messages from 2007-11-18 to 2007-11-19. Participants: Ping Yin, Junio C Hamano, Johannes Sixt.
Thread: https://gitlist.dev/t/10920

## Ping Yin, 2007-11-18 17:36

Subject: [PATCH] Fix start_command closing cmd->out/in regardless of cmd->close_out/in
Message-ID: <1195407366-1610-1-git-send-email-pkufranky@gmail.com>
URL: https://gitlist.dev/e/1195407366-1610-1-git-send-email-pkufranky%40gmail.com

```
When 'FILE *fp' is assigned to child_process.out and then start_command or
run_command is run, the standard output of the child process is expected to
be outputed to fp. However, sometimes fp is not expected to be closed since
further IO may be still performmed on fp.
---
 run-command.c |    4 ----
 1 files changed, 0 insertions(+), 4 deletions(-)

diff --git a/run-command.c b/run-command.c
index 476d00c..4e5f58d 100644
--- a/run-command.c
+++ b/run-command.c
@@ -115,13 +115,9 @@ int start_command(struct child_process *cmd)
 
 	if (need_in)
 		close(fdin[0]);
-	else if (cmd->in)
-		close(cmd->in);
 
 	if (need_out)
 		close(fdout[1]);
-	else if (cmd->out > 1)
-		close(cmd->out);
 
 	if (need_err)
 		close(fderr[1]);
-- 
1.5.3.5.1876.g7ba19-dirty

```

## Junio C Hamano, 2007-11-18 18:52

Subject: Re: [PATCH] Fix start_command closing cmd->out/in regardless of cmd->close_out/in
Message-ID: <7vmytbv11m.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vmytbv11m.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <1195407366-1610-1-git-send-email-pkufranky@gmail.com>

```
Ping Yin <pkufranky@gmail.com> writes:

> When 'FILE *fp' is assigned to child_process.out and then start_command or
> run_command is run, the standard output of the child process is expected to
> be outputed to fp. However, sometimes fp is not expected to be closed since
> further IO may be still performmed on fp.

Could you clarify "However, sometimes" with a concrete codepath
that has this problem in the proposed commit log message, and
Sign-off your patch please?

```

## Johannes Sixt, 2007-11-19 07:39

Subject: Re: [PATCH] Fix start_command closing cmd->out/in regardless of cmd->close_out/in
Message-ID: <47413DB9.9030306@viscovery.net>
URL: https://gitlist.dev/e/47413DB9.9030306%40viscovery.net
In-Reply-To: <1195407366-1610-1-git-send-email-pkufranky@gmail.com>

```
Ping Yin schrieb:
> When 'FILE *fp' is assigned to child_process.out and then start_command or
> run_command is run, the standard output of the child process is expected to
> be outputed to fp. However, sometimes fp is not expected to be closed since
> further IO may be still performmed on fp.
> ---
>  run-command.c |    4 ----
>  1 files changed, 0 insertions(+), 4 deletions(-)
> 
> diff --git a/run-command.c b/run-command.c
> index 476d00c..4e5f58d 100644
> --- a/run-command.c
> +++ b/run-command.c
> @@ -115,13 +115,9 @@ int start_command(struct child_process *cmd)
>  
>  	if (need_in)
>  		close(fdin[0]);
> -	else if (cmd->in)
> -		close(cmd->in);
>  
>  	if (need_out)
>  		close(fdout[1]);
> -	else if (cmd->out > 1)
> -		close(cmd->out);
>  
>  	if (need_err)
>  		close(fderr[1]);

This is dangerous! You have to audit all current callers whether they close 
cmd->in or cmd->out (if they don't need the fd anymore). Otherwise you risk 
to keep a writable pipe end open and then the reader hangs, waiting for 
input that will never arrive.

-- Hannes

```

## Junio C Hamano, 2007-11-19 08:46

Subject: Re: [PATCH] Fix start_command closing cmd->out/in regardless of cmd->close_out/in
Message-ID: <7vfxz2oc6e.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vfxz2oc6e.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <47413DB9.9030306@viscovery.net>

```
Johannes Sixt <j.sixt@viscovery.net> writes:

> Ping Yin schrieb:
>> When 'FILE *fp' is assigned to child_process.out and then start_command or
>> run_command is run, the standard output of the child process is expected to
>> be outputed to fp. However, sometimes fp is not expected to be closed since
>> further IO may be still performmed on fp.
>> ---
>>  run-command.c |    4 ----
>>  1 files changed, 0 insertions(+), 4 deletions(-)
>>
>> diff --git a/run-command.c b/run-command.c
>> index 476d00c..4e5f58d 100644
>> --- a/run-command.c
>> +++ b/run-command.c
>> @@ -115,13 +115,9 @@ int start_command(struct child_process *cmd)
>>   	if (need_in)
>>  		close(fdin[0]);
>> -	else if (cmd->in)
>> -		close(cmd->in);
>>   	if (need_out)
>>  		close(fdout[1]);
>> -	else if (cmd->out > 1)
>> -		close(cmd->out);
>>   	if (need_err)
>>  		close(fderr[1]);
>
> This is dangerous! You have to audit all current callers whether they
> close cmd->in or cmd->out (if they don't need the fd
> anymore).

I am reasonably sure that they are already relying on these auto
closing of the file descriptors.

Funny that somebody falls into the trap the day after we
discussed it on another thread.

```
