Browse Source

Fix resource leaks due to unclosed repositories

Whenever a call to JGit returns a Repository the caller should make sure
to call close() on it if he doesn't need it anymore. Since instances of
Repository contain e.g. open FileOutputStreams (for pack files)
forgetting to close the repository can lead to resource leaks.

This was the reason why dozens of the JUnit tests failed on Windows
with "Can't delete file ...." errors. 

In LocalDiskRepositoryTestCase.tearDown() we tried to delete the
repositories we used during tests which failed because we had open
FileOutputStreams.

Not only the obvious cases during Clone or Init operations returned
Repositories, but also the new SubModule API created repository
instances. In some places we even forgot to close submodule repositories
in our internal coding.

To see the effects of this fix run the JGit JUnit tests under Windows.
On other platforms it's harder to see because either the leaking
resources don't lead to failing JUnit tests (on Unix you can delete
files with open FileOutputStreams) or the java gc runs differently and
cleans up the resources earlier.

Change-Id: I6d4f637b0d4af20ff4d501db091548696373a58a
Signed-off-by: Christian Halstrick <christian.halstrick@sap.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
stable-2.1
Christian Halstrick 13 years ago committed by Matthias Sohn
parent
commit
80113c7bb6
  1. 2
      org.eclipse.jgit.ant/src/org/eclipse/jgit/ant/tasks/GitCloneTask.java
  2. 14
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java
  3. 10
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java
  4. 29
      org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java
  5. 10
      org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleSyncTest.java
  6. 1
      org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleUpdateTest.java
  7. 2
      org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java
  8. 9
      org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java
  9. 9
      org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java
  10. 7
      org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleStatusCommand.java
  11. 34
      org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleSyncCommand.java
  12. 50
      org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleUpdateCommand.java
  13. 16
      org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleWalk.java

2
org.eclipse.jgit.ant/src/org/eclipse/jgit/ant/tasks/GitCloneTask.java

@ -109,7 +109,7 @@ public class GitCloneTask extends Task {
CloneCommand clone = Git.cloneRepository();
try {
clone.setURI(uri).setDirectory(destination).setBranch(branch).setBare(bare);
clone.call();
clone.call().getRepository().close();
} catch (Exception e) {
log("Could not clone repository: " + e, e, Project.MSG_ERR);
throw new BuildException("Could not clone repository: " + e.getMessage(), e);

14
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java

@ -281,6 +281,7 @@ public class CloneCommandTest extends RepositoryTestCase {
command.setURI(uri);
Repository repo = command.call();
assertNotNull(repo);
addRepoToClose(repo);
git.add().addFilepattern(path)
.addFilepattern(Constants.DOT_GIT_MODULES).call();
git.commit().setMessage("adding submodule").call();
@ -342,15 +343,19 @@ public class CloneCommandTest extends RepositoryTestCase {
assertNotNull(sub2Head);
// Add submodule 2 to submodule 1
assertNotNull(sub1Git.submoduleAdd().setPath(path)
.setURI(sub2.getDirectory().toURI().toString()).call());
Repository r = sub1Git.submoduleAdd().setPath(path)
.setURI(sub2.getDirectory().toURI().toString()).call();
assertNotNull(r);
addRepoToClose(r);
RevCommit sub1Head = sub1Git.commit().setAll(true)
.setMessage("Adding submodule").call();
assertNotNull(sub1Head);
// Add submodule 1 to default repository
assertNotNull(git.submoduleAdd().setPath(path)
.setURI(sub1.getDirectory().toURI().toString()).call());
r = git.submoduleAdd().setPath(path)
.setURI(sub1.getDirectory().toURI().toString()).call();
assertNotNull(r);
addRepoToClose(r);
assertNotNull(git.commit().setAll(true).setMessage("Adding submodule")
.call());
@ -383,6 +388,7 @@ public class CloneCommandTest extends RepositoryTestCase {
SubmoduleWalk walk = SubmoduleWalk.forIndex(git2.getRepository());
assertTrue(walk.next());
Repository clonedSub1 = walk.getRepository();
addRepoToClose(clonedSub1);
assertNotNull(clonedSub1);
status = new SubmoduleStatusCommand(clonedSub1);
statuses = status.call();

10
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java

@ -179,6 +179,7 @@ public class CommitCommandTest extends RepositoryTestCase {
command.setURI(uri);
Repository repo = command.call();
assertNotNull(repo);
addRepoToClose(repo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next());
@ -187,7 +188,9 @@ public class CommitCommandTest extends RepositoryTestCase {
assertEquals(uri, generator.getModulesUrl());
assertEquals(path, generator.getModulesPath());
assertEquals(uri, generator.getConfigUrl());
assertNotNull(generator.getRepository());
Repository subModRepo = generator.getRepository();
addRepoToClose(subModRepo);
assertNotNull(subModRepo);
assertEquals(commit, repo.resolve(Constants.HEAD));
RevCommit submoduleCommit = git.commit().setMessage("submodule add")
@ -224,6 +227,7 @@ public class CommitCommandTest extends RepositoryTestCase {
command.setURI(uri);
Repository repo = command.call();
assertNotNull(repo);
addRepoToClose(repo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next());
@ -232,7 +236,9 @@ public class CommitCommandTest extends RepositoryTestCase {
assertEquals(uri, generator.getModulesUrl());
assertEquals(path, generator.getModulesPath());
assertEquals(uri, generator.getConfigUrl());
assertNotNull(generator.getRepository());
Repository subModRepo = generator.getRepository();
addRepoToClose(subModRepo);
assertNotNull(subModRepo);
assertEquals(commit2, repo.resolve(Constants.HEAD));
RevCommit submoduleAddCommit = git.commit().setMessage("submodule add")

29
org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java

@ -78,7 +78,7 @@ public class SubmoduleAddTest extends RepositoryTestCase {
@Test
public void commandWithNullPath() throws GitAPIException {
try {
new SubmoduleAddCommand(db).setURI("uri").call();
new SubmoduleAddCommand(db).setURI("uri").call().close();
fail("Exception not thrown");
} catch (IllegalArgumentException e) {
assertEquals(JGitText.get().pathNotConfigured, e.getMessage());
@ -88,7 +88,8 @@ public class SubmoduleAddTest extends RepositoryTestCase {
@Test
public void commandWithEmptyPath() throws GitAPIException {
try {
new SubmoduleAddCommand(db).setPath("").setURI("uri").call();
new SubmoduleAddCommand(db).setPath("").setURI("uri").call()
.close();
fail("Exception not thrown");
} catch (IllegalArgumentException e) {
assertEquals(JGitText.get().pathNotConfigured, e.getMessage());
@ -98,7 +99,7 @@ public class SubmoduleAddTest extends RepositoryTestCase {
@Test
public void commandWithNullUri() throws GitAPIException {
try {
new SubmoduleAddCommand(db).setPath("sub").call();
new SubmoduleAddCommand(db).setPath("sub").call().close();
fail("Exception not thrown");
} catch (IllegalArgumentException e) {
assertEquals(JGitText.get().uriNotConfigured, e.getMessage());
@ -108,7 +109,8 @@ public class SubmoduleAddTest extends RepositoryTestCase {
@Test
public void commandWithEmptyUri() throws GitAPIException {
try {
new SubmoduleAddCommand(db).setPath("sub").setURI("").call();
new SubmoduleAddCommand(db).setPath("sub").setURI("").call()
.close();
fail("Exception not thrown");
} catch (IllegalArgumentException e) {
assertEquals(JGitText.get().uriNotConfigured, e.getMessage());
@ -129,6 +131,7 @@ public class SubmoduleAddTest extends RepositoryTestCase {
command.setURI(uri);
Repository repo = command.call();
assertNotNull(repo);
addRepoToClose(repo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next());
@ -137,7 +140,9 @@ public class SubmoduleAddTest extends RepositoryTestCase {
assertEquals(uri, generator.getModulesUrl());
assertEquals(path, generator.getModulesPath());
assertEquals(uri, generator.getConfigUrl());
assertNotNull(generator.getRepository());
Repository subModRepo = generator.getRepository();
addRepoToClose(subModRepo);
assertNotNull(subModRepo);
assertEquals(commit, repo.resolve(Constants.HEAD));
Status status = Git.wrap(db).status().call();
@ -165,7 +170,7 @@ public class SubmoduleAddTest extends RepositoryTestCase {
command.setPath(path);
command.setURI("git://server/repo.git");
try {
command.call();
command.call().close();
fail("Exception not thrown");
} catch (JGitInternalException e) {
assertEquals(
@ -188,6 +193,7 @@ public class SubmoduleAddTest extends RepositoryTestCase {
command.setURI(uri);
Repository repo = command.call();
assertNotNull(repo);
addRepoToClose(repo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next());
@ -199,11 +205,12 @@ public class SubmoduleAddTest extends RepositoryTestCase {
if (File.separatorChar == '\\')
fullUri = fullUri.replace('\\', '/');
assertEquals(fullUri, generator.getConfigUrl());
assertNotNull(generator.getRepository());
Repository subModRepo = generator.getRepository();
addRepoToClose(subModRepo);
assertNotNull(subModRepo);
assertEquals(
fullUri,
generator
.getRepository()
subModRepo
.getConfig()
.getString(ConfigConstants.CONFIG_REMOTE_SECTION,
Constants.DEFAULT_REMOTE_NAME,
@ -238,7 +245,9 @@ public class SubmoduleAddTest extends RepositoryTestCase {
command.setPath(path2);
String url2 = db.getDirectory().toURI().toString();
command.setURI(url2);
assertNotNull(command.call());
Repository r = command.call();
assertNotNull(r);
addRepoToClose(r);
modulesConfig.load();
assertEquals(path1, modulesConfig.getString(

10
org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleSyncTest.java

@ -115,6 +115,7 @@ public class SubmoduleSyncTest extends RepositoryTestCase {
.setURI(db.getDirectory().toURI().toString())
.setDirectory(new File(db.getWorkTree(), path)).call()
.getRepository();
addRepoToClose(subRepo);
assertNotNull(subRepo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
@ -133,7 +134,9 @@ public class SubmoduleSyncTest extends RepositoryTestCase {
generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next());
assertEquals(url, generator.getConfigUrl());
StoredConfig submoduleConfig = generator.getRepository().getConfig();
Repository subModRepository = generator.getRepository();
addRepoToClose(subModRepository);
StoredConfig submoduleConfig = subModRepository.getConfig();
assertEquals(url, submoduleConfig.getString(
ConfigConstants.CONFIG_REMOTE_SECTION,
Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL));
@ -181,6 +184,7 @@ public class SubmoduleSyncTest extends RepositoryTestCase {
.setDirectory(new File(db.getWorkTree(), path)).call()
.getRepository();
assertNotNull(subRepo);
addRepoToClose(subRepo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next());
@ -202,7 +206,9 @@ public class SubmoduleSyncTest extends RepositoryTestCase {
generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next());
assertEquals("git://server/sub.git", generator.getConfigUrl());
StoredConfig submoduleConfig = generator.getRepository().getConfig();
Repository subModRepository1 = generator.getRepository();
addRepoToClose(subModRepository1);
StoredConfig submoduleConfig = subModRepository1.getConfig();
assertEquals("git://server/sub.git", submoduleConfig.getString(
ConfigConstants.CONFIG_REMOTE_SECTION,
Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL));

1
org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleUpdateTest.java

@ -121,6 +121,7 @@ public class SubmoduleUpdateTest extends RepositoryTestCase {
SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next());
Repository subRepo = generator.getRepository();
addRepoToClose(subRepo);
assertNotNull(subRepo);
assertEquals(commit, subRepo.resolve(Constants.HEAD));
}

2
org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java

@ -169,6 +169,7 @@ public class SubmoduleWalkTest extends RepositoryTestCase {
assertNull(gen.getModulesUpdate());
assertNull(gen.getModulesUrl());
Repository subRepo = gen.getRepository();
addRepoToClose(subRepo);
assertNotNull(subRepo);
assertEquals(modulesGitDir, subRepo.getDirectory());
assertEquals(new File(db.getWorkTree(), path), subRepo.getWorkTree());
@ -217,6 +218,7 @@ public class SubmoduleWalkTest extends RepositoryTestCase {
assertNull(gen.getModulesUpdate());
assertNull(gen.getModulesUrl());
Repository subRepo = gen.getRepository();
addRepoToClose(subRepo);
assertNotNull(subRepo);
assertEquals(modulesGitDir, subRepo.getDirectory());
assertEquals(new File(db.getWorkTree(), path), subRepo.getWorkTree());

9
org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java

@ -256,7 +256,8 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
editor.commit();
Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
.setDirectory(new File(db.getWorkTree(), path)).call();
.setDirectory(new File(db.getWorkTree(), path)).call()
.getRepository().close();
TreeWalk walk = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
@ -355,7 +356,8 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
editor.commit();
Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
.setDirectory(new File(db.getWorkTree(), path)).call();
.setDirectory(new File(db.getWorkTree(), path)).call()
.getRepository().close();
TreeWalk walk = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
@ -388,7 +390,8 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
editor.commit();
Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
.setDirectory(new File(db.getWorkTree(), path)).call();
.setDirectory(new File(db.getWorkTree(), path)).call()
.getRepository().close();
TreeWalk walk = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());

9
org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java

@ -245,8 +245,13 @@ public class CloneCommand extends TransportCommand<CloneCommand, Git> {
SubmoduleWalk walk = SubmoduleWalk.forIndex(clonedRepo);
while (walk.next()) {
Repository subRepo = walk.getRepository();
if (subRepo != null)
cloneSubmodules(subRepo);
if (subRepo != null) {
try {
cloneSubmodules(subRepo);
} finally {
subRepo.close();
}
}
}
}
}

7
org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleStatusCommand.java

@ -130,7 +130,12 @@ public class SubmoduleStatusCommand extends
return new SubmoduleStatus(SubmoduleStatusType.UNINITIALIZED, path,
id);
ObjectId headId = subRepo.resolve(Constants.HEAD);
ObjectId headId;
try {
headId = subRepo.resolve(Constants.HEAD);
} finally {
subRepo.close();
}
// Report uninitialized if no HEAD commit in submodule repository
if (headId == null)

34
org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleSyncCommand.java

@ -130,21 +130,27 @@ public class SubmoduleSyncCommand extends GitCommand<Map<String, String>> {
if (subRepo == null)
continue;
StoredConfig subConfig = subRepo.getConfig();
// Get name of remote associated with current branch and
// fall back to default remote name as last resort
String branch = getHeadBranch(subRepo);
String remote = null;
if (branch != null)
remote = subConfig.getString(
ConfigConstants.CONFIG_BRANCH_SECTION, branch,
ConfigConstants.CONFIG_KEY_REMOTE);
if (remote == null)
remote = Constants.DEFAULT_REMOTE_NAME;
StoredConfig subConfig;
String branch;
try {
subConfig = subRepo.getConfig();
// Get name of remote associated with current branch and
// fall back to default remote name as last resort
branch = getHeadBranch(subRepo);
String remote = null;
if (branch != null)
remote = subConfig.getString(
ConfigConstants.CONFIG_BRANCH_SECTION, branch,
ConfigConstants.CONFIG_KEY_REMOTE);
if (remote == null)
remote = Constants.DEFAULT_REMOTE_NAME;
subConfig.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
remote, ConfigConstants.CONFIG_KEY_URL, remoteUrl);
subConfig.save();
subConfig.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
remote, ConfigConstants.CONFIG_KEY_URL, remoteUrl);
subConfig.save();
} finally {
subRepo.close();
}
}
if (!synced.isEmpty())
config.save();

50
org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleUpdateCommand.java

@ -164,29 +164,35 @@ public class SubmoduleUpdateCommand extends
submoduleRepo = clone.call().getRepository();
}
RevWalk walk = new RevWalk(submoduleRepo);
RevCommit commit = walk.parseCommit(generator.getObjectId());
try {
RevWalk walk = new RevWalk(submoduleRepo);
RevCommit commit = walk
.parseCommit(generator.getObjectId());
String update = generator.getConfigUpdate();
if (ConfigConstants.CONFIG_KEY_MERGE.equals(update)) {
MergeCommand merge = new MergeCommand(submoduleRepo);
merge.include(commit);
merge.call();
} else if (ConfigConstants.CONFIG_KEY_REBASE.equals(update)) {
RebaseCommand rebase = new RebaseCommand(submoduleRepo);
rebase.setUpstream(commit);
rebase.call();
} else {
// Checkout commit referenced in parent repository's index
// as a detached HEAD
DirCacheCheckout co = new DirCacheCheckout(submoduleRepo,
submoduleRepo.lockDirCache(), commit.getTree());
co.setFailOnConflict(true);
co.checkout();
RefUpdate refUpdate = submoduleRepo.updateRef(
Constants.HEAD, true);
refUpdate.setNewObjectId(commit);
refUpdate.forceUpdate();
String update = generator.getConfigUpdate();
if (ConfigConstants.CONFIG_KEY_MERGE.equals(update)) {
MergeCommand merge = new MergeCommand(submoduleRepo);
merge.include(commit);
merge.call();
} else if (ConfigConstants.CONFIG_KEY_REBASE.equals(update)) {
RebaseCommand rebase = new RebaseCommand(submoduleRepo);
rebase.setUpstream(commit);
rebase.call();
} else {
// Checkout commit referenced in parent repository's
// index as a detached HEAD
DirCacheCheckout co = new DirCacheCheckout(
submoduleRepo, submoduleRepo.lockDirCache(),
commit.getTree());
co.setFailOnConflict(true);
co.checkout();
RefUpdate refUpdate = submoduleRepo.updateRef(
Constants.HEAD, true);
refUpdate.setNewObjectId(commit);
refUpdate.forceUpdate();
}
} finally {
submoduleRepo.close();
}
updated.add(generator.getPath());
}

16
org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleWalk.java

@ -627,7 +627,13 @@ public class SubmoduleWalk {
*/
public ObjectId getHead() throws IOException {
Repository subRepo = getRepository();
return subRepo != null ? subRepo.resolve(Constants.HEAD) : null;
if (subRepo == null)
return null;
try {
return subRepo.resolve(Constants.HEAD);
} finally {
subRepo.close();
}
}
/**
@ -640,8 +646,12 @@ public class SubmoduleWalk {
Repository subRepo = getRepository();
if (subRepo == null)
return null;
Ref head = subRepo.getRef(Constants.HEAD);
return head != null ? head.getLeaf().getName() : null;
try {
Ref head = subRepo.getRef(Constants.HEAD);
return head != null ? head.getLeaf().getName() : null;
} finally {
subRepo.close();
}
}
/**

Loading…
Cancel
Save