Skip to content

Commit c7fb43d

Browse files
committed
fix: access a disposed object in SFTP.
chore!: remove all references of Micro soft App Center <body>
1 parent 086f3fe commit c7fb43d

19 files changed

+127
-68
lines changed

Ui/App.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static void Main(string[] args)
4040
}
4141
catch (Exception e)
4242
{
43-
MsAppCenterHelper.Error(e);
43+
SentryIoHelper.Error(e);
4444
}
4545
#if DEV
4646
string kind = aea?.Kind.ToString() ?? "null";

Ui/Model/GlobalData.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ public Result DeleteServer(IEnumerable<ProtocolBase> protocolServers)
379379
}
380380
catch (Exception e)
381381
{
382-
MsAppCenterHelper.Error(e);
382+
SentryIoHelper.Error(e);
383383
throw;
384384
}
385385
finally
@@ -564,7 +564,7 @@ private void TimerOnElapsed()
564564
}
565565
catch (Exception ex)
566566
{
567-
MsAppCenterHelper.Error(ex);
567+
SentryIoHelper.Error(ex);
568568
throw;
569569
}
570570
finally

Ui/Model/Protocol/FileTransmit/Transmitters/TransmissionController/TransmitTask.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ private void DataTransmitting(ref TransmitItem item, ulong readLength)
564564
}
565565
catch (Exception e)
566566
{
567-
MsAppCenterHelper.Error(e, new Dictionary<string, string>()
567+
SentryIoHelper.Error(e, new Dictionary<string, string>()
568568
{
569569
{"readLength", readLength.ToString()},
570570
{"item.TransmittedSize", item.TransmittedSize.ToString()},

Ui/Model/Protocol/FileTransmit/Transmitters/TransmitterFtp.cs

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading;
66
using System.Threading.Tasks;
77
using FluentFTP;
8+
using static System.Net.WebRequestMethods;
89

910
namespace _1RM.Model.Protocol.FileTransmit.Transmitters
1011
{
@@ -39,7 +40,12 @@ public async Task Conn()
3940

4041
public bool IsConnected()
4142
{
42-
return _ftp?.IsConnected == true;
43+
bool isConnected = true;
44+
lock (this)
45+
{
46+
isConnected = _ftp?.IsConnected == true;
47+
}
48+
return isConnected;
4349
}
4450

4551
public ITransmitter Clone()
@@ -50,35 +56,42 @@ public ITransmitter Clone()
5056
public async Task<RemoteItem?> Get(string path)
5157
{
5258
await FtpConnection;
53-
if (_ftp == null) return null;
59+
lock (this)
60+
{
61+
if (_ftp == null || _ftp.IsDisposed) return null;
62+
}
5463
return await Exists(path) ? FtpListItem2RemoteItem(await _ftp.GetObjectInfo(path)) : null;
5564
}
5665

5766
public async Task<List<RemoteItem>> ListDirectoryItems(string path)
5867
{
5968
await FtpConnection;
6069
var ret = new List<RemoteItem>();
61-
if (_ftp != null)
70+
lock (this)
6271
{
63-
IEnumerable<FtpListItem> items = await _ftp.GetListing(path);
64-
if (!items.Any())
65-
return ret;
72+
if (_ftp == null || _ftp.IsDisposed) return ret;
73+
}
74+
IEnumerable<FtpListItem> items = await _ftp.GetListing(path);
75+
if (!items.Any())
76+
return ret;
6677

67-
items = items.OrderBy(x => x.Name);
68-
foreach (var item in items)
69-
{
70-
if (item.Name == "." || item.Name == "..")
71-
continue;
72-
ret.Add(FtpListItem2RemoteItem(item));
73-
}
78+
items = items.OrderBy(x => x.Name);
79+
foreach (var item in items)
80+
{
81+
if (item.Name == "." || item.Name == "..")
82+
continue;
83+
ret.Add(FtpListItem2RemoteItem(item));
7484
}
7585
return ret;
7686
}
7787

7888
public async Task<bool> Exists(string path)
7989
{
8090
await FtpConnection;
81-
if (_ftp == null) return false;
91+
lock (this)
92+
{
93+
if (_ftp == null || _ftp.IsDisposed) return false;
94+
}
8295
if (await _ftp.FileExists(path))
8396
return true;
8497
if (await _ftp.DirectoryExists(path))
@@ -129,7 +142,10 @@ private RemoteItem FtpListItem2RemoteItem(FtpListItem item)
129142
public async Task Delete(string path)
130143
{
131144
await FtpConnection;
132-
if (_ftp == null) return;
145+
lock (this)
146+
{
147+
if (_ftp == null || _ftp.IsDisposed) return;
148+
}
133149
var item = await Get(path);
134150
if (item != null)
135151
{
@@ -152,14 +168,21 @@ public async Task Delete(RemoteItem item)
152168
public async Task CreateDirectory(string path)
153169
{
154170
await FtpConnection;
155-
if (_ftp == null) return;
171+
lock (this)
172+
{
173+
if (_ftp == null || _ftp.IsDisposed) return;
174+
}
156175
if (await _ftp.DirectoryExists(path) == false)
157176
await _ftp.CreateDirectory(path);
158177
}
159178

160179
public async Task RenameFile(string path, string newPath)
161180
{
162181
await FtpConnection;
182+
lock (this)
183+
{
184+
if (_ftp == null || _ftp.IsDisposed) return;
185+
}
163186
if (_ftp != null && path != newPath && await Exists(path))
164187
await _ftp.Rename(path, newPath);
165188
}
@@ -171,7 +194,10 @@ public async Task UploadFile(string localFilePath, string saveToRemotePath, Acti
171194
return;
172195

173196
await FtpConnection;
174-
if (_ftp == null) return;
197+
lock (this)
198+
{
199+
if (_ftp == null || _ftp.IsDisposed) return;
200+
}
175201
await FtpSemaphoe.WaitAsync().ConfigureAwait(false);
176202
try
177203
{
@@ -204,7 +230,10 @@ await _ftp.UploadFile(localFilePath, saveToRemotePath, FtpRemoteExists.Overwrite
204230
public async Task DownloadFile(string remoteFilePath, string saveToLocalPath, Action<ulong> readCallBack, CancellationToken cancellationToken)
205231
{
206232
await FtpConnection;
207-
if (_ftp == null) return;
233+
lock (this)
234+
{
235+
if (_ftp == null || _ftp.IsDisposed) return;
236+
}
208237
await FtpSemaphoe.WaitAsync().ConfigureAwait(false);
209238
try
210239
{
@@ -227,23 +256,33 @@ public void Release()
227256
{
228257
FtpSemaphoe?.Dispose();
229258
FtpConnection?.Dispose();
230-
var ftp = _ftp;
231-
ftp?.Disconnect();
232-
ftp?.Dispose();
233-
_ftp = null;
259+
lock (this)
260+
{
261+
_ftp?.Disconnect();
262+
}
263+
ReleaseFtp();
264+
}
265+
266+
private void ReleaseFtp()
267+
{
268+
lock (this)
269+
{
270+
_ftp?.Dispose();
271+
_ftp = null;
272+
}
234273
}
235274

236275
private async Task InitClient()
237276
{
238-
_ftp?.Dispose();
277+
await _ftp?.Disconnect();
278+
ReleaseFtp();
239279
_ftp = new AsyncFtpClient(Hostname, new System.Net.NetworkCredential(Username, Password), Port);
240280
_ftp.Config.Noop = true;
241281
await _ftp.AutoConnect();
242282
if (!_ftp.IsConnected)
243283
{
244284
await _ftp.Disconnect();
245-
_ftp.Dispose();
246-
_ftp = null;
285+
ReleaseFtp();
247286
throw new Exception("Couldn't connect to the server.");
248287
}
249288
}

Ui/Model/Protocol/FileTransmit/Transmitters/TransmitterSFtp.cs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ public async Task Conn()
5252

5353
public bool IsConnected()
5454
{
55-
return _sftp?.IsConnected == true;
55+
bool isConnected = true;
56+
lock (this)
57+
{
58+
isConnected = _sftp?.IsConnected == true;
59+
}
60+
return isConnected;
5661
}
5762

5863
public ITransmitter Clone()
@@ -95,7 +100,12 @@ public async Task<List<RemoteItem>> ListDirectoryItems(string path)
95100
public async Task<bool> Exists(string path)
96101
{
97102
await SFtpConnection;
98-
return _sftp?.Exists(path) == true;
103+
bool ret = false;
104+
lock (this)
105+
{
106+
ret = _sftp?.Exists(path) == true;
107+
}
108+
return ret;
99109
}
100110

101111
private RemoteItem SftpFile2RemoteItem(ISftpFile item)
@@ -258,22 +268,32 @@ public async Task DownloadFile(string remoteFilePath, string saveToLocalPath, Ac
258268

259269
public void Release()
260270
{
261-
SFtpConnection?.Dispose();
262-
var sftp = _sftp;
263-
sftp?.Disconnect();
264-
sftp?.Dispose();
265-
_sftp = null;
271+
if (SFtpConnection?.IsCompleted == true)
272+
{
273+
SFtpConnection?.Dispose();
274+
}
275+
ReleaseSftp();
276+
}
277+
278+
private void ReleaseSftp()
279+
{
280+
lock (this)
281+
{
282+
_sftp?.Disconnect();
283+
_sftp?.Dispose();
284+
_sftp = null;
285+
}
266286
}
267287

268288
private async Task InitClient()
269289
{
270290
await Task.Run(() =>
271291
{
272-
if (_sftp?.IsConnected != true)
292+
if (IsConnected() != true)
273293
{
274294
RetryHelper.Try(() =>
275295
{
276-
_sftp?.Dispose();
296+
ReleaseSftp();
277297
if (string.IsNullOrEmpty(Password)
278298
&& string.IsNullOrEmpty(SshKeyPath) == false
279299
&& File.Exists(SshKeyPath))
@@ -285,7 +305,7 @@ await Task.Run(() =>
285305
}
286306
catch (Exception e)
287307
{
288-
MsAppCenterHelper.Error(e);
308+
SentryIoHelper.Error(e);
289309
}
290310
}
291311
_sftp ??= new SftpClient(new ConnectionInfo(Hostname, Port, Username, new PasswordAuthenticationMethod(Username, Password)));

Ui/Service/ConfigurationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ public void Save()
322322
RetryHelper.Try(() =>
323323
{
324324
File.WriteAllText(AppPathHelper.Instance.ProfileJsonPath, JsonConvert.SerializeObject(this._cfg, Formatting.Indented), Encoding.UTF8);
325-
}, actionOnError: exception => MsAppCenterHelper.Error(exception));
325+
}, actionOnError: exception => SentryIoHelper.Error(exception));
326326
}
327327

328328
DataSourceService.AdditionalSourcesSaveToProfile(AppPathHelper.Instance.ProfileAdditionalDataSourceJsonPath, AdditionalDataSource);

Ui/Service/DataSource/DataSourceService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public DatabaseStatus AddOrUpdateDataSource(DataSourceBase config, int connectTi
126126
catch (Exception e)
127127
{
128128
SimpleLogHelper.Warning(e);
129-
MsAppCenterHelper.Error(e);
129+
SentryIoHelper.Error(e);
130130
var ret = DatabaseStatus.New(EnumDatabaseStatus.OtherError, e.Message);
131131
return ret;
132132
}
@@ -183,7 +183,7 @@ public static void AdditionalSourcesSaveToProfile(string path, List<DataSourceBa
183183
RetryHelper.Try(() =>
184184
{
185185
File.WriteAllText(path, JsonConvert.SerializeObject(sources, Formatting.Indented), Encoding.UTF8);
186-
}, actionOnError: exception => MsAppCenterHelper.Error(exception));
186+
}, actionOnError: exception => SentryIoHelper.Error(exception));
187187
}
188188
}
189189
finally

Ui/Service/Locality/LocalityConnectRecorder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private static void Save()
6161
CanSave = false;
6262
AppPathHelper.CreateDirIfNotExist(AppPathHelper.Instance.LocalityDirPath, false);
6363
RetryHelper.Try(() => { File.WriteAllText(JsonPath, JsonConvert.SerializeObject(_settings, Formatting.Indented), Encoding.UTF8); },
64-
actionOnError: exception => MsAppCenterHelper.Error(exception));
64+
actionOnError: exception => SentryIoHelper.Error(exception));
6565
CanSave = true;
6666
}
6767
}

Ui/Service/Locality/LocalityListViewService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private static void Save()
6060
CanSave = false;
6161
AppPathHelper.CreateDirIfNotExist(AppPathHelper.Instance.LocalityDirPath, false);
6262
RetryHelper.Try(() => { File.WriteAllText(JsonPath, JsonConvert.SerializeObject(_settings, Formatting.Indented), Encoding.UTF8); },
63-
actionOnError: exception => MsAppCenterHelper.Error(exception));
63+
actionOnError: exception => SentryIoHelper.Error(exception));
6464
CanSave = true;
6565
}
6666
}
@@ -156,7 +156,7 @@ public static void GroupedIsExpandedSet(string dataSourceName, bool isExpanded)
156156
}
157157
catch (Exception e)
158158
{
159-
MsAppCenterHelper.Error(e);
159+
SentryIoHelper.Error(e);
160160
_settings.GroupedIsExpanded = new Dictionary<string, bool>();
161161
}
162162
Save();

Ui/Service/Locality/LocalityService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private void Save()
6464
CanSave = false;
6565
AppPathHelper.CreateDirIfNotExist(AppPathHelper.Instance.LocalityDirPath, false);
6666
RetryHelper.Try(() => { File.WriteAllText(JsonPath, JsonConvert.SerializeObject(this._localitySettings, Formatting.Indented), Encoding.UTF8); },
67-
actionOnError: exception => MsAppCenterHelper.Error(exception));
67+
actionOnError: exception => SentryIoHelper.Error(exception));
6868
CanSave = true;
6969
}
7070
}

0 commit comments

Comments
 (0)