Skip to content

lock SendData to fix random connection failures #1623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/Renci.SshNet/Channels/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal abstract class Channel : IChannel
{
private readonly Lock _serverWindowSizeLock = new Lock();
private readonly Lock _messagingLock = new Lock();
private readonly Lock _sendDataLock = new Lock();
private readonly uint _initialWindowSize;
private readonly ISession _session;
private readonly ILogger _logger;
Expand Down Expand Up @@ -340,19 +341,22 @@ public void SendData(byte[] data, int offset, int size)
return;
}

var totalBytesToSend = size;
while (totalBytesToSend > 0)
lock (_sendDataLock)
{
var sizeOfCurrentMessage = GetDataLengthThatCanBeSentInMessage(totalBytesToSend);
var totalBytesToSend = size;
while (totalBytesToSend > 0)
{
var sizeOfCurrentMessage = GetDataLengthThatCanBeSentInMessage(totalBytesToSend);

var channelDataMessage = new ChannelDataMessage(RemoteChannelNumber,
data,
offset,
sizeOfCurrentMessage);
_session.SendMessage(channelDataMessage);
var channelDataMessage = new ChannelDataMessage(RemoteChannelNumber,
data,
offset,
sizeOfCurrentMessage);
_session.SendMessage(channelDataMessage);

totalBytesToSend -= sizeOfCurrentMessage;
offset += sizeOfCurrentMessage;
totalBytesToSend -= sizeOfCurrentMessage;
offset += sizeOfCurrentMessage;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@ public void Test_Sftp_Upload_Forbidden()
[TestCategory("Sftp")]
public void Test_Sftp_Multiple_Async_Upload_And_Download_10Files_5MB_Each()
{
if (Environment.GetEnvironmentVariable("CI") == "true")
{
Assert.Inconclusive("Skipping because of failures in CI, see #1253");
}

var maxFiles = 10;
var maxSize = 5;

Expand Down