csharp-rest-api icon indicating copy to clipboard operation
csharp-rest-api copied to clipboard

Resources not always disposed

Open jeroenheijmans opened this issue 5 years ago • 0 comments

In the RestClient.cs there is the following code:

https://github.com/messagebird/csharp-rest-api/blob/c6e2199c00420335c1fc5ac40df36364cae9ec90/MessageBird/Net/RestClient.cs#L203-L206

Looking at the source for StreamWriter in .NET Core latest (should be similar in other runtimes) this only closes but does not dispose the inner stream.

It might be prudent to change the start of the code to:

using (var stream = request.GetRequestStream())
using (var requestWriter = new StreamWriter(stream))
{
    requestWriter.Write(body);
}

The specific reason I'm asking for this, is that we're seeing ErrorExceptions with inner SocketExceptions claiming AddressAlreadyInUse (10048) errors. This happens after we send a few thousand SMSs, and seems to indicate that sockets on the host are being consumed faster than they become available. So maybe if we don't wait for the GC to collect the .GetRequestStream(), and instead dispose via a using as I suggest, things might improve.

jeroenheijmans avatar Mar 25 '20 17:03 jeroenheijmans