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

fix data race from getDefaultTransport 这个api会产生data race #218

Merged
merged 2 commits into from Mar 23, 2022

Conversation

wubin1989
Copy link
Contributor

我把控制台打印日志贴出来供参考:

==================
WARNING: DATA RACE
Read at 0x000006033768 by goroutine 7:
github.com/apolloconfig/agollo/v4/protocol/http.getDefaultTransport()
/Users/wubin1989/go/pkg/mod/github.com/apolloconfig/agollo/v4@v4.1.0/protocol/http/request.go:63 +0x31
github.com/apolloconfig/agollo/v4/protocol/http.Request()
/Users/wubin1989/go/pkg/mod/github.com/apolloconfig/agollo/v4@v4.1.0/protocol/http/request.go:111 +0x1e4
github.com/apolloconfig/agollo/v4/component/serverlist.SyncServerIPList()
/Users/wubin1989/go/pkg/mod/github.com/apolloconfig/agollo/v4@v4.1.0/component/serverlist/sync.go:90 +0x29c
github.com/apolloconfig/agollo/v4/component/serverlist.(*SyncServerIPListComponent).Start()
/Users/wubin1989/go/pkg/mod/github.com/apolloconfig/agollo/v4@v4.1.0/component/serverlist/sync.go:56 +0x44
github.com/apolloconfig/agollo/v4/component.StartRefreshConfig()
/Users/wubin1989/go/pkg/mod/github.com/apolloconfig/agollo/v4@v4.1.0/component/common.go:27 +0x37
github.com/apolloconfig/agollo/v4/component/serverlist.InitSyncServerIPList·dwrap·1()
/Users/wubin1989/go/pkg/mod/github.com/apolloconfig/agollo/v4@v4.1.0/component/serverlist/sync.go:45 +0x47

Previous write at 0x000006033768 by main goroutine:
github.com/apolloconfig/agollo/v4/protocol/http.getDefaultTransport.func1()
/Users/wubin1989/go/pkg/mod/github.com/apolloconfig/agollo/v4@v4.1.0/protocol/http/request.go:65 +0x270
sync.(*Once).doSlow()
/usr/local/go/src/sync/once.go:68 +0x127
sync.(*Once).Do()
/usr/local/go/src/sync/once.go:59 +0x46
github.com/apolloconfig/agollo/v4/protocol/http.getDefaultTransport()
/Users/wubin1989/go/pkg/mod/github.com/apolloconfig/agollo/v4@v4.1.0/protocol/http/request.go:64 +0x6f
github.com/apolloconfig/agollo/v4/protocol/http.Request()
/Users/wubin1989/go/pkg/mod/github.com/apolloconfig/agollo/v4@v4.1.0/protocol/http/request.go:111 +0x1e4
github.com/apolloconfig/agollo/v4/protocol/http.RequestRecovery()
/Users/wubin1989/go/pkg/mod/github.com/apolloconfig/agollo/v4@v4.1.0/protocol/http/request.go:203 +0x186
github.com/apolloconfig/agollo/v4/component/remote.(*AbsApolloConfig).SyncWithNamespace()
/Users/wubin1989/go/pkg/mod/github.com/apolloconfig/agollo/v4@v4.1.0/component/remote/abs.go:58 +0x4cd
github.com/apolloconfig/agollo/v4/component/remote.(*syncApolloConfig).Sync.func1()
/Users/wubin1989/go/pkg/mod/github.com/apolloconfig/agollo/v4@v4.1.0/component/remote/sync.go:100 +0x9c
github.com/apolloconfig/agollo/v4/env/config.SplitNamespaces()
/Users/wubin1989/go/pkg/mod/github.com/apolloconfig/agollo/v4@v4.1.0/env/config/config.go:116 +0x1ac
github.com/apolloconfig/agollo/v4/component/remote.(*syncApolloConfig).Sync()
/Users/wubin1989/go/pkg/mod/github.com/apolloconfig/agollo/v4@v4.1.0/component/remote/sync.go:99 +0x175
github.com/apolloconfig/agollo/v4.StartWithConfig()
/Users/wubin1989/go/pkg/mod/github.com/apolloconfig/agollo/v4@v4.1.0/client.go:124 +0x2c9
github.com/unionj-cloud/go-doudou/framework/configmgr.LoadFromApollo()
/Users/wubin1989/workspace/cloud/go-doudou/framework/configmgr/apollo.go:19 +0x6f
github.com/unionj-cloud/go-doudou/framework/internal/config.init.0()
/Users/wubin1989/workspace/cloud/go-doudou/framework/internal/config/config.go:67 +0x719

