We run gpg2 from another program. A typical command would be:
gpg2 --homedir $tmpdir --status-file logfile --import keyfile
In all cases we pass a temporary directory as --homedir because we don't want to interfere or use the user's own gpg keys.
After the program runs the temporary directory is deleted.
After this, gpg-agent instances are found to be leaking:
16543 ? Ss 0:00 gpg-agent --homedir /tmp/virt-builder.eYvl5v/vb.gpghome.zyHsvg --use-standard-socket --daemon 16559 ? Ss 0:00 gpg-agent --homedir /tmp/virt-builder.eYvl5v/vb.gpghome.MN7zSD --use-standard-socket --daemon
They basically hang around forever as far as I can tell.