Skip to content

Commit c28f023

Browse files
authored
Swallow ObjectDisposed on SFTP wait handle when receiving late response (#1531)
1 parent 3e12c96 commit c28f023

File tree

2 files changed

+51
-36
lines changed

2 files changed

+51
-36
lines changed

src/Renci.SshNet/Common/Extensions.cs

+18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Numerics;
88
using System.Runtime.CompilerServices;
99
using System.Text;
10+
using System.Threading;
1011

1112
using Renci.SshNet.Abstractions;
1213
using Renci.SshNet.Messages;
@@ -120,6 +121,23 @@ public static byte[] ExportKeyParameter(this BigInteger value, int length)
120121
return target;
121122
}
122123

124+
/// <summary>
125+
/// Sets a wait handle, swallowing any resulting <see cref="ObjectDisposedException"/>.
126+
/// Used in cases where set and dispose may race.
127+
/// </summary>
128+
/// <param name="waitHandle">The wait handle to set.</param>
129+
public static void SetIgnoringObjectDisposed(this EventWaitHandle waitHandle)
130+
{
131+
try
132+
{
133+
_ = waitHandle.Set();
134+
}
135+
catch (ObjectDisposedException)
136+
{
137+
// ODE intentionally ignored.
138+
}
139+
}
140+
123141
/// <summary>
124142
/// Reverses the sequence of the elements in the entire one-dimensional <see cref="Array"/>.
125143
/// </summary>

src/Renci.SshNet/Sftp/SftpSession.cs

+33-36
Original file line numberDiff line numberDiff line change
@@ -492,12 +492,12 @@ public byte[] RequestOpen(string path, Flags flags, bool nullOnError = false)
492492
response =>
493493
{
494494
handle = response.Handle;
495-
_ = wait.Set();
495+
wait.SetIgnoringObjectDisposed();
496496
},
497497
response =>
498498
{
499499
exception = GetSftpException(response);
500-
_ = wait.Set();
500+
wait.SetIgnoringObjectDisposed();
501501
});
502502

503503
SendRequest(request);
@@ -625,7 +625,7 @@ public void RequestClose(byte[] handle)
625625
response =>
626626
{
627627
exception = GetSftpException(response);
628-
_ = wait.Set();
628+
wait.SetIgnoringObjectDisposed();
629629
});
630630

631631
SendRequest(request);
@@ -825,7 +825,7 @@ public byte[] RequestRead(byte[] handle, ulong offset, uint length)
825825
response =>
826826
{
827827
data = response.Data;
828-
_ = wait.Set();
828+
wait.SetIgnoringObjectDisposed();
829829
},
830830
response =>
831831
{
@@ -838,7 +838,7 @@ public byte[] RequestRead(byte[] handle, ulong offset, uint length)
838838
data = Array.Empty<byte>();
839839
}
840840

841-
_ = wait.Set();
841+
wait.SetIgnoringObjectDisposed();
842842
});
843843

844844
SendRequest(request);
@@ -928,10 +928,7 @@ public void RequestWrite(byte[] handle,
928928
writeCompleted?.Invoke(response);
929929

930930
exception = GetSftpException(response);
931-
if (wait != null)
932-
{
933-
_ = wait.Set();
934-
}
931+
wait?.SetIgnoringObjectDisposed();
935932
});
936933

937934
SendRequest(request);
@@ -1011,12 +1008,12 @@ public SftpFileAttributes RequestLStat(string path)
10111008
response =>
10121009
{
10131010
attributes = response.Attributes;
1014-
_ = wait.Set();
1011+
wait.SetIgnoringObjectDisposed();
10151012
},
10161013
response =>
10171014
{
10181015
exception = GetSftpException(response);
1019-
_ = wait.Set();
1016+
wait.SetIgnoringObjectDisposed();
10201017
});
10211018

10221019
SendRequest(request);
@@ -1140,12 +1137,12 @@ public SftpFileAttributes RequestFStat(byte[] handle, bool nullOnError)
11401137
response =>
11411138
{
11421139
attributes = response.Attributes;
1143-
_ = wait.Set();
1140+
wait.SetIgnoringObjectDisposed();
11441141
},
11451142
response =>
11461143
{
11471144
exception = GetSftpException(response);
1148-
_ = wait.Set();
1145+
wait.SetIgnoringObjectDisposed();
11491146
});
11501147

11511148
SendRequest(request);
@@ -1207,7 +1204,7 @@ public void RequestSetStat(string path, SftpFileAttributes attributes)
12071204
response =>
12081205
{
12091206
exception = GetSftpException(response);
1210-
_ = wait.Set();
1207+
wait.SetIgnoringObjectDisposed();
12111208
});
12121209

12131210
SendRequest(request);
@@ -1239,7 +1236,7 @@ public void RequestFSetStat(byte[] handle, SftpFileAttributes attributes)
12391236
response =>
12401237
{
12411238
exception = GetSftpException(response);
1242-
_ = wait.Set();
1239+
wait.SetIgnoringObjectDisposed();
12431240
});
12441241

12451242
SendRequest(request);
@@ -1274,12 +1271,12 @@ public byte[] RequestOpenDir(string path, bool nullOnError = false)
12741271
response =>
12751272
{
12761273
handle = response.Handle;
1277-
_ = wait.Set();
1274+
wait.SetIgnoringObjectDisposed();
12781275
},
12791276
response =>
12801277
{
12811278
exception = GetSftpException(response);
1282-
_ = wait.Set();
1279+
wait.SetIgnoringObjectDisposed();
12831280
});
12841281

12851282
SendRequest(request);
@@ -1345,7 +1342,7 @@ public KeyValuePair<string, SftpFileAttributes>[] RequestReadDir(byte[] handle)
13451342
response =>
13461343
{
13471344
result = response.Files;
1348-
_ = wait.Set();
1345+
wait.SetIgnoringObjectDisposed();
13491346
},
13501347
response =>
13511348
{
@@ -1354,7 +1351,7 @@ public KeyValuePair<string, SftpFileAttributes>[] RequestReadDir(byte[] handle)
13541351
exception = GetSftpException(response);
13551352
}
13561353

1357-
_ = wait.Set();
1354+
wait.SetIgnoringObjectDisposed();
13581355
});
13591356

13601357
SendRequest(request);
@@ -1426,7 +1423,7 @@ public void RequestRemove(string path)
14261423
response =>
14271424
{
14281425
exception = GetSftpException(response);
1429-
_ = wait.Set();
1426+
wait.SetIgnoringObjectDisposed();
14301427
});
14311428

14321429
SendRequest(request);
@@ -1493,7 +1490,7 @@ public void RequestMkDir(string path)
14931490
response =>
14941491
{
14951492
exception = GetSftpException(response);
1496-
_ = wait.Set();
1493+
wait.SetIgnoringObjectDisposed();
14971494
});
14981495

14991496
SendRequest(request);
@@ -1558,7 +1555,7 @@ public void RequestRmDir(string path)
15581555
response =>
15591556
{
15601557
exception = GetSftpException(response);
1561-
_ = wait.Set();
1558+
wait.SetIgnoringObjectDisposed();
15621559
});
15631560

15641561
SendRequest(request);
@@ -1625,12 +1622,12 @@ internal KeyValuePair<string, SftpFileAttributes>[] RequestRealPath(string path,
16251622
response =>
16261623
{
16271624
result = response.Files;
1628-
_ = wait.Set();
1625+
wait.SetIgnoringObjectDisposed();
16291626
},
16301627
response =>
16311628
{
16321629
exception = GetSftpException(response);
1633-
_ = wait.Set();
1630+
wait.SetIgnoringObjectDisposed();
16341631
});
16351632

16361633
SendRequest(request);
@@ -1751,12 +1748,12 @@ public SftpFileAttributes RequestStat(string path, bool nullOnError = false)
17511748
response =>
17521749
{
17531750
attributes = response.Attributes;
1754-
_ = wait.Set();
1751+
wait.SetIgnoringObjectDisposed();
17551752
},
17561753
response =>
17571754
{
17581755
exception = GetSftpException(response);
1759-
_ = wait.Set();
1756+
wait.SetIgnoringObjectDisposed();
17601757
});
17611758

17621759
SendRequest(request);
@@ -1849,7 +1846,7 @@ public void RequestRename(string oldPath, string newPath)
18491846
response =>
18501847
{
18511848
exception = GetSftpException(response);
1852-
_ = wait.Set();
1849+
wait.SetIgnoringObjectDisposed();
18531850
});
18541851

18551852
SendRequest(request);
@@ -1930,12 +1927,12 @@ internal KeyValuePair<string, SftpFileAttributes>[] RequestReadLink(string path,
19301927
response =>
19311928
{
19321929
result = response.Files;
1933-
_ = wait.Set();
1930+
wait.SetIgnoringObjectDisposed();
19341931
},
19351932
response =>
19361933
{
19371934
exception = GetSftpException(response);
1938-
_ = wait.Set();
1935+
wait.SetIgnoringObjectDisposed();
19391936
});
19401937

19411938
SendRequest(request);
@@ -1975,7 +1972,7 @@ public void RequestSymLink(string linkpath, string targetpath)
19751972
response =>
19761973
{
19771974
exception = GetSftpException(response);
1978-
_ = wait.Set();
1975+
wait.SetIgnoringObjectDisposed();
19791976
});
19801977

19811978
SendRequest(request);
@@ -2013,7 +2010,7 @@ public void RequestPosixRename(string oldPath, string newPath)
20132010
response =>
20142011
{
20152012
exception = GetSftpException(response);
2016-
_ = wait.Set();
2013+
wait.SetIgnoringObjectDisposed();
20172014
});
20182015

20192016
if (!_supportedExtensions.ContainsKey(request.Name))
@@ -2060,12 +2057,12 @@ public SftpFileSystemInformation RequestStatVfs(string path, bool nullOnError =
20602057
response =>
20612058
{
20622059
information = response.GetReply<StatVfsReplyInfo>().Information;
2063-
_ = wait.Set();
2060+
wait.SetIgnoringObjectDisposed();
20642061
},
20652062
response =>
20662063
{
20672064
exception = GetSftpException(response);
2068-
_ = wait.Set();
2065+
wait.SetIgnoringObjectDisposed();
20692066
});
20702067

20712068
if (!_supportedExtensions.ContainsKey(request.Name))
@@ -2148,12 +2145,12 @@ internal SftpFileSystemInformation RequestFStatVfs(byte[] handle, bool nullOnErr
21482145
response =>
21492146
{
21502147
information = response.GetReply<StatVfsReplyInfo>().Information;
2151-
_ = wait.Set();
2148+
wait.SetIgnoringObjectDisposed();
21522149
},
21532150
response =>
21542151
{
21552152
exception = GetSftpException(response);
2156-
_ = wait.Set();
2153+
wait.SetIgnoringObjectDisposed();
21572154
});
21582155

21592156
if (!_supportedExtensions.ContainsKey(request.Name))
@@ -2197,7 +2194,7 @@ internal void HardLink(string oldPath, string newPath)
21972194
response =>
21982195
{
21992196
exception = GetSftpException(response);
2200-
_ = wait.Set();
2197+
wait.SetIgnoringObjectDisposed();
22012198
});
22022199

22032200
if (!_supportedExtensions.ContainsKey(request.Name))

0 commit comments

Comments
 (0)