Skip to content

Commit 4b893eb

Browse files
authored
Discard received data on uTP content stream timeout (#3014)
Discard the received data on uTP content stream read timeout. Before the data was still added to the queue and being processed and should normally fail in validation. However as we know not all data got read it should not even move to the validation step. Added however a FIN send after the timeout instead of the delayed socket clean-up which does not make much sense in that scenario either. Basically either be nice and still send a FIN or just destroy the socket immediatly.
1 parent c62024e commit 4b893eb

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

fluffy/network/wire/portal_stream.nim

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Nimbus
2-
# Copyright (c) 2022-2024 Status Research & Development GmbH
2+
# Copyright (c) 2022-2025 Status Research & Development GmbH
33
# Licensed and distributed under either of
44
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
55
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@@ -300,16 +300,13 @@ proc readContentOffer(
300300
# Read number of content values according to amount of ContentKeys accepted.
301301
# This will either end with a FIN, or because the read action times out or
302302
# because the number of expected values was read (if this happens and no FIN
303-
# was received yet, a FIN will be send from this side).
303+
# is received eventually, the socket will just get destroyed).
304304
# None of this means that the contentValues are valid, further validation is
305-
# required.
306-
# Socket will be closed when this call ends.
307-
308-
# TODO: Currently reading from the socket one value at a time, and validating
309-
# values at later time. Uncertain what is best approach here (mostly from a
310-
# security PoV), e.g. other options such as reading all content from socket at
311-
# once, then processing the individual content values. Or reading and
312-
# validating one per time.
305+
# required. This call deals with cleaning up the socket.
306+
307+
# Content items are read from the socket and added to a queue for later
308+
# validation. Validating the content item immediatly would likely result in
309+
# timeouts of the rest of the content transmitted.
313310
let amount = offer.contentKeys.len()
314311

315312
var contentValues: seq[seq[byte]]
@@ -327,11 +324,15 @@ proc readContentOffer(
327324
contentKeys = offer.contentKeys, error = contentValue.error
328325
break
329326
else:
330-
# Read timed out, stop further reading, but still process data received
331-
# so far.
327+
# Read timed out, stop further reading and discard the data received so
328+
# far as it will be incomplete.
332329
debug "Reading data from socket timed out, content offer failed",
333330
contentKeys = offer.contentKeys
334-
break
331+
# Still closing the socket (= sending FIN) but not waiting here for its
332+
# ACK however, so no `closeWait`. Underneath the socket will still wait
333+
# for the FIN-ACK (or timeout) before it destroys the socket.
334+
socket.close()
335+
return
335336

336337
if socket.atEof():
337338
# Destroy socket and not closing as we already received FIN. Closing would

0 commit comments

Comments
 (0)