You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@subversion.apache.org by Alan Barrett <ap...@cequrux.com> on 2006/03/21 15:24:13 UTC

Bug with svn+ssh (plus workaround)

I found a horrible interaction between subversion and ssh.

This works:

        svn log svn+ssh://host/repo/trunk | less

This doesn't work:

        svn log svn+ssh://host/repo/trunk 2>&1 | less

The symptom is that the output is truncated after the first few
kilobytes.

The underlying cause is this sequence of events:

1. The shell runs "svn" in such a way that both fd 1 (stdout) and fd 2
   (stderr) are connected to a pipe which goes to "less".

2. svn runs ssh, in such a way that ssh's fd 0 (stdin) and fd 1
   (stdout) are connected to pipes controlled by svn, but ssh's fd 2
   (stderr) is still connected to the pipe that goes to "less".  In
   other words, all three of svn's fd 1 (stdout), svn's fd 2 (stderr),
   and ssh's fd 2 (stderr) are connected to the same place.

3. ssh puts its stderr into non-blocking mode.  Because ssh's stderr
   and svn's stdout and stderr all go to the same place, this
   accidentally puts svn's stdout and stderr into non-blocking mode.

4. After a while, svn starts writing to stdout.  The first few writes
   work fine.  But after a few kilobytes, a write attempt gets an
   EAGAIN error (a.k.a. EWOULDBLOCK).  The pipe is full and the write
   would have to block until "less" drains some data from the other
   end of the pipe; but the file descriptor is in non-blocking mode,
   so the write gets an error instead of blocking.

5. svn gives up.  It tries to write an error message to stderr, but
   that also encounters an EAGAIN/EWOULDBLOCK error.

As a workaround, I pointed the SVN_SSH environment variable to this
script, and everything worked correctly:

    #!/bin/sh
    # Run ssh in such a way that a pipe through a "cat" process is
    # interposed between the inner fd 2 (stderr) seen by the ssh
    # command, and the outer fd 2 (stderr) seen by our caller (which
    # is typically "svn").  This insulates the outer fd 2 from ssh's
    # habit of putting the inner fd 2 into non-blocking mode.
    PATH=/bin:/usr/bin
    REAL_SSH=/usr/bin/ssh
    exec 4>&1
    exec "${REAL_SSH}" ${1+"$@"} 1>&4 2>&1 4>&- | cat 1>&2

As a fix, I suggest that svn should connect ssh's stdout to a pipe
that is managed by svn.

--apb (Alan Barrett)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: Bug with svn+ssh (plus workaround)

Posted by Vincent Lefevre <vi...@vinc17.org>.
On 2006-03-21 18:48:07 +0200, Alan Barrett wrote:
> Anyway, ssh tries to use /dev/tty in preference to stderr for
> authentication prompts.  If /dev/tty doesn't work, then it tries to
> prompt on stderr and read from stdin, but it's unlikely that that will
> work when ssh's stdin is a pipe to svn.

I confirm that redirecting stderr works. I've often used that with cvs:

cvst: aliased to cvs status |& grep Status: | grep -v Up-to-date

and I could always type my password.

-- 
Vincent Lefèvre <vi...@vinc17.org> - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / SPACES project at LORIA

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: Bug with svn+ssh (plus workaround)

Posted by Alan Barrett <ap...@cequrux.com>.
On Tue, 21 Mar 2006, Joseph Galbraith wrote:
> Alan Barrett wrote:
> > This doesn't work:
> > 
> >         svn log svn+ssh://host/repo/trunk 2>&1 | less
> > 
> > The symptom is that the output is truncated after the first few
> > kilobytes.
> > [...]
> > As a fix, I suggest that svn should connect ssh's stdout to a pipe
> > that is managed by svn.
> 
> I assume you meant stderr here, since it already does so
> with stdout (according to your message.)

Yes, I meant "stderr".

> The reason not to do this is that ssh uses stderr to prompt
> for authentication credentials.  By leaving stderr connected
> to terminal (in most cases) the user can actually type
> his password/passphrase/whatever to authenticate
> to the server.  If we redirect stderr, then this will no
> longer work.

I didn't mean "connect ssh's stderr to a pipe that swallows the output",
I meant "connect ssh to a pipe that copies the output to the user".

Anyway, ssh tries to use /dev/tty in preference to stderr for
authentication prompts.  If /dev/tty doesn't work, then it tries to
prompt on stderr and read from stdin, but it's unlikely that that will
work when ssh's stdin is a pipe to svn.

--apb (Alan Barrett)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: Bug with svn+ssh (plus workaround)

Posted by Joseph Galbraith <ga...@vandyke.com>.
Alan Barrett wrote:
> I found a horrible interaction between subversion and ssh.
> 
> This works:
> 
>         svn log svn+ssh://host/repo/trunk | less
> 
> This doesn't work:
> 
>         svn log svn+ssh://host/repo/trunk 2>&1 | less
> 
> The symptom is that the output is truncated after the first few
> kilobytes.
> 
> The underlying cause is this sequence of events:
> 
> 1. The shell runs "svn" in such a way that both fd 1 (stdout) and fd 2
>    (stderr) are connected to a pipe which goes to "less".
> 
> 2. svn runs ssh, in such a way that ssh's fd 0 (stdin) and fd 1
>    (stdout) are connected to pipes controlled by svn, but ssh's fd 2
>    (stderr) is still connected to the pipe that goes to "less".  In
>    other words, all three of svn's fd 1 (stdout), svn's fd 2 (stderr),
>    and ssh's fd 2 (stderr) are connected to the same place.
> 
> 3. ssh puts its stderr into non-blocking mode.  Because ssh's stderr
>    and svn's stdout and stderr all go to the same place, this
>    accidentally puts svn's stdout and stderr into non-blocking mode.
> 
> 4. After a while, svn starts writing to stdout.  The first few writes
>    work fine.  But after a few kilobytes, a write attempt gets an
>    EAGAIN error (a.k.a. EWOULDBLOCK).  The pipe is full and the write
>    would have to block until "less" drains some data from the other
>    end of the pipe; but the file descriptor is in non-blocking mode,
>    so the write gets an error instead of blocking.
> 
> 5. svn gives up.  It tries to write an error message to stderr, but
>    that also encounters an EAGAIN/EWOULDBLOCK error.
> 
> As a workaround, I pointed the SVN_SSH environment variable to this
> script, and everything worked correctly:
> 
>     #!/bin/sh
>     # Run ssh in such a way that a pipe through a "cat" process is
>     # interposed between the inner fd 2 (stderr) seen by the ssh
>     # command, and the outer fd 2 (stderr) seen by our caller (which
>     # is typically "svn").  This insulates the outer fd 2 from ssh's
>     # habit of putting the inner fd 2 into non-blocking mode.
>     PATH=/bin:/usr/bin
>     REAL_SSH=/usr/bin/ssh
>     exec 4>&1
>     exec "${REAL_SSH}" ${1+"$@"} 1>&4 2>&1 4>&- | cat 1>&2
> 
> As a fix, I suggest that svn should connect ssh's stdout to a pipe
> that is managed by svn.

I assume you meant stderr here, since it already does so
with stdout (according to your message.)

The reason not to do this is that ssh uses stderr to prompt
for authentication credentials.  By leaving stderr connected
to terminal (in most cases) the user can actually type
his password/passphrase/whatever to authenticate
to the server.  If we redirect stderr, then this will no
longer work.

Thanks,

Joseph

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: Bug with svn+ssh (plus workaround)

Posted by Alan Barrett <ap...@cequrux.com>.
On Tue, 21 Mar 2006, Alan Barrett wrote:
> As a workaround, I pointed the SVN_SSH environment variable to this
> script, and everything worked correctly:
> 
>     #!/bin/sh
>     # Run ssh in such a way that a pipe through a "cat" process is
>     # interposed between the inner fd 2 (stderr) seen by the ssh
>     # command, and the outer fd 2 (stderr) seen by our caller (which
>     # is typically "svn").  This insulates the outer fd 2 from ssh's
>     # habit of putting the inner fd 2 into non-blocking mode.
>     PATH=/bin:/usr/bin
>     REAL_SSH=/usr/bin/ssh
>     exec 4>&1
>     exec "${REAL_SSH}" ${1+"$@"} 1>&4 2>&1 4>&- | cat 1>&2

Of course, the last line should have been

    exec "${REAL_SSH}" ${1+"$@"} 2>&1 1>&4 4>&- | cat 1>&2

The incorrect version appeared to work because ssh didn't actually
write anything to stderr.

--apb (Alan Barrett)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: Bug with svn+ssh (plus workaround)

Posted by Greg Hudson <gh...@MIT.EDU>.
On Tue, 2006-03-21 at 17:24 +0200, Alan Barrett wrote:
> I found a horrible interaction between subversion and ssh.

I blame Unix.  O_NONBLOCK should be a file descriptor flag, like
FD_CLOEXEC is, not an open file description flag.  I've run into this
kind of interaction in other contexts as well.

Given that Unix probably won't change (introducing FD_NONBLOCK and
defining its interactions with O_NONBLOCK would be pretty confusing, to
solve what is generally a small problem, and of course "Unix" is a dozen
different operating systems whose programmers don't talk to each other
much), I also blame ssh.  You shouldn't set an fd non-blocking if it
might be shared with code you don't control, and there are alternatives
to non-blocking I/O.

Joseph Galbraith wrote:
> The reason not to do this is that ssh uses stderr to prompt
> for authentication credentials.

That would surprise me.  I would expect it to use /dev/tty, since it has
to use that to read input from the user anyway (stdin is controlled by
Subversion).

Even if so, we would presumably just be funneling ssh's stderr pipe to
Subversion's own stderr.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: Bug with svn+ssh (plus workaround)

Posted by Alan Barrett <ap...@cequrux.com>.
On Wed, 22 Mar 2006, Julian Foad wrote:
> Alan Barrett wrote:
> >As a fix, I suggest that svn should connect ssh's stdout to a pipe
> >that is managed by svn.
>
> That seems right.  Are you willing and able to write a patch to do
> this?

Yes, I'll give it a go.  I know nothing about APR, but I am willing to
learn.

--apb (Alan Barrett)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: Bug with svn+ssh (plus workaround)

Posted by Julian Foad <ju...@btopenworld.com>.
Alan Barrett wrote:
> As a fix, I suggest that svn should connect ssh's stdout to a pipe
> that is managed by svn.

That seems right.  Are you willing and able to write a patch to do this?  If 
so, people on this list would be glad to help you with the details of it.  If 
not, please file a report in the issue tracker.

- Julian

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org