Skip to content

Commit

Permalink
Fix a race condition when BeginInvoke completes before the IAsyncResu…
Browse files Browse the repository at this point in the history
…lt added to m_asyncmethods

By moving the BeginInvoke inside of the lock it is no longer possible for the End method to be called before the IAsyncResult is added to m_asyncmethods.
  • Loading branch information
Luke Horsley committed May 2, 2018
1 parent 75d895b commit 530e495
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 39 deletions.
7 changes: 3 additions & 4 deletions FluentFTP/Client/FtpClient_Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,8 @@ public IAsyncResult BeginExecute(string command, AsyncCallback callback, object
AsyncExecute func;
IAsyncResult ar;

ar = (func = new AsyncExecute(Execute)).BeginInvoke(command, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncExecute(Execute)).BeginInvoke(command, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -1492,9 +1492,8 @@ public IAsyncResult BeginConnect(AsyncCallback callback, object state) {
AsyncConnect func;
IAsyncResult ar;

ar = (func = new AsyncConnect(Connect)).BeginInvoke(callback, state);

lock (m_asyncmethods) {
ar = (func = new AsyncConnect(Connect)).BeginInvoke(callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -1618,8 +1617,8 @@ public IAsyncResult BeginDisconnect(AsyncCallback callback, object state) {
IAsyncResult ar;
AsyncDisconnect func;

ar = (func = new AsyncDisconnect(Disconnect)).BeginInvoke(callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncDisconnect(Disconnect)).BeginInvoke(callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down
33 changes: 19 additions & 14 deletions FluentFTP/Client/FtpClient_Hash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ public IAsyncResult BeginGetHashAlgorithm(AsyncCallback callback, object state)
AsyncGetHashAlgorithm func;
IAsyncResult ar;

ar = (func = new AsyncGetHashAlgorithm(GetHashAlgorithm)).BeginInvoke(callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncGetHashAlgorithm(GetHashAlgorithm)).BeginInvoke(callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -238,8 +238,8 @@ public IAsyncResult BeginSetHashAlgorithm(FtpHashAlgorithm type, AsyncCallback c
AsyncSetHashAlgorithm func;
IAsyncResult ar;

ar = (func = new AsyncSetHashAlgorithm(SetHashAlgorithm)).BeginInvoke(type, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncSetHashAlgorithm(SetHashAlgorithm)).BeginInvoke(type, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -377,8 +377,8 @@ public IAsyncResult BeginGetHash(string path, AsyncCallback callback, object sta
AsyncGetHash func;
IAsyncResult ar;

ar = (func = new AsyncGetHash(GetHash)).BeginInvoke(path, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncGetHash(GetHash)).BeginInvoke(path, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -550,10 +550,10 @@ public FtpHash GetChecksum(string path) {
public IAsyncResult BeginGetChecksum(string path, AsyncCallback callback,
object state) {
AsyncGetChecksum func = new AsyncGetChecksum(GetChecksum);
IAsyncResult ar = func.BeginInvoke(path, callback, state);
;
IAsyncResult ar;

lock (m_asyncmethods) {
ar = func.BeginInvoke(path, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -687,10 +687,10 @@ public string GetMD5(string path) {
/// <returns>IAsyncResult</returns>
public IAsyncResult BeginGetMD5(string path, AsyncCallback callback, object state) {
AsyncGetMD5 func = new AsyncGetMD5(GetMD5);
IAsyncResult ar = func.BeginInvoke(path, callback, state);
;
IAsyncResult ar;

lock (m_asyncmethods) {
ar = func.BeginInvoke(path, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -775,9 +775,10 @@ public string GetXCRC(string path) {
/// <returns>IAsyncResult</returns>
public IAsyncResult BeginGetXCRC(string path, AsyncCallback callback, object state) {
AsyncGetXCRC func = new AsyncGetXCRC(GetXCRC);
IAsyncResult ar = func.BeginInvoke(path, callback, state); ;

IAsyncResult ar;
lock (m_asyncmethods) {
ar = func.BeginInvoke(path, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -863,9 +864,10 @@ public string GetXMD5(string path) {
/// <returns>IAsyncResult</returns>
public IAsyncResult BeginGetXMD5(string path, AsyncCallback callback, object state) {
AsyncGetXMD5 func = new AsyncGetXMD5(GetXMD5);
IAsyncResult ar = func.BeginInvoke(path, callback, state); ;

IAsyncResult ar;
lock (m_asyncmethods) {
ar = func.BeginInvoke(path, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -944,9 +946,10 @@ public string GetXSHA1(string path) {
/// <returns>IAsyncResult</returns>
public IAsyncResult BeginGetXSHA1(string path, AsyncCallback callback, object state) {
AsyncGetXSHA1 func = new AsyncGetXSHA1(GetXSHA1);
IAsyncResult ar = func.BeginInvoke(path, callback, state); ;
IAsyncResult ar;

lock (m_asyncmethods) {
ar = func.BeginInvoke(path, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -1026,9 +1029,10 @@ public string GetXSHA256(string path) {
/// <returns>IAsyncResult</returns>
public IAsyncResult BeginGetXSHA256(string path, AsyncCallback callback, object state) {
AsyncGetXSHA256 func = new AsyncGetXSHA256(GetXSHA256);
IAsyncResult ar = func.BeginInvoke(path, callback, state); ;
IAsyncResult ar;

lock (m_asyncmethods) {
ar = func.BeginInvoke(path, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -1108,9 +1112,10 @@ public string GetXSHA512(string path) {
/// <returns>IAsyncResult</returns>
public IAsyncResult BeginGetXSHA512(string path, AsyncCallback callback, object state) {
AsyncGetXSHA512 func = new AsyncGetXSHA512(GetXSHA512);
IAsyncResult ar = func.BeginInvoke(path, callback, state); ;
IAsyncResult ar;

lock (m_asyncmethods) {
ar = func.BeginInvoke(path, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down
6 changes: 3 additions & 3 deletions FluentFTP/Client/FtpClient_Listing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ public IAsyncResult BeginGetObjectInfo(string path, bool dateModified, AsyncCall
IAsyncResult ar;
AsyncGetObjectInfo func;

ar = (func = new AsyncGetObjectInfo(GetObjectInfo)).BeginInvoke(path, dateModified, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncGetObjectInfo(GetObjectInfo)).BeginInvoke(path, dateModified, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -614,8 +614,8 @@ public IAsyncResult BeginGetListing(string path, FtpListOption options, AsyncCal
IAsyncResult ar;
AsyncGetListing func;

ar = (func = new AsyncGetListing(GetListing)).BeginInvoke(path, options, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncGetListing(GetListing)).BeginInvoke(path, options, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -987,8 +987,8 @@ public IAsyncResult BeginGetNameListing(string path, AsyncCallback callback, obj
IAsyncResult ar;
AsyncGetNameListing func;

ar = (func = new AsyncGetNameListing(GetNameListing)).BeginInvoke(path, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncGetNameListing(GetNameListing)).BeginInvoke(path, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down
8 changes: 4 additions & 4 deletions FluentFTP/Client/FtpClient_LowLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,8 @@ public IAsyncResult BeginOpenRead(string path, FtpDataType type, long restart, A
AsyncOpenRead func;
IAsyncResult ar;

ar = (func = new AsyncOpenRead(OpenRead)).BeginInvoke(path, type, restart, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncOpenRead(OpenRead)).BeginInvoke(path, type, restart, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -1098,8 +1098,8 @@ public IAsyncResult BeginOpenWrite(string path, FtpDataType type, AsyncCallback
AsyncOpenWrite func;
IAsyncResult ar;

ar = (func = new AsyncOpenWrite(OpenWrite)).BeginInvoke(path, type, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncOpenWrite(OpenWrite)).BeginInvoke(path, type, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -1280,8 +1280,8 @@ public IAsyncResult BeginOpenAppend(string path, FtpDataType type, AsyncCallback
IAsyncResult ar;
AsyncOpenAppend func;

ar = (func = new AsyncOpenAppend(OpenAppend)).BeginInvoke(path, type, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncOpenAppend(OpenAppend)).BeginInvoke(path, type, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -1429,8 +1429,8 @@ protected IAsyncResult BeginSetDataType(FtpDataType type, AsyncCallback callback
IAsyncResult ar;
AsyncSetDataType func;

ar = (func = new AsyncSetDataType(SetDataType)).BeginInvoke(type, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncSetDataType(SetDataType)).BeginInvoke(type, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down
29 changes: 15 additions & 14 deletions FluentFTP/Client/FtpClient_Management.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ public IAsyncResult BeginDeleteFile(string path, AsyncCallback callback, object
IAsyncResult ar;
AsyncDeleteFile func;

ar = (func = new AsyncDeleteFile(DeleteFile)).BeginInvoke(path, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncDeleteFile(DeleteFile)).BeginInvoke(path, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -324,8 +324,8 @@ public IAsyncResult BeginDeleteDirectory(string path, FtpListOption options, Asy
AsyncDeleteDirectory func;
IAsyncResult ar;

ar = (func = new AsyncDeleteDirectory(DeleteDirectory)).BeginInvoke(path, options, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncDeleteDirectory(DeleteDirectory)).BeginInvoke(path, options, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -510,8 +510,8 @@ public IAsyncResult BeginDirectoryExists(string path, AsyncCallback callback, ob
AsyncDirectoryExists func;
IAsyncResult ar;

ar = (func = new AsyncDirectoryExists(DirectoryExists)).BeginInvoke(path, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncDirectoryExists(DirectoryExists)).BeginInvoke(path, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -660,9 +660,10 @@ public bool FileExists(string path) {
/// <example><code source="..\Examples\BeginFileExists.cs" lang="cs" /></example>
public IAsyncResult BeginFileExists(string path, AsyncCallback callback, object state) {
AsyncFileExists func;
IAsyncResult ar;

IAsyncResult ar = (func = new AsyncFileExists(FileExists)).BeginInvoke(path, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncFileExists(FileExists)).BeginInvoke(path, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -833,8 +834,8 @@ public IAsyncResult BeginCreateDirectory(string path, bool force, AsyncCallback
AsyncCreateDirectory func;
IAsyncResult ar;

ar = (func = new AsyncCreateDirectory(CreateDirectory)).BeginInvoke(path, force, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncCreateDirectory(CreateDirectory)).BeginInvoke(path, force, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -955,8 +956,8 @@ public IAsyncResult BeginRename(string path, string dest, AsyncCallback callback
AsyncRename func;
IAsyncResult ar;

ar = (func = new AsyncRename(Rename)).BeginInvoke(path, dest, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncRename(Rename)).BeginInvoke(path, dest, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -1070,8 +1071,8 @@ public IAsyncResult BeginMoveFile(string path, string dest, FtpExists existsMode
AsyncMoveFile func;
IAsyncResult ar;

ar = (func = new AsyncMoveFile(MoveFile)).BeginInvoke(path, dest, existsMode, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncMoveFile(MoveFile)).BeginInvoke(path, dest, existsMode, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -1201,8 +1202,8 @@ public IAsyncResult BeginMoveDirectory(string path, string dest, FtpExists exist
AsyncMoveDirectory func;
IAsyncResult ar;

ar = (func = new AsyncMoveDirectory(MoveDirectory)).BeginInvoke(path, dest, existsMode, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncMoveDirectory(MoveDirectory)).BeginInvoke(path, dest, existsMode, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -1586,8 +1587,8 @@ public IAsyncResult BeginDereferenceLink(FtpListItem item, int recMax, AsyncCall
IAsyncResult ar;
AsyncDereferenceLink func;

ar = (func = new AsyncDereferenceLink(DereferenceLink)).BeginInvoke(item, recMax, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncDereferenceLink(DereferenceLink)).BeginInvoke(item, recMax, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -1734,8 +1735,8 @@ public IAsyncResult BeginSetWorkingDirectory(string path, AsyncCallback callback
IAsyncResult ar;
AsyncSetWorkingDirectory func;

ar = (func = new AsyncSetWorkingDirectory(SetWorkingDirectory)).BeginInvoke(path, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncSetWorkingDirectory(SetWorkingDirectory)).BeginInvoke(path, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -1826,8 +1827,8 @@ public IAsyncResult BeginGetWorkingDirectory(AsyncCallback callback, object stat
IAsyncResult ar;
AsyncGetWorkingDirectory func;

ar = (func = new AsyncGetWorkingDirectory(GetWorkingDirectory)).BeginInvoke(callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncGetWorkingDirectory(GetWorkingDirectory)).BeginInvoke(callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -1939,8 +1940,8 @@ public IAsyncResult BeginGetFileSize(string path, AsyncCallback callback, object
IAsyncResult ar;
AsyncGetFileSize func;

ar = (func = new AsyncGetFileSize(GetFileSize)).BeginInvoke(path, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncGetFileSize(GetFileSize)).BeginInvoke(path, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -2059,8 +2060,8 @@ public IAsyncResult BeginGetModifiedTime(string path, FtpDate type, AsyncCallbac
IAsyncResult ar;
AsyncGetModifiedTime func;

ar = (func = new AsyncGetModifiedTime(GetModifiedTime)).BeginInvoke(path, type, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncGetModifiedTime(GetModifiedTime)).BeginInvoke(path, type, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down Expand Up @@ -2183,8 +2184,8 @@ public IAsyncResult BeginSetModifiedTime(string path, DateTime date, FtpDate typ
IAsyncResult ar;
AsyncSetModifiedTime func;

ar = (func = new AsyncSetModifiedTime(SetModifiedTime)).BeginInvoke(path, date, type, callback, state);
lock (m_asyncmethods) {
ar = (func = new AsyncSetModifiedTime(SetModifiedTime)).BeginInvoke(path, date, type, callback, state);
m_asyncmethods.Add(ar, func);
}

Expand Down

0 comments on commit 530e495

Please sign in to comment.