Skip to content

fix: nil keepalive timer when the negotiated hold time is zero - #68

Open
gregwight wants to merge 1 commit into
jwhited:mainfrom
gregwight:fix-holdtime-zero-nil-keepalive-timer
Open

gregwight wants to merge 1 commit into
jwhited:mainfrom
gregwight:fix-holdtime-zero-nil-keepalive-timer

Conversation

@gregwight

@gregwight gregwight commented Sep 18, 2026 •

Copy link
Copy Markdown

A peer advertising Hold Time 0 panics the FSM

What happens

fsm.keepAliveTimer is only ever assigned inside a non-zero hold time check in openSent:

f.holdTime = time.Duration(m.holdTime) * time.Second
if f.peer.options.holdTime < f.holdTime {
    f.holdTime = f.peer.options.holdTime
}
if f.holdTime != 0 {
    f.keepAliveInterval = f.holdTime / 3
    f.keepAliveTimer = time.NewTimer(f.keepAliveInterval)
    f.drainAndResetHoldTimer()
}

The struct field is never initialised elsewhere - the constructor sets idleHoldTimer but not this one - so a negotiated hold time of zero leaves it nil. openConfirm then reads from it unconditionally:

case <-f.keepAliveTimer.C:

which is a nil pointer dereference. established does the same, and both states' cleanup paths call f.keepAliveTimer.Stop().

There is no recover in fsm.run, so this takes down the whole process, not just the peer.

Why it is reachable

Hold Time 0 is legal. RFC 4271 section 4.2:

A value of 0 indicates that the Hold Timer is never started.

The negotiation takes the minimum of the two speakers' values, so a remote peer sending 0 produces 0 regardless of what the local side configured. A local WithHoldTime(0) does the same.

That makes it reachable from the other end of the connection: a speaker which accepts inbound sessions can be crashed by a peer that offers Hold Time 0, whether by misconfiguration or deliberately. A Plugin cannot defend against
it either, since OnOpenMessage is not given the hold time.

The invariant is already understood in one place - the keepalive manager goroutine guards its reset with if f.holdTime != 0 - so this looks like the same check missing from the other five uses rather than a design intent.

Fix

The attached patch routes every use through three small helpers, so a nil timer means "no keepalives, never expires", which is what a zero hold time is specified to mean. A nil channel in a select blocks forever, which gives that behaviour without a special case at each site.

Applies to v0.8.5. Verified: builds, go vet clean, and the existing test suite passes.

A regression test

Not included, because reproducing it needs an OPEN exchange and TestServer currently covers peer bookkeeping rather than session establishment. Happy to add one against whatever harness you would prefer - two servers peering over loopback with WithHoldTime(0) on one side reproduces it.

Note on a related semantic

With the panic fixed, a zero hold time still leaves holdTimer running at the longHoldTime it was given before negotiation, because drainAndResetHoldTimer is only called on the non-zero path. So the session would be torn down when that expires, where RFC 4271 says the timer should never start. That is a separate change and not in this patch.

keepAliveTimer is assigned only inside the non-zero hold time check in
openSent, and the fsm constructor does not initialise it, so a negotiated hold
time of zero leaves it nil. openConfirm and established then read from it
unconditionally and both cleanup paths call Stop on it, which panics. There is
no recover in fsm.run, so this takes down the process rather than the peer.

Hold Time 0 is legal: RFC 4271 section 4.2 gives it the meaning that the hold
timer is never started. Negotiation takes the lower of the two speakers'
values, so a remote peer produces it regardless of the local configuration, and
a Plugin cannot refuse it because OnOpenMessage is not given the hold time.

Every use now goes through nil-safe helpers, so a nil timer means no keepalives
and no expiry, which is what a zero hold time specifies. Receiving from a nil
channel blocks forever, which gives that behaviour in a select without a
special case at each site. The keepalive manager's existing "if f.holdTime != 0"
guard is subsumed by resetKeepAliveTimer.
@floatingstatic

Copy link
Copy Markdown
Contributor

@jwhited I need this as well if you have time to review. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants