Skip to content

In tray mode, SIGTERM and Ctrl+C kill the process without running OnStop or flushing a pending preference save #21

Closed

Description

@matt-edmondson

What's wrong

Only the console path installs signal handlers. ConsoleRunner.cs:54-70 hooks Console.CancelKeyPress and PosixSignalRegistration(SIGTERM). The tray path (TrayAppBuilder.RunTray, TrayAppBuilder.cs:458-500, and TrayApplication.cs:98-178) registers neither.

Avalonia 12.1.3 does not handle these signals either. The assemblies contain no references to PosixSignalRegistration, CancelKeyPress or a Unix signal handler. So a tray run killed by kill, systemctl stop, logout, or Ctrl+C in the launching terminal ends through the runtime's default handler:

  • TrayApplication.Dispose never runs, so the tool's OnStop is skipped. Anything OnStart acquired, such as a sleep inhibitor or a lock, is left for the OS to clean up.
  • FinishAsync never flushes the debounced preference store. A toggle clicked within the debounce window (500 ms by default) is lost.

This contradicts what the library documents:

  • README.md:35: "Ctrl+C and SIGTERM both release through the tool's own stop action"
  • README.md:96: "Ctrl+C and SIGTERM both releasing through OnStop"
  • The OnStop XML doc on TrayAppBuilder, which says it runs on Ctrl+C and SIGTERM "in both the tray and the console mode"

Failure scenario

The repro was a scratch tool whose OnStart/OnStop print, using a valid PNG icon:

  • xvfb-run -a dotnet repro.dll --tray, then kill -TERM <pid>: the output is OnStart #1 followed by Terminated. OnStop is never printed.
  • The same tool run with --no-tray releases through OnStop as documented.

Suggested fix

In RunTray, before StartWithClassicDesktopLifetime:

  1. Register PosixSignalRegistration.Create(PosixSignal.SIGTERM, …) and (PosixSignal.SIGINT, …). Each handler sets context.Cancel = true and posts a lifetime shutdown to Dispatcher.UIThread.
  2. Hook Console.CancelKeyPress, set Cancel = true, and post the same shutdown.
  3. Dispose the registrations and unhook the handler after the run.

Shutting down through the lifetime already runs OnExit, then Dispose, then OnStop, then the FinishAsync flush. That is the same path the Quit menu item takes.

Acceptance criteria

  • A tray run sent SIGTERM calls OnStop exactly once and exits 0.
  • A preference toggled just before the signal is persisted.
  • Ctrl+C in the launching terminal behaves the same way.
  • The xvfb smoke check, or a README verification step, covers this.

Activity

  1. added
    bug
    on Sep 28, 2026
  2. matt-edmondson commented on Sep 28, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    • Category: Bug (behaviour contradicts the README and the OnStop XML docs)
    • Priority: High. Tray mode is the library's main mode. Under kill, systemctl stop or logout, OnStop never runs, so resources that tools take in OnStart (such as sleep inhibitors or locks) are never released, and a preference saved within the debounce window is lost.
    • Area / suggested assignment: TrayAppBuilder.RunTray and TrayApplication, mirroring the signal handling already in ConsoleRunner
    • Duplicates: none
    • In progress: no open PR in TrayApp.

    Suggested next step: register SIGTERM/SIGINT handlers and a CancelKeyPress handler that post a lifetime shutdown to Dispatcher.UIThread. That reuses the Quit path. Extend the xvfb smoke check to send kill -TERM and assert that OnStop ran.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions