fix(enterprise/coderd): prevent deadlock during entitlements update (#8215)

This commit is contained in:
Mathias Fredriksson
2023-06-26 20:22:28 +03:00
committed by GitHub
parent 56395410bd
commit 31076ad665
2 changed files with 9 additions and 6 deletions

View File

@ -313,6 +313,7 @@ type API struct {
// ProxyHealth checks the reachability of all workspace proxies. // ProxyHealth checks the reachability of all workspace proxies.
ProxyHealth *proxyhealth.ProxyHealth ProxyHealth *proxyhealth.ProxyHealth
entitlementsUpdateMu sync.Mutex
entitlementsMu sync.RWMutex entitlementsMu sync.RWMutex
entitlements codersdk.Entitlements entitlements codersdk.Entitlements
} }
@ -329,8 +330,8 @@ func (api *API) Close() error {
} }
func (api *API) updateEntitlements(ctx context.Context) error { func (api *API) updateEntitlements(ctx context.Context) error {
api.entitlementsMu.Lock() api.entitlementsUpdateMu.Lock()
defer api.entitlementsMu.Unlock() defer api.entitlementsUpdateMu.Unlock()
entitlements, err := license.Entitlements( entitlements, err := license.Entitlements(
ctx, api.Database, ctx, api.Database,
@ -457,6 +458,8 @@ func (api *API) updateEntitlements(ctx context.Context) error {
} }
} }
api.entitlementsMu.Lock()
defer api.entitlementsMu.Unlock()
api.entitlements = entitlements api.entitlements = entitlements
api.AGPL.SiteHandler.Entitlements.Store(&entitlements) api.AGPL.SiteHandler.Entitlements.Store(&entitlements)

View File

@ -9,9 +9,9 @@ import (
) )
func (api *API) shouldBlockNonBrowserConnections(rw http.ResponseWriter) bool { func (api *API) shouldBlockNonBrowserConnections(rw http.ResponseWriter) bool {
api.entitlementsMu.Lock() api.entitlementsMu.RLock()
browserOnly := api.entitlements.Features[codersdk.FeatureBrowserOnly].Enabled browserOnly := api.entitlements.Features[codersdk.FeatureBrowserOnly].Enabled
api.entitlementsMu.Unlock() api.entitlementsMu.RUnlock()
if browserOnly { if browserOnly {
httpapi.Write(context.Background(), rw, http.StatusConflict, codersdk.Response{ httpapi.Write(context.Background(), rw, http.StatusConflict, codersdk.Response{
Message: "Non-browser connections are disabled for your deployment.", Message: "Non-browser connections are disabled for your deployment.",