@@ -137,7 +137,9 @@ func (t *sshSmartSubtransport) Action(urlString string, action git2go.SmartServi
137137 if t .lastAction == git2go .SmartServiceActionUploadpackLs {
138138 return t .currentStream , nil
139139 }
140- t .Close ()
140+ if err := t .Close (); err != nil {
141+ traceLog .Error (err , "[ssh]: error cleaning up previous stream" )
142+ }
141143 }
142144 cmd = fmt .Sprintf ("git-upload-pack '%s'" , uPath )
143145
@@ -146,7 +148,9 @@ func (t *sshSmartSubtransport) Action(urlString string, action git2go.SmartServi
146148 if t .lastAction == git2go .SmartServiceActionReceivepackLs {
147149 return t .currentStream , nil
148150 }
149- t .Close ()
151+ if err := t .Close (); err != nil {
152+ traceLog .Error (err , "[ssh]: error cleaning up previous stream" )
153+ }
150154 }
151155 cmd = fmt .Sprintf ("git-receive-pack '%s'" , uPath )
152156
@@ -161,11 +165,11 @@ func (t *sshSmartSubtransport) Action(urlString string, action git2go.SmartServi
161165 defer cred .Free ()
162166
163167 var addr string
168+ port := "22"
164169 if u .Port () != "" {
165- addr = fmt .Sprintf ("%s:%s" , u .Hostname (), u .Port ())
166- } else {
167- addr = fmt .Sprintf ("%s:22" , u .Hostname ())
170+ port = u .Port ()
168171 }
172+ addr = fmt .Sprintf ("%s:%s" , u .Hostname (), port )
169173
170174 ckey , sshConfig , err := cacheKeyAndConfig (addr , cred )
171175 if err != nil {
@@ -264,19 +268,21 @@ func (t *sshSmartSubtransport) Close() error {
264268
265269 traceLog .Info ("[ssh]: sshSmartSubtransport.Close()" )
266270 t .currentStream = nil
267- if t .client != nil {
271+ if t .client != nil && t . stdin != nil {
268272 if err := t .stdin .Close (); err != nil {
269273 returnErr = fmt .Errorf ("cannot close stdin: %w" , err )
270274 }
271- t .client = nil
272275 }
276+ t .client = nil
277+
273278 if t .session != nil {
274279 traceLog .Info ("[ssh]: skipping session.wait" )
275280 traceLog .Info ("[ssh]: session.Close()" )
276281 if err := t .session .Close (); err != nil {
277282 returnErr = fmt .Errorf ("cannot close session: %w" , err )
278283 }
279284 }
285+ t .session = nil
280286
281287 return returnErr
282288}
@@ -302,6 +308,10 @@ func (stream *sshSmartSubtransportStream) Free() {
302308}
303309
304310func cacheKeyAndConfig (remoteAddress string , cred * git2go.Credential ) (string , * ssh.ClientConfig , error ) {
311+ if cred == nil {
312+ return "" , nil , fmt .Errorf ("cannot create cache key from a nil credential" )
313+ }
314+
305315 username , _ , privatekey , passphrase , err := cred .GetSSHKey ()
306316 if err != nil {
307317 return "" , nil , err
0 commit comments