#882161 gpg: fails to sign in console after quitting X; works in console after a new login

Package:
gpg
Source:
gnupg2
Description:
GNU Privacy Guard -- minimalist public key operations
Submitter:
"Francesco Poli (wintermute)"
Date:
2017-11-19 18:36:04 UTC
Severity:
important
#882161#5
Date:
2017-11-19 18:32:48 UTC
From:
To:
Hello Debian GnuPG Maintainers!

I have the following pinentry packages installed:

  $ aptitude search pinentry | grep ^i
  i A pinentry-gtk2 - GTK+-2-based PIN or pass-phrase entry dialog for GnuPG
  i  pinentry-tty - minimal dumb-terminal PIN or pass-phrase entry for GnuPG

Hence, I expect gpg to work in text consoles (without X), as well as in
X sessions.

Well, I have no problems, when in an X session (pinentry-gtk2 asks for
the passphrase and gpg is able to sign and/or decrypt data).
On the other hand, I cannot use gpg in the text console, after exiting
from an X session.

Steps to reproduce the bug:

0) login on a text console (textual login on getty, I mean)

1) start an X session

     $ startx

2) use the X session a bit and then quit from it

3) back to the text console (the one where you logged in); try to sign
   a file

     $ gpg --sign foo
     gpg: using "XXXXXXXXXXXXXXXX" as default secret key for signing
     gpg: signing failed: Permission denied
     gpg: signing failed: Permission denied

   gpg exited with the above error and no foo.gpg was generated

     $ ls
     foo

4) exit from the text console

     $ exit

5) login again on the text console

6) try again (without starting an X session)

     $ gpg --sign foo

   pinentry-tty correctly asks for the passphrase and everything
   works as intended

7) file foo.gpg has been generated

     $ ls
     foo  foo.gpg


What's wrong?
What did I fail to understand?

Please let me know.
Thanks for your time!