Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Recursion causing stack overflow when disposing client/service #588

Merged
merged 5 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Next Release

- Corrects all API documentation link references to point to their new locations
- Fix recursion during disposal causing stack overflow

## v6.7.2 (2024-08-16)

Expand Down
9 changes: 9 additions & 0 deletions EasyPost.Tests/ClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ public void TestThreadSafety()

private const string FakeApikey = "fake_api_key";

[Fact]
public void TestClientDisposal()
{
Client client = new(new ClientConfiguration(FakeApikey));
client.Dispose();

// As long as this test doesn't throw an exception, it passes
nwithan8 marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
public void TestBaseUrlOverride()
{
Expand Down
10 changes: 10 additions & 0 deletions EasyPost.Tests/HttpTests/ClientConfigurationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ public class ClientConfigurationTest
{
#region Tests

[Fact]
public void TestClientConfigurationDisposal()
{
ClientConfiguration configuration = new("not_a_real_api_key");

configuration.Dispose();

// As long as this test doesn't throw an exception, it passes
}

[Fact]
[Testing.Function]
public void TestClientConfiguration()
Expand Down
23 changes: 23 additions & 0 deletions EasyPost.Tests/HttpTests/RequestTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using EasyPost._base;
using EasyPost.Http;
using Xunit;

namespace EasyPost.Tests.HttpTests;

public class RequestTest
{
#region Tests

[Fact]
public void TestRequestDisposal()
{
Request request = new("https://google.com", "not_a_real_endpoint", Method.Get, ApiVersion.V2, new Dictionary<string, object> { { "key", "value" } }, new Dictionary<string, string> { { "header_key", "header_value" } });

request.Dispose();

// As long as this test doesn't throw an exception, it passes
}

#endregion
}
12 changes: 12 additions & 0 deletions EasyPost.Tests/ServicesTests/ServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using EasyPost.Exceptions.General;
using EasyPost.Http;
using EasyPost.Models.API;
using EasyPost.Services;
using EasyPost.Tests._Utilities;
using EasyPost.Tests._Utilities.Attributes;
using EasyPost.Utilities.Internal.Attributes;
Expand All @@ -21,6 +22,17 @@ public ServiceTests() : base("base_service")
{
}

[Fact]
public void TestServiceDisposal()
{
Client client = new(new ClientConfiguration("not_a_real_api_key"));

AddressService addressService = client.Address;
addressService.Dispose();

// As long as this test doesn't throw an exception, it passes
}

/// <summary>
/// This test confirms that the GetNextPage method works as expected.
/// This method is implemented on a per-service, but rather than testing in each service, we'll test it once here.
Expand Down
76 changes: 36 additions & 40 deletions EasyPost/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,46 +190,42 @@ public Client(ClientConfiguration configuration)
/// <inheritdoc cref="EasyPostClient.Dispose(bool)"/>
protected override void Dispose(bool disposing)
{
// ref: https://dzone.com/articles/when-and-how-to-use-dispose-and-finalize-in-c
// ref: https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1063#pseudo-code-example
if (disposing)
{
// Dispose managed state (managed objects).
// "disposing" inherently true when called from Dispose(), so don't need to pass it in.

// Dispose of the services
Address.Dispose();
ApiKey.Dispose();
Batch.Dispose();
Billing.Dispose();
CarrierAccount.Dispose();
CarrierMetadata.Dispose();
CarrierType.Dispose();
Claim.Dispose();
CustomsInfo.Dispose();
CustomsItem.Dispose();
EndShipper.Dispose();
Event.Dispose();
Insurance.Dispose();
Order.Dispose();
Parcel.Dispose();
Pickup.Dispose();
Rate.Dispose();
ReferralCustomer.Dispose();
Refund.Dispose();
Report.Dispose();
ScanForm.Dispose();
Shipment.Dispose();
SmartRate.Dispose();
Tracker.Dispose();
User.Dispose();
Webhook.Dispose();

// Dispose of the Beta client
Beta.Dispose();
}

// Free native resources (unmanaged objects) and override a finalizer below.
if (!disposing) return;

// Dispose managed state (managed objects)

// "disposing" inherently true when called from Dispose(), so don't need to pass it in.

// Attempt to dispose of the services (some may already be disposed)
Address.Dispose();
ApiKey.Dispose();
Batch.Dispose();
Billing.Dispose();
CarrierAccount.Dispose();
CarrierMetadata.Dispose();
CarrierType.Dispose();
Claim.Dispose();
CustomsInfo.Dispose();
CustomsItem.Dispose();
EndShipper.Dispose();
Event.Dispose();
Insurance.Dispose();
Order.Dispose();
Parcel.Dispose();
Pickup.Dispose();
Rate.Dispose();
ReferralCustomer.Dispose();
Refund.Dispose();
Report.Dispose();
ScanForm.Dispose();
Shipment.Dispose();
SmartRate.Dispose();
Tracker.Dispose();
User.Dispose();
Webhook.Dispose();

// Attempt to dispose of the Beta client (may already be disposed)
Beta.Dispose();

// Dispose of the base client
base.Dispose(disposing);
Expand Down
19 changes: 8 additions & 11 deletions EasyPost/ClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,18 @@ public void Dispose()
/// <inheritdoc cref="EasyPostClient.Dispose(bool)"/>
protected virtual void Dispose(bool disposing)
{
if (_isDisposed) return;
if (!disposing || _isDisposed) return;

if (disposing)
{
// Dispose managed state (managed objects).
// Set the disposed flag to true before disposing of the object to avoid infinite loops
_isDisposed = true;

// dispose of the prepared HTTP client
PreparedHttpClient?.Dispose();
// Dispose managed state (managed objects)

// dispose of the user-provided HTTP client
CustomHttpClient?.Dispose();
}
// Attempt to dispose of the prepared HTTP client (may already be disposed)
PreparedHttpClient?.Dispose();

// Free native resources (unmanaged objects) and override a finalizer below.
_isDisposed = true;
// Attempt to dispose of the user-provided HTTP client (may already be disposed)
CustomHttpClient?.Dispose();
}

/// <summary>
Expand Down
16 changes: 7 additions & 9 deletions EasyPost/Http/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,15 @@ public void Dispose()
/// <inheritdoc cref="EasyPostClient.Dispose(bool)"/>
protected virtual void Dispose(bool disposing)
{
if (_isDisposed) return;
if (disposing)
{
// Dispose managed state (managed objects).

// Dispose the request message
_requestMessage.Dispose();
}
if (!disposing || _isDisposed) return;

// Free native resources (unmanaged objects) and override a finalizer below.
// Set the disposed flag to true before disposing of the object to avoid infinite loops
_isDisposed = true;

// Dispose managed state (managed objects)

// Dispose the request message
_requestMessage.Dispose();
}

/// <summary>
Expand Down
16 changes: 7 additions & 9 deletions EasyPost/_base/EasyPostClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,15 @@ public void Dispose()
/// <param name="disposing">Whether this object is being disposed.</param>
protected virtual void Dispose(bool disposing)
{
if (_isDisposed) return;
if (disposing)
{
// Dispose managed state (managed objects).

// Dispose the configuration
_configuration.Dispose();
}
if (!disposing || _isDisposed) return;

// Free native resources (unmanaged objects) and override a finalizer below.
// Set the disposed flag to true before disposing of the object to avoid infinite loops
_isDisposed = true;

// Dispose managed state (managed objects)

// Dispose of the configuration
_configuration.Dispose();
}

/// <summary>
Expand Down
16 changes: 7 additions & 9 deletions EasyPost/_base/EasyPostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,15 @@ public void Dispose()
/// <inheritdoc cref="EasyPostClient.Dispose(bool)"/>
protected virtual void Dispose(bool disposing)
{
if (_isDisposed) return;
if (disposing)
{
// Dispose managed state (managed objects).
if (!disposing || _isDisposed) return;

// Dispose the client
Client.Dispose();
}

// Free native resources (unmanaged objects) and override a finalizer below.
// Set the disposed flag to true before disposing of the object to avoid infinite loops
_isDisposed = true;

// Dispose managed state (managed objects)

// Don't dispose of the associated client here, as it may be shared among multiple services
// Client.Dispose();
nwithan8 marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
Expand Down
Loading