Skip to content

The Component Dispose is not safe in lock #40771

Open
@lindexi

Description

@lindexi

Description

The System.ComponentModel.Component will lock self when enter Dispose. Maybe there will be dead locks in this approach.

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            lock (this)
            {
                _site?.Container?.Remove(this);
                if (_events != null)
                {
                    ((EventHandler?)_events[s_eventDisposed])?.Invoke(this, EventArgs.Empty);
                }
            }
        }
    }

Other information

The mini demo: https://github.com/lindexi/lindexi_gd/tree/5219a17e96a3577639d0b486c57bc699941c585b/HearwhejiyehallyiheFubaduwheefu

        var component = new Component();
        lock (component)
        {
            Task.Run(() => component.Dispose()).Wait(); // dead lock
        }

Run the code, and then you will find that the thread never exits

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions