Skip to content

Commit e3280ab

Browse files
authored
fix memory leak #1506 (#1543)
1 parent da30579 commit e3280ab

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/RestSharp/Http.Async.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,29 @@ long CalculateContentLength()
173173
void SetTimeout(IAsyncResult asyncResult)
174174
{
175175
if (Timeout != 0)
176-
ThreadPool.RegisterWaitForSingleObject(
176+
_timeoutState.Handle = ThreadPool.RegisterWaitForSingleObject(
177177
asyncResult.AsyncWaitHandle,
178178
TimeoutCallback, _timeoutState, Timeout, true
179179
);
180180

181181
static void TimeoutCallback(object state, bool timedOut)
182182
{
183-
if (!timedOut)
184-
return;
185-
186183
if (!(state is TimeOutState tos))
187184
return;
188185

189-
lock (tos) tos.TimedOut = true;
186+
lock(tos)
187+
{
188+
if(!timedOut)
189+
{
190+
if(tos.Handle != null)
191+
{
192+
tos.Handle.Unregister(null);
193+
}
194+
return;
195+
}
196+
197+
tos.TimedOut = true;
198+
}
190199

191200
tos.Request?.Abort();
192201
}
@@ -286,6 +295,8 @@ class TimeOutState
286295
public bool TimedOut { get; set; }
287296

288297
public HttpWebRequest? Request { get; set; }
298+
299+
public RegisteredWaitHandle? Handle { get; set; }
289300
}
290301
}
291302
}

0 commit comments

Comments
 (0)