This has been reported as Debian bug #359758 and it seems to be related to #187 too.
If the agent is used and input (to sign) is given via STDIN (e.g.):
echo foo | gpg --use-agent -a -b - > foo.asc
...
gpg-agent[XXX] DBG: <- OPTION ttyname=/dev/tty
...
gpg-agent[XXX]: command get_passphrase failed: Operation cancelled
gpg-agent[XXX.X] DBG: -> ERR 83886179 Operation cancelled <Pinentry>
gpg: cancelled by user
[GNUPG:] MISSING_PASSPHRASE
...
then ttyname is always /dev/tty and this (might) fails. In this case ttyname
always has to be corrected via GPG_TTY. But:
gpg --use-agent -a -b foo
...
gpg-agent[XXX] DBG: <- OPTION ttyname=/dev/pts/X
...
works as expected. In agent_send_all_options() you first try ttyname(0) and then
use tty_get_ttyname(). Maybe you need another special case for input via STDIN:
ttyname(fileno(stdin)) to fix this(?).