Skip to content

feat(session): per-call max_payload_bytes and max_blocks on get() - #131

Merged
QuiteYellow merged 2 commits into
QuiteYellow:mainfrom
mbillow:claude/dtls-coap-block-limits-qvik01
Nov 13, 2026
Merged

QuiteYellow merged 2 commits into
QuiteYellow:mainfrom
mbillow:claude/dtls-coap-block-limits-qvik01

Conversation

@mbillow

@mbillow mbillow commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Follows up on mbillow/localthings#659 (comment).

An appliance lists files on /file/list/vs/0 and serves them as a Block2 transfer from /file/transfer/vs/0. The fixed reassembly caps stop that transfer. BLOCK2/SIZE2 are managed options, so the caller can't fetch the blocks itself, at any timeout.

What this does

DtlsCoapSession.get() gets two keyword-only arguments, both defaulting to None:

  • max_payload_bytes: the assembled-byte cap. None keeps the accumulator's default (MAX_BLOCK2_PAYLOAD_BYTES).
  • max_blocks: the block-count cap. None keeps self.MAX_BLOCKS, so a subclass override still applies.

Both are passed through _blockwise_get to both _blockwise_get_once attempts, so an ETag restart keeps the caller's caps, and into Block2Accumulator. No other caller passes them, so the Observe refetch path is unchanged. Defaults, BLOCK_SZX, _BLOCK_MAX_ATTEMPTS, _BLOCK_ACK_TIMEOUT and the timeout semantics are unchanged too.

A bool, a non-int, or a value ≤ 0 raises in get() before a token is minted or a datagram sent. Type problems raise TypeError and range problems raise ValueError, the same split get() already uses for extra_options. The accumulator itself raises ValueError for both. I followed get()'s convention; say if you'd rather match the accumulator.

Two changes

  1. max_payload_bytes: what you agreed to in the thread.
  2. max_blocks: an addition you haven't agreed to, but I think may be needed.

MAX_BLOCKS is 64, which is the 64 KiB byte cap in 1024-byte blocks. So at 1 KiB blocks the two caps are the same limit, and raising max_payload_bytes alone doesn't carry a transfer past 64 KiB. The block count stops it at the same point (test_max_payload_bytes_alone_stops_at_the_default_block_cap). With commit 1 alone, a caller also needs a subclass that raises MAX_BLOCKS. That works and is tested, but it's per-session and not per-call.

With small blocks it's worse. If a server answers block 0 at 1024 bytes and then advertises SZX 2, the 64th block arrives at about 5 KB, whatever the byte cap is. The device log in the localthings report looks like that pattern (32–64 B blocks after the first one), but I haven't measured it here. The tests use a fake device with that pattern and don't describe any appliance.

Tests

New file tests/test_get_block2_limits.py (30 tests). It uses the synchronous fake device from test_block2_get_cap.py, extended to downshift after block 0 and to change ETag on the first transfer. It covers:

  • Defaults unchanged: a transfer of 64 KiB + 1 fails after 64 requests, and 65 blocks of 64 B also fail after 64.
  • Each cap raised alone completes the transfer it should, and still stops at the other cap.
  • Both raised: transfers over both default caps complete, at 1 KiB blocks and at 64 B blocks.
  • A cap below what the transfer needs fails the same way the default cap does.
  • A subclass MAX_BLOCKS applies when max_blocks is None.
  • True, 0, -1, "1024" and 1.5 for each argument raise before any datagram is sent or token is minted.
  • Raised caps don't carry over to the next get().
  • The ETag restart runs under a fresh token with the caller's caps.

I checked that the tests catch mistakes. Dropping the caps from the ETag-restart call fails test_etag_restart_uses_the_callers_block_cap. Using MAX_BLOCK2_BLOCKS in place of self.MAX_BLOCKS fails the two subclass tests.

Python 3.11  1394 passed, 49 warnings
Python 3.12  1394 passed, 49 warnings
Python 3.13  1394 passed, 49 warnings
Python 3.14  1394 passed, 49 warnings
floor (3.11, cbor2 5.6.0, cryptography 38.0.0, pyOpenSSL 23.1.0, pytest 8.0.0)  1394 passed
tools/check_share_safety.py --changed-since main  clean
python -m build + tools/check_distribution.py  distribution contents verified

main gives the same 49 warnings. docs/api.md was regenerated with tools/generate_api_docs.py.

get() always reassembled under the accumulator's default 64 KiB cap,
and BLOCK2 and SIZE2 are managed options, so a caller had no way to
read a larger representation at any timeout. A file listed under
/file/list/vs/0 and served by /file/transfer/vs/0 is one such read,
reported through localthings.

Block2Accumulator already takes max_payload_bytes. get() now does too,
keyword-only, and passes it through _blockwise_get to both attempts of
_blockwise_get_once, so an ETag restart keeps the caller's cap. None,
the default, leaves the accumulator's own default in place. Nothing
else calls _blockwise_get with it, so the Observe refetch is unchanged.

A bool, a non-int, or a value <= 0 raises before anything is sent:
TypeError for the type, ValueError for the range, the same split get()
already uses for extra_options.

On its own this does not carry a transfer past 64 KiB. MAX_BLOCKS is
the default byte cap in 1024-byte blocks, so the block count stops the
transfer at the same point unless a subclass raises it. The tests pin
that as well as the subclass route.
The block count is the second cap, and with small blocks it is the one
that ends a transfer. MAX_BLOCKS is 64, derived from 1024-byte blocks.
A server that answers block 0 at 1024 bytes and then advertises SZX 2
delivers 64-byte blocks after it, and the 64th block arrives at about
5 KB, far inside any byte cap. The device log in the localthings report
is consistent with that shape; it was not measured here.

get() now takes max_blocks beside max_payload_bytes, keyword-only and
validated the same way. None keeps self.MAX_BLOCKS, so a subclass
override still applies. The two stay independent: neither is derived
from the other or from the negotiated SZX, and raising one leaves the
other at its default. Both are threaded to the ETag restart.

This is separate from the previous commit so it can be dropped on its
own.
@mbillow

mbillow commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

@QuiteYellow This is what we talked about in mbillow/localthings#659. Let me know if you have any feedback!

@QuiteYellow

Copy link
Copy Markdown
Owner

Thanks @mbillow, I'll run an automated review then take a look properly over the next few days.

@QuiteYellow-reviewer QuiteYellow-reviewer left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review at 07a1e97.

Not run: no local build or test run. CI passes on all 8 checks.

One documentation point, inline. No code defects found. The validation, the None defaults, the ETag restart path and the unchanged Observe refetch path all match the PR description.

@QuiteYellow

Copy link
Copy Markdown
Owner

Thanks for this. Merging as it stands.

On the question in the description: keep the split. It matches what get() already does for extra_options, a wrong type is a TypeError and a bad value is a ValueError.

I'll follow up with a small docs-only PR: the docstring will say that timeout covers the whole transfer, pacing included, so a large max_blocks needs a longer timeout, and "raise the caps" becomes "set". Nothing for you to do.

@QuiteYellow
QuiteYellow merged commit f25fa44 into QuiteYellow:main Nov 13, 2026
8 checks passed
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.

3 participants