From 66ec3159d61887141eebb334a0147e0b987c2555 Mon Sep 17 00:00:00 2001 From: Nick Khyl Date: Wed, 25 Nov 2026 10:41:48 -0800 Subject: [PATCH] util/syspolicy/rsop: don't wait for change callbacks when reloading policy Registering, replacing or unregistering a store, or calling Policy.Reload, waited for the policy change callbacks to return, deadlocking if the caller held a lock that a callback acquires, such as LocalBackend.mu. Invoke the callbacks from an execqueue instead. The effective policy is still updated by the time these calls return. Updates tailscale/corp#51945 Change-Id: I4afa36b9acaa0e0104e4e420c561eab076d06c80 Signed-off-by: Nick Khyl --- cmd/tailscale/depaware.txt | 1 + util/syspolicy/rsop/change_callbacks.go | 14 ++++- util/syspolicy/rsop/resultant_policy_test.go | 54 ++++++++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/cmd/tailscale/depaware.txt b/cmd/tailscale/depaware.txt index 7140805ac..8b22467e8 100644 --- a/cmd/tailscale/depaware.txt +++ b/cmd/tailscale/depaware.txt @@ -309,6 +309,7 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep L 💣 tailscale.com/util/dirwalk from tailscale.com/metrics tailscale.com/util/dnsname from tailscale.com/cmd/tailscale/cli+ tailscale.com/util/eventbus from tailscale.com/client/local+ + tailscale.com/util/execqueue from tailscale.com/util/syspolicy/rsop tailscale.com/util/groupmember from tailscale.com/client/web 💣 tailscale.com/util/hashx from tailscale.com/util/deephash tailscale.com/util/httpbody from tailscale.com/control/ts2021 diff --git a/util/syspolicy/rsop/change_callbacks.go b/util/syspolicy/rsop/change_callbacks.go index 0b6cd10c4..4bd549432 100644 --- a/util/syspolicy/rsop/change_callbacks.go +++ b/util/syspolicy/rsop/change_callbacks.go @@ -10,6 +10,7 @@ import ( "time" "tailscale.com/syncs" + "tailscale.com/util/execqueue" "tailscale.com/util/set" "tailscale.com/util/syspolicy/internal/loggerx" "tailscale.com/util/syspolicy/pkey" @@ -73,6 +74,7 @@ func (c PolicyChange) HasChangedAnyOf(keys ...pkey.Key) bool { type policyChangeCallbacks struct { mu syncs.Mutex cbs set.HandleSet[PolicyChangeCallback] + q execqueue.ExecQueue // invokes cbs, one policy change at a time } // Register adds the specified callback to be invoked whenever the policy changes. @@ -87,8 +89,15 @@ func (c *policyChangeCallbacks) Register(callback PolicyChangeCallback) (unregis } } -// Invoke calls the registered callback functions with the specified policy change info. +// Invoke schedules the registered callback functions to be called with the +// specified policy change info. It does not wait for them to return, so it +// can be called while holding locks that the callbacks acquire. The callbacks +// for one policy change return before those for the next one are called. func (c *policyChangeCallbacks) Invoke(snapshots Change[*setting.Snapshot]) { + c.q.Add(func() { c.invoke(snapshots) }) +} + +func (c *policyChangeCallbacks) invoke(snapshots Change[*setting.Snapshot]) { var wg sync.WaitGroup defer wg.Wait() @@ -105,8 +114,9 @@ func (c *policyChangeCallbacks) Invoke(snapshots Change[*setting.Snapshot]) { } } -// Close awaits the completion of active callbacks and prevents any further invocations. +// Close prevents any further invocations. func (c *policyChangeCallbacks) Close() { + c.q.Shutdown() c.mu.Lock() defer c.mu.Unlock() if c.cbs != nil { diff --git a/util/syspolicy/rsop/resultant_policy_test.go b/util/syspolicy/rsop/resultant_policy_test.go index 3f83afa72..4b9408962 100644 --- a/util/syspolicy/rsop/resultant_policy_test.go +++ b/util/syspolicy/rsop/resultant_policy_test.go @@ -1036,3 +1036,57 @@ func policyForTest(tb testing.TB, target setting.PolicyScope) (*Policy, error) { }) return policy, nil } + +// TestReplacePolicySourceWithCallbackLockHeld verifies that a store can be +// replaced while holding a lock that a policy change callback acquires. +func TestReplacePolicySourceWithCallbackLockHeld(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + testSetting := setting.NewDefinition("TestSettingA", setting.DeviceSetting, setting.StringValue) + if err := setting.SetDefinitionsForTest(t, testSetting); err != nil { + t.Fatalf("SetDefinitionsForTest failed: %v", err) + } + + reg, err := RegisterStoreForTest(t, "TestStore", setting.DeviceScope, source.NewTestStoreOf(t, source.TestSettingOf(testSetting.Key(), "0"))) + if err != nil { + t.Fatalf("Failed to register the initial store: %v", err) + } + policy, err := policyForTest(t, setting.DeviceScope) + if err != nil { + t.Fatalf("Failed to get effective policy: %v", err) + } + + var mu sync.Mutex + var got []any // values seen by the callback; guarded by mu + t.Cleanup(policy.RegisterChangeCallback(func(pc policyclient.PolicyChange) { + mu.Lock() + defer mu.Unlock() + got = append(got, pc.(*PolicyChange).New().Get(testSetting.Key())) + })) + + var want []any + mu.Lock() + for i := range 3 { + value := strconv.Itoa(i + 1) + want = append(want, value) + reg, err = reg.ReplaceStore(source.NewTestStoreOf(t, source.TestSettingOf(testSetting.Key(), value))) + if err != nil { + mu.Unlock() + t.Fatalf("Failed to replace the policy store: %v", err) + } + t.Cleanup(func() { reg.Unregister() }) + // The effective policy must be updated by the time ReplaceStore returns. + if got := policy.Get().Get(testSetting.Key()); got != value { + mu.Unlock() + t.Fatalf("Setting %q: got %q; want %q", testSetting.Key(), got, value) + } + } + mu.Unlock() + + synctest.Wait() + mu.Lock() + defer mu.Unlock() + if !slices.Equal(got, want) { + t.Errorf("callback values: got %v; want %v", got, want) + } + }) +}