git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[JGIT PATCH 2/2] pgm.push: Ensure SSH connections are closed

From
Shawn O. Pearce <spearce@spearce.org>
Date
Aug 27, 2008, 23:02 UTC
Message-ID
<1219878126-18622-2-git-send-email-spearce@spearce.org>
In-Reply-To
<1219878126-18622-1-git-send-email-spearce@spearce.org>

If we don't close the transport when we are done with it the SSH session stored within the Transport will still be opened to the remote side. JSch may have created one or more background user threads to handle that connection, which means the JVM won't terminate cleanly when we are done with our work.

We must close each and every transport we opened.
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../src/org/spearce/jgit/pgm/Push.java             |   24 ++++++++++++-------
 1 files changed, 15 insertions(+), 9 deletions(-)
diff --git a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Push.java b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Push.java
index f53f2fe..53ad080 100644
--- a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Push.java
+++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Push.java
@@ -50,6 +50,7 @@
 import org.spearce.jgit.transport.RefSpec;
 import org.spearce.jgit.transport.RemoteRefUpdate;
 import org.spearce.jgit.transport.Transport;
+import org.spearce.jgit.transport.URIish;
 import org.spearce.jgit.transport.RemoteRefUpdate.Status;
 
 @Command(common = true, usage = "Update remote repository from local refs")
@@ -111,13 +112,18 @@ protected void run() throws Exception {
 			final Collection<RemoteRefUpdate> toPush = transport
 					.findRemoteRefUpdatesFor(refSpecs);
 
-			final PushResult result = transport.push(new TextProgressMonitor(),
-					toPush);
-			printPushResult(transport, result);
+			final URIish uri = transport.getURI();
+			final PushResult result;
+			try {
+				result = transport.push(new TextProgressMonitor(), toPush);
+			} finally {
+				transport.close();
+			}
+			printPushResult(uri, result);
 		}
 	}
 
-	private void printPushResult(final Transport transport,
+	private void printPushResult(final URIish uri,
 			final PushResult result) {
 		shownURI = false;
 		boolean everythingUpToDate = true;
@@ -126,7 +132,7 @@ private void printPushResult(final Transport transport,
 		for (final RemoteRefUpdate rru : result.getRemoteUpdates()) {
 			if (rru.getStatus() == Status.UP_TO_DATE) {
 				if (verbose)
-					printRefUpdateResult(transport, result, rru);
+					printRefUpdateResult(uri, result, rru);
 			} else
 				everythingUpToDate = false;
 		}
@@ -134,25 +140,25 @@ private void printPushResult(final Transport transport,
 		for (final RemoteRefUpdate rru : result.getRemoteUpdates()) {
 			// ...then successful updates...
 			if (rru.getStatus() == Status.OK)
-				printRefUpdateResult(transport, result, rru);
+				printRefUpdateResult(uri, result, rru);
 		}
 
 		for (final RemoteRefUpdate rru : result.getRemoteUpdates()) {
 			// ...finally, others (problematic)
 			if (rru.getStatus() != Status.OK
 					&& rru.getStatus() != Status.UP_TO_DATE)
-				printRefUpdateResult(transport, result, rru);
+				printRefUpdateResult(uri, result, rru);
 		}
 
 		if (everythingUpToDate)
 			out.println("Everything up-to-date");
 	}
 
-	private void printRefUpdateResult(final Transport transport,
+	private void printRefUpdateResult(final URIish uri,
 			final PushResult result, final RemoteRefUpdate rru) {
 		if (!shownURI) {
 			shownURI = true;
-			out.format("To %s\n", transport.getURI());
+			out.format("To %s\n", uri);
 		}
 
 		final String remoteName = rru.getRemoteName();
-- 
1.6.0.174.gd789c
Previous: Shawn O. PearceNext: Marek Zawirski
Message 2 of 5 in “Ignore unreadable SSH private keys when autoloading identities”
  1. 1/2 Ignore unreadable SSH private keys when autoloading identitiesShawn O. Pearce, Aug 27, 2008
  2. 2/2 pgm.push: Ensure SSH connections are closedShawn O. Pearce, Aug 27, 2008
  3. Marek ZawirskiAug 27, 2008
  4. Shawn O. PearceAug 27, 2008
  5. 1/2 Ignore unreadable SSH private keys when autoloading identitiesShawn O. Pearce, Aug 28, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.