Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore interrupts and wait for child process to exit #119

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

AdamSLevy
Copy link

Because signals were not handled, godotenv will sometimes exit before its child process, causing unexpected behavior in some apps. Instead ignore interrupts and just wait for the subprocess to exit.

fix #21

cmd/godotenv/cmd.go Outdated Show resolved Hide resolved
@bhechinger
Copy link

I've been using Adam's patched version for a while now and it works great. Can we get this merged please @joho?

Thanks!

hoshsadiq added a commit to hoshsadiq/godotenv that referenced this pull request Nov 23, 2021
Using exec is better in this case as it makes the process tree simpler,
and allows the child's signals to be handled by the caller. In addition
the `Exec` function has been removed as it's not really great as a
library function. Users should manually call the `Load` function and
run `exec.Command` or `syscall.Exec` appropriately.

In the future we probably want to handle signals for Windows anyway.

This is based on joho/godotenv#77 and also
addresses the following PRs in upstream:

- joho/godotenv#119
@scalp42
Copy link

scalp42 commented Aug 24, 2022

@joho any chance to see this merged?

@evandam
Copy link

evandam commented Aug 24, 2022

👍 It would be great to have this fix released!

Alternatively, I was looking at somewhat similar tools like aws-env and it uses syscall.Exec which replaces the process rather than spawning a child process, leaving nothing for godotenv to handle after being called if it helps.

@evandam
Copy link

evandam commented Aug 25, 2022

Hey @AdamSLevy I was wondering in the meantime would you be able to rebase your fork to be in sync with the latest release here?

I'd also love to switch to your version but not as familiar with how to install/build it - would you be able to share how we can get up and running?

Thanks in advance!

@c9s
Copy link

c9s commented Apr 7, 2023

I also have this annoying signal issue, would like to see this PR getting merged! Thanks!

@c9s
Copy link

c9s commented Apr 7, 2023

Looks like node's dotenv-cli has the same issue, Only ruby's dotenv is working fine, I will switch to the ruby version for now.

@c9s
Copy link

c9s commented Apr 7, 2023

Here is the simple test code, if you want to reproduce this:

package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"
	"syscall"
	"time"
)

func main() {
	done := make(chan struct{})
	ctx, cancel := context.WithCancel(context.Background())

	go func() {
		ticker := time.NewTicker(time.Second)

		defer close(done)
		for {
			select {
			case <-ctx.Done():
				fmt.Println("ticker shutting down")
				for i := 0; i < 10; i++ {
					time.Sleep(1 * time.Second)
					fmt.Println(i)
				}
				fmt.Println("ticker shut down")
				return

			case t := <-ticker.C:
				fmt.Println(t)
			}
		}
	}()

	sig := make(chan os.Signal, 1)
	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
	fmt.Println("blocking, press ctrl+c to continue...")
	s := <-sig // Will block here until user hits ctrl+c
	fmt.Println("sig received", s)
	cancel()

	fmt.Println("waiting for done")
	<-done
	fmt.Println("done")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

signal passing
5 participants