小编Jea*_*ler的帖子

应用程序未在发布模式下进入 Posix 信号处理程序

我正在尝试在 Linux Arm 设备上正常关闭我的控制台应用程序。发送的信号是SIGTERM我使用新的PosixSignalRegistration.Create()方法实现了 Posix Sgnal Handler 。当在调试模式下编译时,这可以完美地工作。但是,在发布模式(打开优化)下,永远不会进入处理程序。

这是一个 MRE:

using Microsoft.Extensions.Hosting;
using System.Runtime.InteropServices;

ManualResetEvent resetEvent = new(false);
CancellationTokenSource cts = new();

// Handles CTRL + C in both operating systems
Console.CancelKeyPress += (sender, eventArgs) =>
{
    Console.WriteLine("Received CTRL+C");
    resetEvent.Set();
    eventArgs.Cancel = true;
    cts.Cancel();
};

// Handles graceful kill (Sigterm) in linux (e.g. systemctl stop)
PosixSignalRegistration.Create(PosixSignal.SIGTERM, (context) =>
{
    Console.WriteLine("Received SIGTERM");
    resetEvent.Set();
    cts.Cancel();
});

IHost? host = null;

try
{
    var hostBuilder = Host.CreateDefaultBuilder(args);
    host = …
Run Code Online (Sandbox Code Playgroud)

c# compiler-optimization sigterm .net-core .net-6.0

4
推荐指数
1
解决办法
789
查看次数

标签 统计

.net-6.0 ×1

.net-core ×1

c# ×1

compiler-optimization ×1

sigterm ×1