restsharp/RestSharp — pull request #516 by mehdime (2014-03-16 00:26:08+00:00)
Title: Fixed bug that caused the async RestClient callbacks to never get called...

In the current version of RestSharp, if you use one of the async `Execute` methods of `RestClient` and an exception is thrown from the `RestRequest.OnBeforeDeserialization` event handler, the callback method is never called.

When using the Task-based Execute methods (e.g. `RestClient.ExecuteTaskAsync()`), this results in the TPL Task never completing. It's not a deadlock and it's not a TPL unhandled exception. It's simply caused by that fact that the callback method that sets the Result of the `TaskCompletionSource` is never called when an exception is thrown from `OnBeforeDeserialization`.

This fix includes an integration test that verifies that an exception thrown from OnBeforeDeserialization is now correctly handled.

The problem lies in this method that gets called as the callback for `HttpWebRequest.BeginGetResponse()`

```
private void DeserializeResponse(IRestRequest request, Action, RestRequestAsyncHandle> callback, IRestResponse response, RestRequestAsyncHandle asyncHandle)
{
IRestResponse restResponse = Deserialize(request, response);
callback(restResponse, asyncHandle);
}
```

If `Deserialize()` throws, `callback` never gets called and anyone waiting for this async task to complete will be waiting forever. `Deserialize()` does this:

```
private IRestResponse Deserialize(IRestRequest request, IRestResponse raw)
{
request.OnBeforeDeserialization(raw);

IRestResponse response = new RestResponse();
try
{
// Deserialize...
}
catch (Exception ex)
{
// Handle any deserialization error...
}

return response;
}
```
`OnBeforeDeserialization` throws and it's game over.