forked from WireGuard/wireguard-go
-
Notifications
You must be signed in to change notification settings - Fork 34
device: fix some lock ordering violations, add a test for a deadlock we hit #55
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* SPDX-License-Identifier: MIT | ||
| * | ||
| * Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved. | ||
| */ | ||
|
|
||
| package device | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/tailscale/wireguard-go/conn" | ||
| "github.com/tailscale/wireguard-go/tun/tuntest" | ||
| ) | ||
|
|
||
| // TestSetPrivateKeyConsumeInitiationDeadlock verifies that SetPrivateKey and | ||
| // ConsumeMessageInitiation do not deadlock. | ||
| // | ||
| // ConsumeMessageInitiation holds staticIdentity.RLock (noise-protocol.go:352) | ||
| // for the duration of the call. When processing an unknown peer, it calls | ||
| // LookupPeer -> PeerLookupFunc -> NewPeer, and NewPeer attempts a reentrant | ||
| // staticIdentity.RLock (peer.go:82). If SetPrivateKey has called | ||
| // staticIdentity.Lock (device.go:235) in between, the pending writer blocks | ||
| // the reentrant reader, while the writer waits for the original reader to | ||
| // release, a classic sync.RWMutex reentrant-reader deadlock. | ||
| // | ||
| // We would like to use testing/synctest to eliminate the wall-clock sleep | ||
| // and 5-second timeout, but sync.RWMutex is not bubble-aware as of Go 1.26, | ||
| // so a goroutine blocked in RWMutex.Lock is not classified as "durably | ||
| // blocked" and synctest cannot detect this deadlock. Revisit once the sync | ||
| // package participates in synctest bubbles. | ||
| func TestSetPrivateKeyConsumeInitiationDeadlock(t *testing.T) { | ||
| // Create the receiver device. We intentionally avoid t.Cleanup(Close) | ||
| // because in the deadlock case the locks are permanently held and | ||
| // Close would block. | ||
| recvSK, err := newPrivateKey() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| receiver := NewDevice( | ||
| tuntest.NewChannelTUN().TUN(), | ||
| conn.NewDefaultBind(), | ||
| NewLogger(LogLevelError, ""), | ||
| ) | ||
| receiver.SetPrivateKey(recvSK) | ||
|
|
||
| // Create the initiator device and add receiver as a peer so we can | ||
| // produce a valid MessageInitiation. | ||
| initiator := randDevice(t) | ||
| defer initiator.Close() | ||
|
|
||
| peer, err := initiator.NewPeer(recvSK.publicKey()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| peer.Start() | ||
|
|
||
| msg, err := initiator.CreateMessageInitiation(peer) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| newSK, err := newPrivateKey() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| // PeerLookupFunc is the synchronization point: when it fires, | ||
| // ConsumeMessageInitiation is holding staticIdentity.RLock and we are | ||
| // between the two RLock acquisitions (the second one is inside NewPeer). | ||
| inLookup := make(chan struct{}) | ||
| proceed := make(chan struct{}) | ||
| receiver.SetPeerLookupFunc(func(pk NoisePublicKey) (_ *NewPeerConfig, ok bool) { | ||
| close(inLookup) | ||
| <-proceed | ||
| return &NewPeerConfig{}, true | ||
| }) | ||
|
|
||
| // Goroutine A: ConsumeMessageInitiation holds staticIdentity.RLock. | ||
| go receiver.ConsumeMessageInitiation(msg, nil) | ||
| <-inLookup | ||
|
|
||
| // Goroutine B: SetPrivateKey calls staticIdentity.Lock (write); | ||
| // blocks because A holds the read lock. | ||
| setKeyDone := make(chan error, 1) | ||
| go func() { | ||
| setKeyDone <- receiver.SetPrivateKey(newSK) | ||
| }() | ||
|
|
||
| // Give B time to reach staticIdentity.Lock (the very first operation | ||
| // in SetPrivateKey), so it becomes a pending writer. | ||
| time.Sleep(20 * time.Millisecond) | ||
|
|
||
| // A proceeds: LookupPeer → NewPeer → staticIdentity.RLock (reentrant). | ||
| // With B's write pending, the reentrant read lock blocks. Deadlock. | ||
| close(proceed) | ||
|
|
||
| select { | ||
| case err := <-setKeyDone: | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| receiver.Close() | ||
| case <-time.After(5 * time.Second): | ||
| t.Fatal("deadlock: ConsumeMessageInitiation holds staticIdentity.RLock, " + | ||
| "SetPrivateKey is pending staticIdentity.Lock (write), and NewPeer's " + | ||
| "reentrant staticIdentity.RLock is blocked by the pending writer") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Lock Ordering in wireguard-go/device | ||
|
|
||
| ## Lock hierarchy | ||
|
|
||
| Locks must be acquired in the order listed below. A goroutine holding a | ||
| lock with a higher number must never attempt to acquire a lock with a | ||
| lower number. | ||
|
|
||
| ``` | ||
| Level 0 device.state.Mutex | ||
| Level 1 device.ipcMutex (sync.RWMutex) | ||
| Level 2 device.net.RWMutex | ||
| Level 3 device.staticIdentity.RWMutex | ||
| Level 4 device.peers.RWMutex | ||
| Level 5 peer.state.Mutex | ||
| Level 6 peer.handshake.mutex (sync.RWMutex) | ||
| Level 7 peer.keypairs.RWMutex | ||
| Level 8 device.allowedips.mu (sync.RWMutex) | ||
| Level 9 device.indexTable.RWMutex | ||
| Level 10 peer.endpoint.Mutex | ||
| Level 11 device.cookieChecker.RWMutex | ||
| Level 12 peer.cookieGenerator.RWMutex | ||
| Level 13 Timer.modifyingLock / Timer.runningLock | ||
| ``` | ||
|
|
||
| Not every pair of locks appears in practice; the ordering above is the | ||
| transitive closure of the pairs that do. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note for posterity: i have an upstream patch been waiting in review since 2023 that removes the rwlock from the path because it's actually slower than a mutex when measured in the real world, and it's over-complicated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, we should make this a regular mutex probably