Skip to content

fix(canopen): grow SDO receive buffers geometrically, report a short RPDO once per run - #263

Merged
dborgards merged 3 commits into
mainfrom
fix/canopen-sdo-growth-and-rpdo-emcy
Oct 3, 2026
Merged

dborgards merged 3 commits into
mainfrom
fix/canopen-sdo-growth-and-rpdo-emcy

Conversation

@dborgards

Copy link
Copy Markdown
Owner

What does this change?

Two findings from the deep review (docs/reviews/2026-09-30-deep-review.md, § 2.8):

  • C11 — The classic SDO client (sizeless or under-declared upload), the block-download server and the block-upload client grew their receive buffer by exactly one segment, copying everything received so far each time: O(N²). A 64 KiB sizeless transfer allocated ~307 MB; the buffers now double (CanOpenNode.GrowCapacity), clamped to MaxSdoTransferBytes (+7 on the block paths, whose last segment may overshoot by six bytes, CANopen: collected minor findings (boundary OOM, registration leak, RTR DLC, doc drift) #59). Results were already trimmed to Offset, so the slack is never visible.
  • C13 — A producer sending a too-short RPDO made the node emit EMCY 8210h for every frame. It is now raised once per run of short frames; a fitting frame or reconfiguring the RPDO ends the run.

C12 (block-download client timer re-armed per sub-block instead of per segment, versus the wording in CanOpenNodeOptions) is not touched; it needs a decision on which side is right.

Type of change

  • feat — new behaviour (minor release)
  • fix / perf — bug or performance fix (patch release)
  • docs / test / refactor / chore / ci — no release
  • Breaking change (! in the title, plus a BREAKING CHANGE: footer explaining the migration)

Checklist

  • dotnet build CanKit.Pro.sln -c Release succeeds (with -p:CI=true)
  • dotnet test CanKit.Pro.sln -c Release passes (net10.0; net48 only runs in CI)
  • Public API changes are documented with XML comments (none; GrowCapacity is internal)
  • New behaviour is covered by a test
  • The requirement or ADR this relates to is referenced (FR-CO-002/003/004, FR-CO-024)

Mutation checks: making GrowCapacity return needed fails the three allocation tests and the unit theory; removing the debounce condition, and removing the reset on a fitting frame, each fail the new EMCY test. The reset on RPDO reconfiguration is not covered by a test.

🤖 Generated with Claude Code

…RPDO once per run

C11: the classic client and both block paths grew a sizeless or under-declared receive buffer by
one segment at a time, copying everything received so far on each segment. A 64 KiB transfer
allocated about 307 MB; it now doubles up to the transfer cap.

C13: a producer sending a too-short RPDO raised EMCY 8210h for every frame. It is now raised once
per run of short frames; a frame that fits, or reconfiguring the RPDO, ends the run.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@cursor

cursor Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches SDO transfer memory behavior and PDO/EMCY handling on the operational path; changes are bounded by existing caps and protocol rules, with broad new tests.

Overview
Fixes two review findings: O(N²) SDO receive-buffer growth and EMCY storms on repeated short RPDOs.

SDO buffers now grow via internal GrowCapacity (double from at least 8 bytes, clamped to MaxSdoTransferBytes, with +7 slack on block paths). Classic segmented upload client, block-download server, and block-upload client use it instead of resizing to exactly the next segment; realloc copies only bytes received so far (Offset), not the whole old array. Large sizeless transfers drop from ~307 MB allocated (64 KiB) to roughly linear total copy/allocation cost.

Short RPDOs still skip actuation and may raise EMCY 8210h once per continuous run of too-short frames (ShortFrameReported on RpdoRuntime). A frame that meets the mapped length or RPDO reconfiguration clears the flag so the next short frame can report again; disabled EMCY (1014h bit 31) emits nothing and does not consume the debounce state.

Tests cover geometric growth (unit theory + GC allocation bounds on .NET 5+) and FR-CO-024 EMCY debounce behavior.

Reviewed by Cursor Bugbot for commit abd93e0. Bugbot is set up for automated code reviews on this repo. Configure here.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-03T07:31:35.569061Z cb1a42b New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 61a5a825f1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/CanKit.Pro.CANopen/CanOpenNode.SdoBlock.cs Outdated
Comment thread tests/CanKit.Pro.Tests/TestCases/CANopen/CanOpenPdoEngineTests.cs Fixed
Comment thread tests/CanKit.Pro.Tests/TestCases/CANopen/CanOpenPdoEngineTests.cs Fixed
Comment thread tests/CanKit.Pro.Tests/TestCases/CANopen/CanOpenPdoEngineTests.cs Fixed
@codecov

codecov Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

dborgards and others added 2 commits October 3, 2026 09:29
…e EMCY-off path

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
GC.GetTotalAllocatedBytes does not exist on net48, which broke the Windows leg's build.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@dborgards
dborgards merged commit 350df5b into main Oct 3, 2026
14 checks passed
@dborgards
dborgards deleted the fix/canopen-sdo-growth-and-rpdo-emcy branch October 3, 2026 07:52
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