Skip to content

SystemEvents deadlock if UI thread shuts down early #37351

Closed
@weltkante

Description

@weltkante

Description

The first thread that accesses SystemEvents is used for initialization. If its a MTA thread then a private STA thread with message loop is created for listening to system events, but if the first thread is an STA thread the initialization happens on this thread.

The code is not prepared for an STA thread that stops pumping messages or shuts down before process exit.

  • WinForms is running UI tests on multiple threads. Initialization of SystemEvents can happen directly in a test or indirectly by usage of WinForms API. If the first usage happens on a UI test (STA) then SystemEvents will bind to that thread. If this is a test thread which gets terminated earily this can lead to deadlocks in the CI framework (Flaky test: ProfessionalColorTable_ChangeUserPreferences_GetColor_ReturnsExpected deadlock winforms#3254)
  • Some applications are using a separate UI thread for the initial splash screen during application startup. This UI thread is likely to terminate early, if SystemEvents were initialized on that thread they would stop working.
  • Some applications use multiple UI threads to implement responsive progress/notification dialogs when the main application thread is busy and work cannot be easily moved off-thread. If such a dialog shows up early (e.g. progress dialog for some update during application startup) SystemEvents may be initialized on a secondary thread that could be terminated early.
  • There apparently have been Watson reports indicating deadlocking STA threads and/or threads that stop pumping messages.

Regression?

No

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions