# Re: git svn import failure : write .git/Git_svn_hash_BmjclS: Bad file descriptor

6 messages from 2015-02-26 to 2015-03-23. Participants: Nico Schlömer, Kyle J. McKay, Eric Wong, Junio C Hamano.
Thread: https://gitlist.dev/t/38655

## Nico Schlömer, 2015-02-26 09:09

Subject: Re: git svn import failure : write .git/Git_svn_hash_BmjclS: Bad file descriptor
Message-ID: <CAK6Z60fhyY3wPG6hx7YJ4y7wZsWb=33JhqejmpVBgZLGdB1XMA@mail.gmail.com>
URL: https://gitlist.dev/e/CAK6Z60fhyY3wPG6hx7YJ4y7wZsWb%3D33JhqejmpVBgZLGdB1XMA%40mail.gmail.com
In-Reply-To: <CAK6Z60ciheWOUGOv1sYcA==B2WR1Rs_eMU+9a=R3FBwc_37CyQ@mail.gmail.com>

```
All,

I applied Kyle's latest patch

> diff --git a/perl/Git/SVN/Ra.pm b/perl/Git/SVN/Ra.pm
> index 622535e2..96888228 100644
> --- a/perl/Git/SVN/Ra.pm
> +++ b/perl/Git/SVN/Ra.pm
> @@ -391,6 +391,7 @@ sub longest_common_path {
> sub gs_fetch_loop_common {
>         my ($self, $base, $head, $gsv, $globs) = @_;
>         return if ($base > $head);
> +       $::_repository->_open_cat_blob_if_needed;
>         my $gpool = SVN::Pool->new_default;
>         my $ra_url = $self->url;
>         my $reload_ra = sub {

alone and this fixes the bug for me. Thanks a lot, Kyle!

Cheers,
Nico

On Thu, Feb 26, 2015 at 12:37 AM, Nico Schlömer
<nico.schloemer@gmail.com> wrote:
> Kyle,
>
> you are absolutely correct, I used the wrong Git installation in my last
> email. With both your and Eric's patches applied, I'm getting
> ˋˋˋ
> [...]
> r100 = e2a9b5baa2cebb18591ecb04ff350410d52f36de (refs/remotes/git-svn)
> Unexpected result returned from git cat-file at
> /home/nschloe/share/perl/5.18.2/Git/SVN/Fetcher.pm line 344.
> Failed to read object 619f9d1d857fb287d06a70c9dac6b8b534d0de6a at
> /home/nschloe/share/perl/5.18.2/Git/SVN/Fetcher.pm line 345, <GEN16> line
> 757.
>
> error closing pipe: Bad file descriptor at
> /home/nschloe/libexec/git-core/git-svn line 0.
> error closing pipe: Bad file descriptor at
> /home/nschloe/libexec/git-core/git-svn line 0.
> ˋˋˋ
> where line 344 is
> ˋˋˋ
> $size = $::_repository->cat_blob($fb->{blob}, $base);
> ˋˋˋ
> Adding the line in `perl/Git/SVN/Ra.pm` as per your suggestion does not
> change this.
>
> Cheers,
> Nico
>
>
> On Wed, Feb 25, 2015 at 9:34 PM Kyle J. McKay <mackyle@gmail.com> wrote:
>>
>> On Feb 25, 2015, at 07:07, Nico Schlömer wrote:
>>
>> > Thanks Kyle for the patch! I applied it and ran
>> > ˋˋˋ
>> > git svn clone https://geuz.org/svn/gmsh/trunk
>> > ˋˋˋ
>> > Unfortunately, I'm still getting
>> > ˋˋˋ
>> > [...]
>> > r100 = e2a9b5baa2cebb18591ecb04ff350410d52f36de (refs/remotes/git-svn)
>> > error closing pipe: Bad file descriptor at /usr/share/perl5/Git/SVN/
>> > Fetcher.pm line 335.
>> > error closing pipe: Bad file descriptor at /usr/share/perl5/Git/SVN/
>> > Fetcher.pm line 335.
>> > out pipe went bad at /usr/share/perl5/Git.pm line 955.
>> > ˋˋˋ
>>
>> Are you certain you're running with the patch?  I ask because earlier
>> you sent this:
>>
>> On Jan 31, 2015, at 04:51, Nico Schlömer wrote:
>>
>> > I tried the patch and I still get
>> > ˋˋˋ
>> > [...]
>> > r100 = e2a9b5baa2cebb18591ecb04ff350410d52f36de (refs/remotes/git-svn)
>> > Unexpected result returned from git cat-file at
>> > /home/nschloe/share/perl/5.18.2/Git/SVN/Fetcher.pm line 335.
>> > Failed to read object 619f9d1d857fb287d06a70c9dac6b8b534d0de6a at
>> > /home/nschloe/share/perl/5.18.2/Git/SVN/Fetcher.pm line 336, <GEN16>
>> > line 757.
>> >
>> > error closing pipe: Bad file descriptor at
>> > /home/nschloe/libexec/git-core/git-svn line 0.
>> > error closing pipe: Bad file descriptor at
>> > /home/nschloe/libexec/git-core/git-svn line 0.
>> > ˋˋˋ
>> > when
>> > ˋˋˋ
>> > git svn clone https://geuz.org/svn/gmsh/trunk
>> > ˋˋˋ
>>
>> And as the patch below applies to Fetcher.pm at line 322 and inserts 8
>> lines, I do not see how you could be getting the same error message at
>> line 335 if the patch was present.  Even if you manually applied it by
>> only inserting the two lines of code, the line numbers would be at
>> least 337, not 335 as reported above.  I also notice the path to
>> Fetcher.pm is different from your earlier report.
>>
>> That said, the fact that it happens right after r100 (which is when
>> SVN::Pool::clear is getting called) suggests there's another file
>> handle getting caught up in the SVN::Pool somehow.  (When I try to
>> clone gmsh, I got to r4885 before a server disconnection.)
>>
>> It could be as simple as the open2 call FileHandle result in later
>> perl versions ends up in the SVN::Pool whereas in earlier Perl and/or
>> svn versions it does not.
>>
>> What happens if you add this change on top of the Fetcher.pm change:
>>
>> diff --git a/perl/Git/SVN/Ra.pm b/perl/Git/SVN/Ra.pm
>> index 622535e2..96888228 100644
>> --- a/perl/Git/SVN/Ra.pm
>> +++ b/perl/Git/SVN/Ra.pm
>> @@ -391,6 +391,7 @@ sub longest_common_path {
>>   sub gs_fetch_loop_common {
>>         my ($self, $base, $head, $gsv, $globs) = @_;
>>         return if ($base > $head);
>> +       $::_repository->_open_cat_blob_if_needed;
>>         my $gpool = SVN::Pool->new_default;
>>         my $ra_url = $self->url;
>>         my $reload_ra = sub {
>>
>> -Kyle
>>
>> > Cheers,
>> > Nico
>> >
>> > On Wed, Feb 25, 2015 at 11:20 AM Kyle J. McKay <mackyle@gmail.com>
>> > wrote:
>> > I believe I have been able to track down this problem and implement a
>> > fix.  Please report back if this patch fixes the problem for you.
>> >
>> > -Kyle
>> >
>> > -- 8< --
>> > Subject: [PATCH] Git::SVN::Fetcher: avoid premature 'svn_hash' temp
>> > file closure
>> >
>> > Since b19138b (git-svn: Make it incrementally faster by minimizing
>> > temp
>> > files, v1.6.0), git-svn has been using the Git.pm temp_acquire and
>> > temp_release mechanism to avoid unnecessary temp file churn and
>> > provide
>> > a speed boost.
>> >
>> > However, that change introduced a call to temp_acquire inside the
>> > Git::SVN::Fetcher::close_file function for an 'svn_hash' temp file.
>> > Because an SVN::Pool is active at the time this function is called, if
>> > the Git::temp_acquire function ends up actually creating a new
>> > FileHandle for the temp file (which it will the first time it's called
>> > with the name 'svn_hash') that FileHandle will end up in the SVN::Pool
>> > and should that pool have SVN::Pool::clear called on it that
>> > FileHandle
>> > will be closed out from under Git::temp_acquire.
>> >
>> > Since the only call site to Git::temp_acquire with the name 'svn_hash'
>> > is inside the close_file function, if an 'svn_hash' temp file is ever
>> > created its FileHandle is guaranteed to be created in the active
>> > SVN::Pool.
>> >
>> > This has not been a problem in the past because the SVN::Pool was not
>> > being cleared.  However, since dfa72fdb (git-svn: reload RA every
>> > log-window-size, v2.2.0) the pool has been getting cleared
>> > periodically
>> > at which point the FileHandle for the 'svn_hash' temp file gets
>> > closed.
>> > Any subsequent calls to Git::temp_acquire for 'svn_hash', however,
>> > succeed without creating/opening a new temporary file since it still
>> > has
>> > the now invalid FileHandle in its cache.  Callers that then attempt to
>> > use that FileHandle fail with an error.
>> >
>> > We avoid this problem by making sure the 'svn_hash' temp file is
>> > created
>> > in the same place the 'svn_delta_...' and 'git_blob_...' temp files
>> > are
>> > (and then temp_release'd) so that it can be safely used inside the
>> > close_file function without having its FileHandle end up in an
>> > SVN::Pool
>> > that gets cleared.
>> >
>> > Signed-off-by: Kyle J. McKay <mackyle@gmail.com>
>> > ---
>> >  perl/Git/SVN/Fetcher.pm | 8 ++++++++
>> >  1 file changed, 8 insertions(+)
>> >
>> > diff --git a/perl/Git/SVN/Fetcher.pm b/perl/Git/SVN/Fetcher.pm
>> > index 10edb277..613055a3 100644
>> > --- a/perl/Git/SVN/Fetcher.pm
>> > +++ b/perl/Git/SVN/Fetcher.pm
>> > @@ -322,6 +322,14 @@ sub apply_textdelta {
>> >         # (but $base does not,) so dup() it for reading in close_file
>> >         open my $dup, '<&', $fh or croak $!;
>> >         my $base = $::_repository->temp_acquire("git_blob_${$}_
>> > $suffix");
>> > +       # close_file may call temp_acquire on 'svn_hash', but
>> > because of the
>> > +       # call chain, if the temp_acquire call from close_file ends
>> > up being the
>> > +       # call that first creates the 'svn_hash' temp file, then the
>> > FileHandle
>> > +       # that's created as a result will end up in an SVN::Pool
>> > that we clear
>> > +       # in SVN::Ra::gs_fetch_loop_common.  Avoid that by making
>> > sure the
>> > +       # 'svn_hash' FileHandle is already created before close_file
>> > is called.
>> > +       my $tmp_fh = $::_repository->temp_acquire('svn_hash');
>> > +       $::_repository->temp_release($tmp_fh, 1);
>> >
>> >         if ($fb->{blob}) {
>> >                 my ($base_is_link, $size);
>> > --
>>
>

```

