Cleanup gpg-agent instances and home directories (#1629)

* Cleanup gpg-agent instances and home directories

The GpgSystemCommandException leaks home directories, but more importantly it
leaks gpg-agent instances.  This can cause problems with inotify limits, since
the agent seems to make use of inotify.  Do a proper cleanup in afterEach().

* Don't fail if we can't kill the agent
This commit is contained in:
Michael Muller 2022-05-16 14:06:26 -04:00 committed by GitHub
parent fd9fe398ae
commit 33ffcfdab3

View file

@ -21,6 +21,7 @@ import static com.google.common.base.Strings.isNullOrEmpty;
import static com.google.common.truth.Truth.assertWithMessage; import static com.google.common.truth.Truth.assertWithMessage;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.flogger.FluentLogger;
import com.google.common.io.ByteSource; import com.google.common.io.ByteSource;
import com.google.common.io.CharStreams; import com.google.common.io.CharStreams;
import java.io.File; import java.io.File;
@ -43,6 +44,7 @@ import org.junit.jupiter.api.extension.ExtensionContext;
*/ */
public final class GpgSystemCommandExtension implements BeforeEachCallback, AfterEachCallback { public final class GpgSystemCommandExtension implements BeforeEachCallback, AfterEachCallback {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private static final File DEV_NULL = new File("/dev/null"); private static final File DEV_NULL = new File("/dev/null");
private static final String TEMP_FILE_PREFIX = "gpgtest"; private static final String TEMP_FILE_PREFIX = "gpgtest";
@ -122,9 +124,29 @@ public final class GpgSystemCommandExtension implements BeforeEachCallback, Afte
.isEqualTo(0); .isEqualTo(0);
} }
static void deleteTree(File dir) {
for (File file : dir.listFiles()) {
if (file.isDirectory()) {
deleteTree(file);
} else {
file.delete();
}
}
dir.delete();
}
@Override @Override
public void afterEach(ExtensionContext context) { public void afterEach(ExtensionContext context) {
// TODO(weiminyu): we should delete the cwd tree. // Kill the gpg-agent.
try {
exec("gpgconf", "--homedir", conf.getPath(), "--kill", "gpg-agent");
} catch (IOException e) {
logger.atInfo().log("Unable to kill gpg-agent: %s", e);
}
// Clean up the temporary directory.
deleteTree(cwd);
cwd = DEV_NULL; cwd = DEV_NULL;
conf = DEV_NULL; conf = DEV_NULL;
} }