Skip to content

Commit

Permalink
transmit signal only windows (#341)
Browse files Browse the repository at this point in the history
* discard signal on non windows OS
* update README (no more detached behavior since discarding signal on non windows OS)

Signed-off-by: Denis Vaumoron <[email protected]>
  • Loading branch information
dvaumoron authored Jan 28, 2025
1 parent d6e128a commit bde3d44
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 176 deletions.
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -658,15 +658,6 @@ If set to true **tenv** will automatically install missing tool versions needed.
</details>


<details><summary><b>TENV_DETACHED_PROXY</b></summary><br>

String (Default: false on Windows OS, true on other OS without CI or PIPELINE_WORKSPACE environment variable)

Enable **tenv** proxies detached behaviour (use a new process group id when launching tool command, allow to avoid killing proxied tool process earlier than expected when proxies redirect interrupt signal already sended to whole process group (issue occurrences known on macOS and Linux)).

</details>


<details><summary><b>TENV_FORCE_REMOTE</b></summary><br>

String (Default: false)
Expand Down
20 changes: 9 additions & 11 deletions config/envname/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,15 @@ const (
AtmosRemoteURL = AtmosPrefix + remoteURL
AtmosRemoteUser = AtmosPrefix + remoteUser

tenvPrefix = "TENV_"
TenvArch = tenvPrefix + arch
TenvAutoInstall = tenvPrefix + autoInstall
TenvDetachedProxy = tenvPrefix + "DETACHED_PROXY"
TenvDetachedProxyDefault = TenvDetachedProxy + "_DEFAULT"
TenvForceRemote = tenvPrefix + forceRemote
TenvLog = tenvPrefix + log
TenvQuiet = tenvPrefix + quiet
TenvRemoteConf = tenvPrefix + "REMOTE_CONF"
TenvRootPath = tenvPrefix + rootPath
TenvToken = tenvPrefix + token
tenvPrefix = "TENV_"
TenvArch = tenvPrefix + arch
TenvAutoInstall = tenvPrefix + autoInstall
TenvForceRemote = tenvPrefix + forceRemote
TenvLog = tenvPrefix + log
TenvQuiet = tenvPrefix + quiet
TenvRemoteConf = tenvPrefix + "REMOTE_CONF"
TenvRootPath = tenvPrefix + rootPath
TenvToken = tenvPrefix + token

TfenvPrefix = "TFENV_"
tfenvTerraformPrefix = TfenvPrefix + "TERRAFORM_"
Expand Down
12 changes: 0 additions & 12 deletions pkg/cmdproxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,6 @@ func initIO(cmd *exec.Cmd, pExitCode *int, gha bool, getenv configutils.GetenvFu
}
}

func transmitIncreasingSignal(signalReceiver <-chan os.Signal, process *os.Process) {
first := true
for range signalReceiver {
if first {
_ = process.Signal(os.Interrupt)
first = false
} else {
_ = process.Signal(os.Kill)
}
}
}

func writeMultiline(file *os.File, key string, value string) error {
delimiter := "ghadelimeter_" + strconv.Itoa(rand.Int()) //nolint
if strings.Contains(key, delimiter) || strings.Contains(value, delimiter) {
Expand Down
29 changes: 29 additions & 0 deletions pkg/cmdproxy/transmit_others.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build !windows

/*
*
* Copyright 2024 tofuutils authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package cmdproxy

import "os"

func transmitIncreasingSignal(signalReceiver <-chan os.Signal, _ *os.Process) {
for range signalReceiver { //nolint
// discard signals on non Windows OS (already send to whole process group)
}
}
33 changes: 33 additions & 0 deletions pkg/cmdproxy/transmit_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
*
* Copyright 2024 tofuutils authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package cmdproxy

import "os"

func transmitIncreasingSignal(signalReceiver <-chan os.Signal, process *os.Process) {
first := true
for range signalReceiver {
if first {
_ = process.Signal(os.Interrupt)
first = false
} else {
_ = process.Signal(os.Kill)
}
}
}
9 changes: 0 additions & 9 deletions versionmanager/proxy/agnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ import (

"github.com/tofuutils/tenv/v4/config"
"github.com/tofuutils/tenv/v4/config/cmdconst"
"github.com/tofuutils/tenv/v4/config/envname"
cmdproxy "github.com/tofuutils/tenv/v4/pkg/cmdproxy"
"github.com/tofuutils/tenv/v4/versionmanager/builder"
detachproxy "github.com/tofuutils/tenv/v4/versionmanager/proxy/detach"
)

// Always call os.Exit.
Expand Down Expand Up @@ -80,12 +78,5 @@ func ExecAgnostic(conf *config.Config, hclParser *hclparse.Parser, cmdArgs []str

cmd := exec.CommandContext(ctx, execPath, cmdArgs...)

defaultDetach, err := conf.Getenv.Bool(false, envname.TenvDetachedProxyDefault)
if err != nil {
fmt.Println(msgReadDefaultDetachErr, err) //nolint
}

detachproxy.InitBehaviorFromEnv(cmd, conf.Getenv, defaultDetach)

cmdproxy.Run(cmd, conf.GithubActions, conf.Getenv)
}
50 changes: 0 additions & 50 deletions versionmanager/proxy/detach/detached_others.go

This file was deleted.

42 changes: 0 additions & 42 deletions versionmanager/proxy/detach/detached_windows.go

This file was deleted.

9 changes: 0 additions & 9 deletions versionmanager/proxy/light/light.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"os/signal"

"github.com/tofuutils/tenv/v4/config/cmdconst"
detachproxy "github.com/tofuutils/tenv/v4/versionmanager/proxy/detach"
)

func Exec(execName string) {
Expand All @@ -36,8 +35,6 @@ func Exec(execName string) {

// proxy to selected version
cmd := exec.Command(cmdconst.TenvName, cmdArgs...) //nolint
defaultDetach := updateDefaultDetachInCmdEnv(cmd)
detachproxy.InitBehaviorFromEnv(cmd, os.Getenv, defaultDetach)
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
Expand All @@ -63,9 +60,3 @@ func exitWithErrorMsg(execName string, err error) {
fmt.Println("Failure during", execName, "call :", err) //nolint
os.Exit(1)
}

func transmitSignal(signalReceiver <-chan os.Signal, process *os.Process) {
for range signalReceiver {
_ = process.Signal(os.Interrupt)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,10 @@

package lightproxy

import (
"os"
"os/exec"
import "os"

"github.com/tofuutils/tenv/v4/config/envname"
"github.com/tofuutils/tenv/v4/pkg/tty"
)

const changeDefaultDetach = envname.TenvDetachedProxyDefault + "=true"

func updateDefaultDetachInCmdEnv(cmd *exec.Cmd) bool {
if tty.Detect() {
cmd.Env = append(os.Environ(), changeDefaultDetach)

return true
func transmitSignal(signalReceiver <-chan os.Signal, _ *os.Process) {
for range signalReceiver { //nolint
// discard signals on non Windows OS (already send to whole process group)
}

return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

package lightproxy

import "os/exec"
import "os"

func updateDefaultDetachInCmdEnv(_ *exec.Cmd) bool {
return false
func transmitSignal(signalReceiver <-chan os.Signal, process *os.Process) {
for range signalReceiver {
_ = process.Signal(os.Interrupt)
}
}
17 changes: 2 additions & 15 deletions versionmanager/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,13 @@ import (
"github.com/hashicorp/hcl/v2/hclparse"

"github.com/tofuutils/tenv/v4/config"
"github.com/tofuutils/tenv/v4/config/envname"
cmdproxy "github.com/tofuutils/tenv/v4/pkg/cmdproxy"
"github.com/tofuutils/tenv/v4/pkg/cmdproxy"
"github.com/tofuutils/tenv/v4/pkg/loghelper"
"github.com/tofuutils/tenv/v4/versionmanager/builder"
"github.com/tofuutils/tenv/v4/versionmanager/lastuse"
detachproxy "github.com/tofuutils/tenv/v4/versionmanager/proxy/detach"
)

const (
chdirFlagPrefix = "-chdir="

msgReadDefaultDetachErr = "Failed to read " + envname.TenvDetachedProxyDefault + " environment variable, default to false :"
)
const chdirFlagPrefix = "-chdir="

// Always call os.Exit.
func Exec(conf *config.Config, builderFunc builder.Func, hclParser *hclparse.Parser, execName string, cmdArgs []string) {
Expand All @@ -67,13 +61,6 @@ func Exec(conf *config.Config, builderFunc builder.Func, hclParser *hclparse.Par

cmd := exec.CommandContext(ctx, execPath, cmdArgs...)

defaultDetach, err := conf.Getenv.Bool(false, envname.TenvDetachedProxyDefault)
if err != nil {
fmt.Println(msgReadDefaultDetachErr, err) //nolint
}

detachproxy.InitBehaviorFromEnv(cmd, conf.Getenv, defaultDetach)

cmdproxy.Run(cmd, conf.GithubActions, conf.Getenv)
}

Expand Down

0 comments on commit bde3d44

Please sign in to comment.