Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

ServerFactory.Start does not cleanup properly if exception after first engine.CreateServer #102

@smbecker

Description

@smbecker

If multiple addresses are being used in ServerFactory and an exception is thrown in engine.CreateServer, then the proceeding disposables are not cleaned up properly. Consider changing the method implementation to something similar to:

public IDisposable Start(IServerInformation serverInformation, Func<IFeatureCollection, Task> application)
{
    var disposables = new List<IDisposable>();
    var dispose = new Disposable(() =>
    {
        foreach (var disposable in disposables)
        {
            disposable.Dispose();
        }
    });
    var information = (ServerInformation)serverInformation;
    var engine = new KestrelEngine(_libraryManager);
    engine.Start(1);
    try
    {
        foreach (var address in information.Addresses)
        {
            disposables.Add(engine.CreateServer(
                address.Scheme,
                address.Host,
                address.Port,
                async frame =>
                {
                    var request = new ServerRequest(frame);
                    await application.Invoke(request.Features);
                }));
        }
        disposables.Add(engine);
        return dispose;
    }
    catch (Exception)
    {
        dispose.Dispose();
        throw;
    }
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions