Send SIGQUIT when cancelling an execution

When the context passed to Nomad Allocation Exec is cancelled, the
process is not terminated. Instead, just the WebSocket connection is
closed. In order to terminate long-running processes, a special
character is injected into the standard input stream. This character is
parsed by the tty line discipline (tty has to be true). The line
discipline sends a SIGQUIT signal to the process, terminating it and
producing a core dump (in a file called 'core'). The SIGQUIT signal can
be caught but isn't by default, which is why the runner is destroyed if
the program does not terminate during a grace period after the signal
was sent.
This commit is contained in:
Konrad Hanff
2021-07-21 09:12:44 +02:00
parent 91537a7364
commit 8d24bda61a
10 changed files with 237 additions and 63 deletions

24
pkg/nullio/nullio.go Normal file
View File

@ -0,0 +1,24 @@
package nullio
import (
"fmt"
"io"
)
// Reader is a struct that implements the io.Reader interface. Read does not return when called.
type Reader struct{}
func (r Reader) Read(_ []byte) (int, error) {
// An empty select blocks forever.
select {}
}
// ReadWriter implements io.ReadWriter and does nothing on Read an Write.
type ReadWriter struct {
Reader
}
func (nrw *ReadWriter) Write(p []byte) (int, error) {
n, err := io.Discard.Write(p)
return n, fmt.Errorf("error writing to io.Discard: %w", err)
}

31
pkg/nullio/nullio_test.go Normal file
View File

@ -0,0 +1,31 @@
package nullio
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
"time"
)
const shortTimeout = 100 * time.Millisecond
func TestReaderDoesNotReturnImmediately(t *testing.T) {
reader := &Reader{}
readerReturned := make(chan bool)
go func() {
p := make([]byte, 0, 5)
_, err := reader.Read(p)
require.NoError(t, err)
close(readerReturned)
}()
var received bool
select {
case <-readerReturned:
received = true
case <-time.After(shortTimeout):
received = false
}
assert.False(t, received)
}