Skip to content

Commit 4d9dced

Browse files
committed
Avoid false alarm in assertion that involves floating point arithmetic
The FP arithmetic that leads to the branch with the assertion and the assertion's FP arithmetic are now in monotonic relationship.
1 parent 7d13ff1 commit 4d9dced

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

simulation/src/Chan/TCP.hs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,15 @@ transport tracer tcpprops sendbuf recvbuf = do
143143
-- if the connection was idle too long, reset the window size
144144
let tcpstate' :: TcpState
145145
tcpstate'
146-
| now' `diffTime` now <= tcpIdleResetTime tcpprops =
147-
tcpstate
148-
| otherwise =
146+
| tcpIdleResetTime tcpprops now <= now' =
149147
let allAcksArrived =
150148
PQ.foldrWithKeyU
151-
(\t _ ok -> t < now' && ok)
149+
(\t _ ok -> t <= now' && ok)
152150
True
153151
(tcpAcknowledgements tcpstate)
154152
in assert allAcksArrived initTcpState
153+
| otherwise =
154+
tcpstate
155155

156156
-- send it
157157
let msgsize = messageSizeBytes msg
@@ -178,8 +178,10 @@ transport tracer tcpprops sendbuf recvbuf = do
178178
-- We could do that, but the algorithm also uses a minimum of 1s, which appears
179179
-- to be the limit in practice. It converges to 1 RTT if there's not much
180180
-- jitter. So we just use the max of the RTT and 1s.
181-
tcpIdleResetTime :: TcpConnProps -> DiffTime
182-
tcpIdleResetTime TcpConnProps{tcpLatency} =
183-
max 1 rtt
184-
where
185-
rtt = tcpLatency * 2
181+
--
182+
-- The signature and order of operations is awkward here because it needs to
183+
-- exactly match an assertion near the use case, since these types are
184+
-- represented as 'Double's.
185+
tcpIdleResetTime :: TcpConnProps -> Time -> Time
186+
tcpIdleResetTime TcpConnProps{tcpLatency} t =
187+
max (1 `addTime` t) (tcpLatency `addTime` (tcpLatency `addTime` t))

0 commit comments

Comments
 (0)