Goroutine 7 (running) created at:
github.com/apolloconfig/agollo/v4/component/serverlist.InitSyncServerIPList()
/Users/wubin1989/go/pkg/mod/github.com/apolloconfig/agollo/v4@v4.1.0/component/serverlist/sync.go:45 +0xcb
github.com/apolloconfig/agollo/v4.StartWithConfig()
/Users/wubin1989/go/pkg/mod/github.com/apolloconfig/agollo/v4@v4.1.0/client.go:121 +0x248
github.com/unionj-cloud/go-doudou/framework/configmgr.LoadFromApollo()
/Users/wubin1989/workspace/cloud/go-doudou/framework/configmgr/apollo.go:19 +0x6f
github.com/unionj-cloud/go-doudou/framework/internal/config.init.0()
/Users/wubin1989/workspace/cloud/go-doudou/framework/internal/config/config.go:67 +0x719

@mergify
Copy link
Contributor

mergify bot commented Mar 22, 2022

感谢您提出Pull Request,我会尽快Review。我会在1-2日内进行查看或者回复,如果遇到节假日可能会处理较慢,敬请谅解。

)

func getDefaultTransport(insecureSkipVerify bool) *http.Transport {
lock.Lock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接把 once 挪到外层可以么?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果想最简单最优雅,可以这样改:

func getDefaultTransport(insecureSkipVerify bool) *http.Transport {
	once.Do(func() {
		defaultTransport = &http.Transport{
			MaxIdleConns:        defaultMaxConnsPerHost,
			MaxIdleConnsPerHost: defaultMaxConnsPerHost,
			DialContext: (&net.Dialer{
				KeepAlive: defaultKeepAliveSecond,
				Timeout:   defaultTimeoutBySecond,
			}).DialContext,
		}
		if insecureSkipVerify {
			defaultTransport.TLSClientConfig = &tls.Config{
				InsecureSkipVerify: insecureSkipVerify,
			}
		}
	})
	return defaultTransport
}

我看defaultTransport这个单例只有在这个函数里有赋值操作,所以不做nil检查也是可以的。如果不放心就按你说的,在once.Do的入参函数里先做nil检查再赋值。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你告诉我怎么改,我马上改好再提一次

Copy link
Member

@zouyx zouyx Mar 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果想最简单最优雅,可以这样改:

func getDefaultTransport(insecureSkipVerify bool) *http.Transport {
	once.Do(func() {
		defaultTransport = &http.Transport{
			MaxIdleConns:        defaultMaxConnsPerHost,
			MaxIdleConnsPerHost: defaultMaxConnsPerHost,
			DialContext: (&net.Dialer{
				KeepAlive: defaultKeepAliveSecond,
				Timeout:   defaultTimeoutBySecond,
			}).DialContext,
		}
		if insecureSkipVerify {
			defaultTransport.TLSClientConfig = &tls.Config{
				InsecureSkipVerify: insecureSkipVerify,
			}
		}
	})
	return defaultTransport
}

我看defaultTransport这个单例只有在这个函数里有赋值操作,所以不做nil检查也是可以的。如果不放心就按你说的,在once.Do的入参函数里先做nil检查再赋值。

其实我的意思就是这个- -

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

收到

@coveralls
Copy link

coveralls commented Mar 23, 2022

Pull Request Test Coverage Report for Build 2019828781

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.03%) to 76.093%

Totals Coverage Status
Change from base Build 1736298782: 0.03%
Covered Lines: 1184
Relevant Lines: 1556

💛 - Coveralls

@zouyx zouyx self-assigned this Mar 23, 2022
@zouyx zouyx added the bug label Mar 23, 2022
@zouyx zouyx changed the base branch from master to develop March 23, 2022 01:59
@wubin1989
Copy link
Contributor Author

麻烦再看一下最新的commit:55ac04e

@mergify mergify bot merged commit 60ed861 into apolloconfig:develop Mar 23, 2022
@zouyx
Copy link
Member

zouyx commented Mar 26, 2022

@wubin1989 已发布

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

Successfully merging this pull request may close these issues.

None yet

3 participants