## Kyle J. McKay, 2015-02-26 13:49

Subject: Re: git svn import failure : write .git/Git_svn_hash_BmjclS: Bad file descriptor
Message-ID: <cbcb926c1b7edd31548a15ea4966a35@74d39fa044aa309eaea14b9f57fe79c>
URL: https://gitlist.dev/e/cbcb926c1b7edd31548a15ea4966a35%4074d39fa044aa309eaea14b9f57fe79c
In-Reply-To: <CAK6Z60fhyY3wPG6hx7YJ4y7wZsWb=33JhqejmpVBgZLGdB1XMA@mail.gmail.com>

```
On Feb 26, 2015, at 01:09, Nico Schl=F6mer wrote:
> All,
>
> I applied Kyle's latest patch
>
>> diff --git a/perl/Git/SVN/Ra.pm b/perl/Git/SVN/Ra.pm
>> index 622535e2..96888228 100644
>> --- a/perl/Git/SVN/Ra.pm
>> +++ b/perl/Git/SVN/Ra.pm
>> @@ -391,6 +391,7 @@ sub longest_common_path {
>> sub gs_fetch_loop_common {
>>        my ($self, $base, $head, $gsv, $globs) =3D @_;
>>        return if ($base > $head);
>> +       $::_repository->_open_cat_blob_if_needed;
>>        my $gpool =3D SVN::Pool->new_default;
>>        my $ra_url =3D $self->url;
>>        my $reload_ra =3D sub {
>
> alone and this fixes the bug for me. Thanks a lot, Kyle!

The updated patch with the additional fix is below.

There are two symptoms it addresses:

1) the 'Git_svn_hash_... bad file descriptor' failure

2) the 'out pipe went bad' failure

While (1) could probably also be addressed by Eric's alternate
"destroy all tempfiles when reloading RA" patch, that would require re-
opening all the temp files every 100 revisions (or whatever the log
window size is changed to).

I noticed that with the default log window size of 100 revisions, each
time the connection was reset at the 100-revision boundary (to reduce
the overall memory usage) it seemed to take approx. 3 seconds to start
up again processing revisions on that gmsh repository.  If you're
cloning 30000 revisions, that adds 15 minutes to the total clone time
already.  Admittedly opening new temp files will be a lot faster and
hardly noticeable, but doing that 300 times over the course of 30000
revisions will probably add at least a little extra delay and since
blowing away all the temp files does not seem to be necessary, why
incur the extra delay?

Also the "destroy all tempfiles when reloading RA" patch isn't going
to fix the cat_blob 'out pipe went bad' problem since that has nothing
to do with the temp files so another change will still be required for
that.

-Kyle

-- 8< --
Subject: [PATCH v2] Git::SVN::*: avoid premature FileHandle closure

Since b19138b (git-svn: Make it incrementally faster by minimizing temp
files, v1.6.0), git-svn has been using the Git.pm temp_acquire and
temp_release mechanism to avoid unnecessary temp file churn and provide
a speed boost.

However, that change introduced a call to temp_acquire inside the
Git::SVN::Fetcher::close_file function for an 'svn_hash' temp file.
Because an SVN::Pool is active at the time this function is called, if
the Git::temp_acquire function ends up actually creating a new
FileHandle for the temp file (which it will the first time it's called
with the name 'svn_hash') that FileHandle will end up in the SVN::Pool
and should that pool have SVN::Pool::clear called on it that FileHandle
will be closed out from under Git::temp_acquire.

Since the only call site to Git::temp_acquire with the name 'svn_hash'
is inside the close_file function, if an 'svn_hash' temp file is ever
created its FileHandle is guaranteed to be created in the active
SVN::Pool.

This has not been a problem in the past because the SVN::Pool was not
being cleared.  However, since dfa72fdb (git-svn: reload RA every
log-window-size, v2.2.0) the pool has been getting cleared periodically
at which point the FileHandle for the 'svn_hash' temp file gets closed.
Any subsequent calls to Git::temp_acquire for 'svn_hash', however,
succeed without creating/opening a new temporary file since it still has
the now invalid FileHandle in its cache.  Callers that then attempt to
use that FileHandle fail with an error.

We avoid this problem by making sure the 'svn_hash' temp file is created
in the same place the 'svn_delta_...' and 'git_blob_...' temp files are
(and then temp_release'd) so that it can be safely used inside the
close_file function without having its FileHandle end up in an SVN::Pool
that gets cleared.

Additionally the Git.pm cat_blob function creates a bidirectional pipe
FileHandle using the IPC::Open2::open2 function.  If that handle is
created too late, it also gets caught up in the SVN::Pool and incorrectly
closed by the SVN::Pool::clear call.  But this only seems to happen with
more recent versions of Perl and svn.

To avoid this problem we add an explicit call to _open_cat_blob_if_needed
before the first call to SVN::Pool->new_default to make sure the open2
handle does not end up in the SVN::Pool.

Signed-off-by: Kyle J. McKay <mackyle@gmail.com>
---
 perl/Git/SVN/Fetcher.pm | 8 ++++++++
 perl/Git/SVN/Ra.pm      | 3 +++
 2 files changed, 11 insertions(+)

diff --git a/perl/Git/SVN/Fetcher.pm b/perl/Git/SVN/Fetcher.pm
index 10edb277..613055a3 100644
--- a/perl/Git/SVN/Fetcher.pm
+++ b/perl/Git/SVN/Fetcher.pm
@@ -322,6 +322,14 @@ sub apply_textdelta {
 	# (but $base does not,) so dup() it for reading in close_file
 	open my $dup, '<&', $fh or croak $!;
 	my $base = $::_repository->temp_acquire("git_blob_${$}_$suffix");
+	# close_file may call temp_acquire on 'svn_hash', but because of the
+	# call chain, if the temp_acquire call from close_file ends up being the
+	# call that first creates the 'svn_hash' temp file, then the FileHandle
+	# that's created as a result will end up in an SVN::Pool that we clear
+	# in SVN::Ra::gs_fetch_loop_common.  Avoid that by making sure the
+	# 'svn_hash' FileHandle is already created before close_file is called.
+	my $tmp_fh = $::_repository->temp_acquire('svn_hash');
+	$::_repository->temp_release($tmp_fh, 1);
 
 	if ($fb->{blob}) {
 		my ($base_is_link, $size);
diff --git a/perl/Git/SVN/Ra.pm b/perl/Git/SVN/Ra.pm
index 622535e2..32e2f19e 100644
--- a/perl/Git/SVN/Ra.pm
+++ b/perl/Git/SVN/Ra.pm
@@ -391,6 +391,9 @@ sub longest_common_path {
 sub gs_fetch_loop_common {
 	my ($self, $base, $head, $gsv, $globs) = @_;
 	return if ($base > $head);
+	# Make sure the cat_blob open2 FileHandle is created before calling
+	# SVN::Pool::new_default so that it does not incorrectly end up in the pool.
+	$::_repository->_open_cat_blob_if_needed;
 	my $gpool = SVN::Pool->new_default;
 	my $ra_url = $self->url;
 	my $reload_ra = sub {
--

```

