Skip to content

Idea: Use the OS Feature trap to catch SIGINTs when running tests to force cleanup #328

Closed
@mahaffey

Description

@mahaffey

Initially opened here: hashicorp/terraform-aws-vault#153

Recently learned about traps and catching different signals and have been implementing them wherever I can in my Bash scripts.

After reading https://github.com/hashicorp/terraform-aws-vault/blob/master/test/README.md they came into mind in regards to cutting the tests short and the environment not properly cleaning up.

For go usage see: https://stackoverflow.com/questions/11268943/is-it-possible-to-capture-a-ctrlc-signal-and-run-a-cleanup-function-in-a-defe

example trap:

package main

import (
    "fmt"
    "os"
    "os/signal"
    "syscall"
    "time" // or "runtime"
)

func cleanup() {
    fmt.Println("cleanup")
}

func main() {
    c := make(chan os.Signal)
    signal.Notify(c, os.Interrupt, syscall.SIGTERM)
    go func() {
        <-c
        cleanup()
        os.Exit(1)
    }()

    for {
        fmt.Println("sleeping...")
        time.Sleep(10 * time.Second) // or runtime.Gosched() or similar per @misterbee
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions