Skip to content

Commit 3808aae

Browse files
committed
Avoid extra IsOpen invocation when attempting to open channel session.
1 parent bb968f5 commit 3808aae

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

src/Renci.SshNet/Channels/ChannelSession.cs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Globalization;
44
using System.Threading;
5-
using Renci.SshNet.Abstractions;
65
using Renci.SshNet.Common;
76
using Renci.SshNet.Messages.Connection;
87

@@ -65,27 +64,27 @@ public override ChannelTypes ChannelType
6564
/// </summary>
6665
public virtual void Open()
6766
{
68-
if (!IsOpen)
67+
if (IsOpen)
68+
return;
69+
70+
// Try to open channel several times
71+
do
6972
{
70-
// Try to open channel several times
71-
while (!IsOpen && _failedOpenAttempts < ConnectionInfo.RetryAttempts)
73+
SendChannelOpenMessage();
74+
try
75+
{
76+
WaitOnHandle(_channelOpenResponseWaitHandle);
77+
}
78+
catch (Exception)
7279
{
73-
SendChannelOpenMessage();
74-
try
75-
{
76-
WaitOnHandle(_channelOpenResponseWaitHandle);
77-
}
78-
catch (Exception)
79-
{
80-
// avoid leaking session semaphore
81-
ReleaseSemaphore();
82-
throw;
83-
}
80+
// avoid leaking session semaphore
81+
ReleaseSemaphore();
82+
throw;
8483
}
84+
} while (!IsOpen && _failedOpenAttempts < ConnectionInfo.RetryAttempts);
8585

86-
if (!IsOpen)
87-
throw new SshException(string.Format(CultureInfo.CurrentCulture, "Failed to open a channel after {0} attempts.", _failedOpenAttempts));
88-
}
86+
if (!IsOpen)
87+
throw new SshException(string.Format(CultureInfo.CurrentCulture, "Failed to open a channel after {0} attempts.", _failedOpenAttempts));
8988
}
9089

9190
/// <summary>
@@ -386,16 +385,18 @@ protected override void Dispose(bool disposing)
386385

387386
if (disposing)
388387
{
389-
if (_channelOpenResponseWaitHandle != null)
388+
var channelOpenResponseWaitHandle = _channelOpenResponseWaitHandle;
389+
if (channelOpenResponseWaitHandle != null)
390390
{
391-
_channelOpenResponseWaitHandle.Dispose();
392391
_channelOpenResponseWaitHandle = null;
392+
channelOpenResponseWaitHandle.Dispose();
393393
}
394394

395-
if (_channelRequestResponse != null)
395+
var channelRequestResponse = _channelRequestResponse;
396+
if (channelRequestResponse != null)
396397
{
397-
_channelRequestResponse.Dispose();
398398
_channelRequestResponse = null;
399+
channelRequestResponse.Dispose();
399400
}
400401
}
401402
}

0 commit comments

Comments
 (0)