Skip to content
Merged
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
21 changes: 16 additions & 5 deletions src/RestSharp/Http.Async.cs
Original file line number Diff line number Diff line change
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; }
}
}
}