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

You can create a state with DisposeAsync where you get ObjectDisposedErrors #595

Closed
paule96 opened this issue Sep 29, 2022 · 3 comments
Closed
Labels
bug Something isn't working
Milestone

Comments

@paule96
Copy link

paule96 commented Sep 29, 2022

Describe the bug

The DisposeAsync method isn't thread safe and as a user of the library you also don't have any option to write a thread safe wrapper around it.

To Reproduce

see the sample

Expected behavior

The SemaphoreSlim that is used by TestcontainersContainer should be disposed thread safe. Or at least the cosument of an TestcontainersContainer should have bean any sign that the object is disposed.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • Version 2.1.0
  • dotnet 6.0.401

Additional context

Here is a sample implementation to reproduce the bug. It's happen around 1/3 tries:

using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Containers;

const TestcontainersState testcontainersCreatedStates = TestcontainersState.Created | TestcontainersState.Running | TestcontainersState.Exited;
Console.WriteLine("Hello, World!");
var config = new MsSqlTestcontainerConfiguration()
{
    Password = Guid.NewGuid().ToString()
};
var myContainer = new TestcontainersBuilder<MsSqlTestcontainer>()
                    .WithDatabase(config);
var dbTestContainer = myContainer.Build();
await dbTestContainer.StartAsync();
await Task.WhenAll(stopIt(dbTestContainer), stopIt(dbTestContainer));

async Task stopIt(MsSqlTestcontainer dbTestContainer)
{
    try
    {
        if (dbTestContainer.State != testcontainersCreatedStates)
        {
            await dbTestContainer.DisposeAsync();
        }

    }
    catch (ObjectDisposedException ex)
    {
        Console.WriteLine($"lol error: {ex.Message}");
    }
}
@HofmeisterAn
Copy link
Collaborator

Thanks for reporting the issue.

@paule96
Copy link
Author

paule96 commented Sep 30, 2022

@HofmeisterAn thanks for the fast fix. :)

@paule96
Copy link
Author

paule96 commented Sep 30, 2022

btw. you solutions is nice. I was thinking twice about the code. But I learned that this is a pretty nice way :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants