Skip to content

Commit 029d151

Browse files
Address a data race setting seal wrapper health attributes (#27014)
* Address a data race setting seal wrapper health attributes - When updating a seal wrapper's lastHealthCheck in a failure scenario we would read the lastSeenHealthy value in order to not update it outside of a locked context. This lead to the data race. - Merge the SetHealthy and setHealthy functions into one, in order to combine the logic and locking in a single function * Add cl
1 parent 9e8d9db commit 029d151

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

changelog/27014.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
core: Address a data race updating a seal's last seen healthy time attribute
3+
```

vault/seal/seal_wrapper.go

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,32 @@ type SealWrapper struct {
5151

5252
func NewSealWrapper(wrapper wrapping.Wrapper, priority int, name string, sealConfigType string, disabled bool, configured bool) *SealWrapper {
5353
ret := &SealWrapper{
54-
Wrapper: wrapper,
55-
Priority: priority,
56-
Name: name,
57-
SealConfigType: sealConfigType,
58-
Disabled: disabled,
59-
Configured: configured,
54+
Wrapper: wrapper,
55+
Priority: priority,
56+
Name: name,
57+
SealConfigType: sealConfigType,
58+
Disabled: disabled,
59+
Configured: configured,
60+
lastSeenHealthy: time.Now(),
61+
healthy: false,
6062
}
6163

6264
if configured {
63-
setHealth(ret, true, time.Now(), ret.lastHealthCheck)
64-
} else {
65-
setHealth(ret, false, time.Now(), ret.lastHealthCheck)
65+
ret.healthy = true
6666
}
6767

6868
return ret
6969
}
7070

7171
func (sw *SealWrapper) SetHealthy(healthy bool, checkTime time.Time) {
72+
sw.hcLock.Lock()
73+
defer sw.hcLock.Unlock()
74+
75+
sw.healthy = healthy
76+
sw.lastHealthCheck = checkTime
77+
7278
if healthy {
73-
setHealth(sw, true, checkTime, checkTime)
74-
} else {
75-
// do not update lastSeenHealthy
76-
setHealth(sw, false, sw.lastHealthCheck, checkTime)
79+
sw.lastSeenHealthy = checkTime
7780
}
7881
}
7982

@@ -134,13 +137,3 @@ func getHealth(sw *SealWrapper) (healthy bool, lastSeenHealthy time.Time, lastHe
134137

135138
return sw.healthy, sw.lastSeenHealthy, sw.lastHealthCheck
136139
}
137-
138-
// setHealth is the only function allowed to mutate the health fields
139-
func setHealth(sw *SealWrapper, healthy bool, lastSeenHealthy, lastHealthCheck time.Time) {
140-
sw.hcLock.Lock()
141-
defer sw.hcLock.Unlock()
142-
143-
sw.healthy = healthy
144-
sw.lastSeenHealthy = lastSeenHealthy
145-
sw.lastHealthCheck = lastHealthCheck
146-
}

0 commit comments

Comments
 (0)