startup panic reading environment vars from file #404

Closed
opened 2023-11-16 15:21:42 +00:00 by infinoid · 0 comments
infinoid commented 2023-11-16 15:21:42 +00:00 (Migrated from gitea.com)

I tried to set up an action runner using the docker image and an edited config file. When I started the container, I got a loop of:

level=info msg="Registering runner, arch=amd64, os=linux, version=v0.2.6."
panic: assignment to entry in nil map

goroutine 1 [running]:
gitea.com/gitea/act_runner/internal/pkg/config.LoadDefault({0x7ffdd6570e52, 0x11})
	/opt/src/act_runner/internal/pkg/config/config.go:92 +0x3a8
gitea.com/gitea/act_runner/internal/app/cmd.registerNoInteractive({0xefd4f8, 0xc00041ea40}, {0x7ffdd6570e52?, 0x0?}, 0xc00051c000)
	/opt/src/act_runner/internal/app/cmd/register.go:253 +0x45
gitea.com/gitea/act_runner/internal/app/cmd.Execute.runRegister.func4(0xc000510100?, {0xc00054a120?, 0x4?, 0xdb6c9a?})
	/opt/src/act_runner/internal/app/cmd/register.go:50 +0x156
github.com/spf13/cobra.(*Command).execute(0xc00050e300, {0xc00054a090, 0x9, 0x9})
	/go/pkg/mod/github.com/spf13/cobra@v1.7.0/command.go:940 +0x87c
github.com/spf13/cobra.(*Command).ExecuteC(0xc00050e000)
	/go/pkg/mod/github.com/spf13/cobra@v1.7.0/command.go:1068 +0x3a5
github.com/spf13/cobra.(*Command).Execute(...)
	/go/pkg/mod/github.com/spf13/cobra@v1.7.0/command.go:992
gitea.com/gitea/act_runner/internal/app/cmd.Execute({0xefd4f8?, 0xc00041ea40})
	/opt/src/act_runner/internal/app/cmd/cmd.go:82 +0x8b6
main.main()
	/opt/src/act_runner/main.go:18 +0x7b
Waiting to retry ...

I have an environment file at /conf/env. The crash occurs while reading that file. The line in config.go is assigning values it read from the environment file, into the environment map.

The runner: section of the config file contains:

  envs:
  # Extra environment variables to run jobs from a file.
  # It will be ignored if it's empty or the file doesn't exist.
  env_file: /conf/env

If I remove the "envs:", I get the same panic.
If I put a dummy value in there, like the default config file has, then it starts working.

  envs:
    # work around a nil map error
    ANSWER_TO_ULTIMATE_QUESTION_OF_LIFE_THE_UNIVERSE_AND_EVERYTHING: 42
  # Extra environment variables to run jobs from a file.
  # It will be ignored if it's empty or the file doesn't exist.
  env_file: /conf/env

The default config file contains some dummy values, but I think it is reasonable for users to remove those when they manually edit that config file. After all, they are obviously dummy values:

  # Extra environment variables to run jobs.
  envs:
    A_TEST_ENV_NAME_1: a_test_env_value_1
    A_TEST_ENV_NAME_2: a_test_env_value_2

I think the problem is that the config function relies on the YAML library to initialize the map, but it doesn't always do so. It only initializes the map if the config file had values to put in there.

So to fix this, I think a simple check would work:

        if cfg.Runner.Envs == nil {
                cfg.Runner.Envs = map[string]string{}
        }

but I haven't tested it.

Alternately, you could initialize the map explicitly when creating the Config object:

        	cfg := &Config{Runner: Runner{Envs: map[string]string{}}}
I tried to set up an action runner using the docker image and an edited config file. When I started the container, I got a loop of: ``` level=info msg="Registering runner, arch=amd64, os=linux, version=v0.2.6." panic: assignment to entry in nil map goroutine 1 [running]: gitea.com/gitea/act_runner/internal/pkg/config.LoadDefault({0x7ffdd6570e52, 0x11}) /opt/src/act_runner/internal/pkg/config/config.go:92 +0x3a8 gitea.com/gitea/act_runner/internal/app/cmd.registerNoInteractive({0xefd4f8, 0xc00041ea40}, {0x7ffdd6570e52?, 0x0?}, 0xc00051c000) /opt/src/act_runner/internal/app/cmd/register.go:253 +0x45 gitea.com/gitea/act_runner/internal/app/cmd.Execute.runRegister.func4(0xc000510100?, {0xc00054a120?, 0x4?, 0xdb6c9a?}) /opt/src/act_runner/internal/app/cmd/register.go:50 +0x156 github.com/spf13/cobra.(*Command).execute(0xc00050e300, {0xc00054a090, 0x9, 0x9}) /go/pkg/mod/github.com/spf13/cobra@v1.7.0/command.go:940 +0x87c github.com/spf13/cobra.(*Command).ExecuteC(0xc00050e000) /go/pkg/mod/github.com/spf13/cobra@v1.7.0/command.go:1068 +0x3a5 github.com/spf13/cobra.(*Command).Execute(...) /go/pkg/mod/github.com/spf13/cobra@v1.7.0/command.go:992 gitea.com/gitea/act_runner/internal/app/cmd.Execute({0xefd4f8?, 0xc00041ea40}) /opt/src/act_runner/internal/app/cmd/cmd.go:82 +0x8b6 main.main() /opt/src/act_runner/main.go:18 +0x7b Waiting to retry ... ``` I have an environment file at `/conf/env`. The crash occurs while reading that file. The [line in config.go](https://gitea.com/gitea/act_runner/src/commit/2020ce79bf4db797739c8e5d0275be27018088c5/internal/pkg/config/config.go#L92) is assigning values it read from the environment file, into the environment map. The `runner:` section of the config file contains: ```yaml envs: # Extra environment variables to run jobs from a file. # It will be ignored if it's empty or the file doesn't exist. env_file: /conf/env ``` If I remove the "envs:", I get the same panic. If I put a dummy value in there, like the default config file has, then it starts working. ```yaml envs: # work around a nil map error ANSWER_TO_ULTIMATE_QUESTION_OF_LIFE_THE_UNIVERSE_AND_EVERYTHING: 42 # Extra environment variables to run jobs from a file. # It will be ignored if it's empty or the file doesn't exist. env_file: /conf/env ``` The default config file contains some dummy values, but I think it is reasonable for users to remove those when they manually edit that config file. After all, they are obviously dummy values: ```yaml # Extra environment variables to run jobs. envs: A_TEST_ENV_NAME_1: a_test_env_value_1 A_TEST_ENV_NAME_2: a_test_env_value_2 ``` I think the problem is that the config function relies on the YAML library to initialize the map, but it doesn't always do so. It only initializes the map if the config file had values to put in there. So to fix this, I think a simple check would work: ```go if cfg.Runner.Envs == nil { cfg.Runner.Envs = map[string]string{} } ``` but I haven't tested it. Alternately, you could initialize the map explicitly when creating the Config object: ```go cfg := &Config{Runner: Runner{Envs: map[string]string{}}} ```
Sign in to join this conversation.