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

fatal error: concurrent map writes panic #629

Open
2tvenom opened this issue Jan 16, 2019 · 3 comments
Open

fatal error: concurrent map writes panic #629

2tvenom opened this issue Jan 16, 2019 · 3 comments

Comments

@2tvenom
Copy link

2tvenom commented Jan 16, 2019

Got panic
v1.3.1

fatal error: concurrent map writes

goroutine 9 [running]:
runtime.throw(0x1b7726c, 0x15)
        /usr/local/Cellar/go/1.11.2/libexec/src/runtime/panic.go:608 +0x72 fp=0xc00015a778 sp=0xc00015a748 pc=0x102cef2
runtime.mapassign_faststr(0x19e5e00, 0xc0001b25a0, 0xc0000d7280, 0x15, 0xc000250f68)
        /usr/local/Cellar/go/1.11.2/libexec/src/runtime/map_faststr.go:199 +0x3da fp=0xc00015a7e0 sp=0xc00015a778 pc=0x101442a
gitlab.__.com/__/__/vendor/github.com/spf13/viper.insensitiviseMap(0xc0001b25a0)
        /Users/__/golibs/src/gitlab.__.com/__/__/vendor/github.com/spf13/viper/util.go:87 +0x196 fp=0xc00015a8c0 sp=0xc00015a7e0 pc=0x152fbe6
@sagikazarmark
Copy link
Collaborator

Can you share some example code to reproduce the issue? Are you trying to use Viper from separate goroutines?

@stanislav-bios-baranov
Copy link

stanislav-bios-baranov commented Mar 12, 2019

Got same problem, but race appears in a place, which is totally unexpected to face it: UnmarshalKey. My expectation was, that unmarshaling part of config into external struct, should not change any internal structures inside viper package.

Here is small test, which fails for me in 50% of cases:

package main

import (
	"fmt"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/spf13/afero"
	"github.com/spf13/viper"
)

type Setting struct {
	TestValue *string `json:"testValue,omitempty" mapstructure:"testvalue"`
}

func TestConcurrency(t *testing.T) {

	content := []byte(`{"settings": {"A": {"value": "value A"}, "B": {"value": "value B"}}}`)

	aferoFS := afero.NewMemMapFs()
	require.NoError(t, afero.WriteFile(aferoFS, "conf.json", content, 0755))

	v := viper.New()

	v.SetConfigType("json")
	v.SetFs(aferoFS)
	v.SetConfigFile("conf.json")
	require.NoError(t, v.ReadInConfig())

	for i := 1; i < 100; i++ {
		t.Run(fmt.Sprintf("flow #%d", i), func(tt *testing.T) {
			tt.Parallel()
			settings := make(map[string]*Setting)
			require.NotPanics(t, func() {
				require.NoError(t, v.UnmarshalKey("settings", &settings))
			})
		})
	}

}

In my case problem is with insensitiviseMaps call from UnmarshalKey ( https://github.com/spf13/viper/blob/master/viper.go#L1582 )

@moorereason
Copy link
Contributor

Is this fixed by 9e56dac?

aeneasr added a commit to ory/viper that referenced this issue Jul 15, 2019
aeneasr added a commit to ory/viper that referenced this issue Jul 15, 2019
ttys3 pushed a commit to ttys3/viper that referenced this issue Aug 25, 2019
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

No branches or pull requests

4 participants