## Eric Wong, 2015-02-26 21:27

Subject: Re: git svn import failure : write .git/Git_svn_hash_BmjclS: Bad file descriptor
Message-ID: <20150226212721.GA2930@dcvr.yhbt.net>
URL: https://gitlist.dev/e/20150226212721.GA2930%40dcvr.yhbt.net
In-Reply-To: <cbcb926c1b7edd31548a15ea4966a35@74d39fa044aa309eaea14b9f57fe79c>

```
"Kyle J. McKay" <mackyle@gmail.com> wrote:
> The updated patch with the additional fix is below.

Thanks, signed-off and pushed to master on git://bogomips.org/git-svn
I've dropped my "destroy all tempfiles..." patch.

```

## Junio C Hamano, 2015-03-23 19:11

Subject: Re: git svn import failure : write .git/Git_svn_hash_BmjclS: Bad file descriptor
Message-ID: <xmqqa8z3a5rm.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqa8z3a5rm.fsf%40gitster.dls.corp.google.com
In-Reply-To: <20150226212721.GA2930@dcvr.yhbt.net>

```
Eric Wong <normalperson@yhbt.net> writes:

> "Kyle J. McKay" <mackyle@gmail.com> wrote:
>> The updated patch with the additional fix is below.
>
> Thanks, signed-off and pushed to master on git://bogomips.org/git-svn
> I've dropped my "destroy all tempfiles..." patch.

I think I missed this exchange.  I see these two patches:

    e0b4cad Git::SVN::*: avoid premature FileHandle closure
    ce1b57b git-svn: fix localtime=true on non-glibc environments

on the 'master' branch of your git://git.bogomips.org/git-svn.git/
repository.  Is this a good time to pull from you, and should I
expect more during this cycle (I am guessing that the answers would
be yes and no, but just to make sure...)?

Thanks.

```

## Eric Wong, 2015-03-23 19:36

Subject: Re: git svn import failure : write .git/Git_svn_hash_BmjclS: Bad file descriptor
Message-ID: <20150323193634.GA3454@dcvr.yhbt.net>
URL: https://gitlist.dev/e/20150323193634.GA3454%40dcvr.yhbt.net
In-Reply-To: <xmqqa8z3a5rm.fsf@gitster.dls.corp.google.com>

```
Junio C Hamano <gitster@pobox.com> wrote:
> Eric Wong <normalperson@yhbt.net> writes:
> > "Kyle J. McKay" <mackyle@gmail.com> wrote:
> >> The updated patch with the additional fix is below.
> >
> > Thanks, signed-off and pushed to master on git://bogomips.org/git-svn
> > I've dropped my "destroy all tempfiles..." patch.
> 
> I think I missed this exchange.  I see these two patches:
> 
>     e0b4cad Git::SVN::*: avoid premature FileHandle closure
>     ce1b57b git-svn: fix localtime=true on non-glibc environments
> 
> on the 'master' branch of your git://git.bogomips.org/git-svn.git/
> repository.  Is this a good time to pull from you, and should I
> expect more during this cycle (I am guessing that the answers would
> be yes and no, but just to make sure...)?

Correct, it's a good time to pull and I don't expect to have more time
to work on new features (lazy load) for a bit.  I was under the
impression you already pulled:

http://mid.gmane.org/xmqq61aol44q.fsf@gitster.dls.corp.google.com

```

## Junio C Hamano, 2015-03-23 19:56

Subject: Re: git svn import failure : write .git/Git_svn_hash_BmjclS: Bad file descriptor
Message-ID: <xmqq619ra3no.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqq619ra3no.fsf%40gitster.dls.corp.google.com
In-Reply-To: <20150323193634.GA3454@dcvr.yhbt.net>

```
Eric Wong <normalperson@yhbt.net> writes:

> Correct, it's a good time to pull and I don't expect to have more time
> to work on new features (lazy load) for a bit.  I was under the
> impression you already pulled:
>
> http://mid.gmane.org/xmqq61aol44q.fsf@gitster.dls.corp.google.com

Indeed I did.  I was fooled by my own rebase X-<.

Thanks, then I am fully up to date with you.

```
