SciSharp/Numpy.NET

SIGTERM issue with BackgroundService

Closed this issue · 4 comments

Hi,
I am encountering a problem with Numpy.NET used inside a BackgroundService. I am using .NET 5.0.

Basically, my background service is handling the CancellationToken that will be set to a cancel state when SIGTERM is sent to the program (e.g. CTRL+C is pressed on the console).

Here is a very minimal skeleton of the application:

static async Task Main(string[] args)
{
	var builder = CreateHostBuilder();
	await builder.RunConsoleAsync();		
}

private static IHostBuilder CreateHostBuilder()
{
	return new HostBuilder()
		.ConfigureServices(services =>
		{
			services.AddHostedService<MyService>();
		});
}

public class MyService : BackgroundService
{	
	protected override async Task ExecuteAsync(CancellationToken stoppingToken)
	{
		while (!stoppingToken.IsCancellationRequested)
		{
			np.arange(1); // Removing this line allows to handle SIGTERM as expected
			Console.WriteLine("hello");
			await Task.Delay(TimeSpan.FromMilliseconds(300));
		}
	}
}

When I press CTRL+C, the cancellation token is not cancelled at all! I think that Numpy is handling SIGTERM instead, and so the application is no longer able to shutdown as expected.
I tried initializing the PythonEngine manually but I think the default initialization is already passing the proper parameters (because the flag initSigs is false by default).

Can you please let me know your thoughts?

Many thanks!

Marco

henon commented

I suspect the SIGTERM is handled in pythonnet code, right?

At this point I think so, but I thought that leaving initSigs to false in the initialization of the PythonEngine was enough...

henon commented

I think you should make a repro of the issue with a simple pythonnet program (without using Numpy.NET to prove that the issue is in fact a pure pythonnet problem) and open an issue on the pythonnet repo.

Makes sense, thanks for your reply. I'll close this here.