Skip to content

Commit

Permalink
fix memory leak #1506 (#1543)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhornbacher committed Dec 9, 2020
1 parent da30579 commit e3280ab
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/RestSharp/Http.Async.cs
Expand Up @@ -173,20 +173,29 @@ long CalculateContentLength()
void SetTimeout(IAsyncResult asyncResult)
{
if (Timeout != 0)
ThreadPool.RegisterWaitForSingleObject(
_timeoutState.Handle = ThreadPool.RegisterWaitForSingleObject(
asyncResult.AsyncWaitHandle,
TimeoutCallback, _timeoutState, Timeout, true
);

static void TimeoutCallback(object state, bool timedOut)
{
if (!timedOut)
return;

if (!(state is TimeOutState tos))
return;

lock (tos) tos.TimedOut = true;
lock(tos)
{
if(!timedOut)
{
if(tos.Handle != null)
{
tos.Handle.Unregister(null);
}
return;
}

tos.TimedOut = true;
}

tos.Request?.Abort();
}
Expand Down Expand Up @@ -286,6 +295,8 @@ class TimeOutState
public bool TimedOut { get; set; }

public HttpWebRequest? Request { get; set; }

public RegisteredWaitHandle? Handle { get; set; }
}
}
}

0 comments on commit e3280ab

Please sign in to comment.