Skip to content

Commit 4d105a2

Browse files
auricomclaude
andcommitted
fix(config): reject negative ElectionTimeout in RaftConfig.Validate
A negative ElectionTimeout was silently ignored (buildRaftConfig only applies values > 0), allowing a misconfigured node to start with the library default instead of failing fast. Add an explicit < 0 check that returns an error; 0 remains valid as the "use library default" sentinel. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent a2a2599 commit 4d105a2

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

pkg/config/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,9 @@ func (c RaftConfig) Validate() error {
451451
multiErr = errors.Join(multiErr, fmt.Errorf("leader lease timeout must be positive"))
452452
}
453453

454-
if c.ElectionTimeout > 0 && c.ElectionTimeout < c.HeartbeatTimeout {
454+
if c.ElectionTimeout < 0 {
455+
multiErr = errors.Join(multiErr, fmt.Errorf("election timeout (%v) must be >= 0", c.ElectionTimeout))
456+
} else if c.ElectionTimeout > 0 && c.ElectionTimeout < c.HeartbeatTimeout {
455457
multiErr = errors.Join(multiErr, fmt.Errorf("election timeout (%v) must be >= heartbeat timeout (%v)", c.ElectionTimeout, c.HeartbeatTimeout))
456458
}
457459

pkg/config/config_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,10 @@ func TestRaftConfig_Validate(t *testing.T) {
450450
mutate: func(c *RaftConfig) { c.ShutdownTimeout = 0 },
451451
expErr: "shutdown timeout must be positive",
452452
},
453+
"negative election timeout rejected": {
454+
mutate: func(c *RaftConfig) { c.ElectionTimeout = -1 * time.Second },
455+
expErr: "election timeout (-1s) must be >= 0",
456+
},
453457
"election timeout less than heartbeat timeout": {
454458
mutate: func(c *RaftConfig) { c.ElectionTimeout = 500 * time.Millisecond },
455459
expErr: "election timeout (500ms) must be >= heartbeat timeout (1s)",

0 commit comments

Comments
 (0)