I have a thread doing a read
syscall, and I need to interrupt it without terminating the entire process. I have tried registering a dummy handler for SIGINT using sigaction()
(it returns 0, and ^C runs my handler), and then calling pthread_kill
to send SIGINT specifically to the thread in question (it captures pthread_self
prior to the read
call), but the signal handler doesn’t run, and the process appears to be violently terminated, and also something strange happens to the terminal; it stops echoing my input, and I have to exit the sudo
session and restart it.
Complicating matters is that I am writing code in C#. I’m pretty sure I have the type correct; the marshaller indicates that it has the same size as struct sigaction
in C code, and my custom handler is in fact run when I send ^C interactively.
Is there a different way to set signal handlers for threads/for pthread_kill
? Not sure what to try next.
Code:
- Definitions:
public delegate void SignalHandler(int signal);
[StructLayout(LayoutKind.Sequential)]
public struct sigaction_t
{
public SignalHandler? sa_handler;
public long sa_mask_0; // C# does not support having arrays be inlined in structures
public long sa_mask_1;
public long sa_mask_2;
public long sa_mask_3;
public long sa_mask_4;
public long sa_mask_5;
public long sa_mask_6;
public long sa_mask_7;
public long sa_mask_8;
public long sa_mask_9;
public long sa_mask_10;
public long sa_mask_11;
public long sa_mask_12;
public long sa_mask_13;
public long sa_mask_14;
public long sa_mask_15;
public int sa_flags;
public Action? sa_restorer;
}
[DllImport("c")]
public static extern int sigaction(int signum, ref sigaction_t act, IntPtr oldact);
- Registering handler for SIGINT:
NativeMethods.sigaction_t int_handler = new();
int_handler.sa_handler = (_) => { Console.WriteLine("Handler for SIGINT"); };
int result = NativeMethods.sigaction(NativeMethods.SIGINT, ref int_handler, IntPtr.Zero);
if (result != 0)
throw new Exception("sigaction failed");
- Thread that needs to be interruptible:
var monitorThread = NativeMethods.pthread_self();
cancellationToken.Register(
() =>
{
Console.WriteLine("Calling pthread_kill");
NativeMethods.pthread_kill(monitorThread, NativeMethods.SIGINT);
Console.WriteLine("pthread_kill finished");
});
...
while (!cancellationToken.IsCancellationRequested)
{
int readSize = NativeMethods.read(_fd, buffer, BufferSize);
...
In the cancellation handler, Calling pthread_kill
appears on stdout but pthread_kill finished
does not. No code after the call to read
appears to be executed.
Hmm, one thought… Are the threads created by the .NET runtime actually pthreads
threads? ???? Maybe pthreads
sees the entire process as a single thread because it’s not being used by the runtime to manage threads?? Is that possible??