Skip to content

feat: Add message size validation to Bridge#59

Draft
beanrepo wants to merge 2 commits intoarduino:mainfrom
beanrepo:log-bridge-message-limits
Draft

feat: Add message size validation to Bridge#59
beanrepo wants to merge 2 commits intoarduino:mainfrom
beanrepo:log-bridge-message-limits

Conversation

@beanrepo
Copy link
Contributor

@beanrepo beanrepo commented Jan 26, 2026

Add message size validation to Bridge

The Arduino router bridge has a message size limitation of 256 bytes. When messages exceeding this limit are sent, the application crashes or blocks threads/processes instead of handling the error gracefully.

This PR adds validation logic to check message sizes before sending them through the bridge, with appropriate error handling for each message type.

Changes

  • Added _max_message_size constant: Set to 256 bytes to match Arduino router limits
  • notify() method:
    • Validates message size before sending
    • Logs error and drops oversized messages (fire-and-forget semantics)
    • Prevents silent failures
  • call() method:
    • Validates message size before sending
    • Raises ValueError with clear error message for oversized requests
    • Properly cleans up callbacks before raising exception
  • _send_response() method:
    • Checks response size before sending
    • Sends minimal error response when payload exceeds limit
    • Prevents response-related crashes

Benefits

  • ✅ Prevents application crashes from oversized messages
  • ✅ Avoids thread/process blocking
  • ✅ Provides clear error logging and exceptions
  • ✅ Maintains backward compatibility with existing code

References



_reconnect_delay = 3.0 # seconds
_max_message_size = 256 # Maximum message size supported by Arduino router (bytes)
Copy link
Contributor

@stefanotorneo stefanotorneo Jan 26, 2026

Choose a reason for hiding this comment

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

Could you add the link of the Arduino router code?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if we need to add here.
Router should fail with proper error (payload too large) so ve can react properly.
We need to have this validation in a single place, imho

Copy link
Collaborator

@dsammaruga dsammaruga Jan 26, 2026

Choose a reason for hiding this comment

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

@rjtokenring I agree with this approach to be more consistent and future-proof with Arduino Router and RPCLite. This needs to be addressed with an issue or PR on these repositories, because at the moment it is not possible to let users know when a message exceed this limit.

Copy link
Contributor

Choose a reason for hiding this comment

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

It was just to understand where that value comes from...
Also a link in the PR description would be okay.

Copy link
Collaborator

@dsammaruga dsammaruga Jan 30, 2026

Choose a reason for hiding this comment

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

@stefanotorneo referral links have been added to PR description. Anyway, this PR will be refactored to handle this error type according to previous discussion in this thread. We are waiting for an improvement in router bridge repository to handle a specific error code via RPC. Open issue here

self._send_bytes(msgpack.packb(request))
packed_data = msgpack.packb(request)
if len(packed_data) > _max_message_size:
with self.callbacks_lock:
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this necessary? It is inside a try except, and in the except there are the same instructions.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants