Skip to content

fix(rtc): avoid mutable default and accept None in LocalParticipant.publish_data - #905

Open
NabiBukhsh-AI wants to merge 2 commits into
livekit:mainfrom
NabiBukhsh-AI:fix/rtc-participant-publish-data-destinations
Open

NabiBukhsh-AI wants to merge 2 commits into
livekit:mainfrom
NabiBukhsh-AI:fix/rtc-participant-publish-data-destinations

Conversation

@NabiBukhsh-AI

Copy link
Copy Markdown
Contributor

Summary

LocalParticipant.publish_data defined its destination parameter with a mutable default:

async def publish_data(
    self,
    payload: Union[bytes, str],
    *,
    reliable: bool = True,
    destination_identities: List[str] = [],
    topic: str = "",
) -> None:
    ...
    req.publish_data.destination_identities.extend(destination_identities)

This caused two issues:

  1. Mutable default argument (ruff B006): destination_identities: List[str] = [] is shared across calls.
  2. TypeError crash on None: Passing destination_identities=None (common when forwarding an optional argument from caller code) caused req.publish_data.destination_identities.extend(None) to raise:
    TypeError: Value must be iterable
    
  3. API Inconsistency: Every other method on LocalParticipant that takes destination identities (publish_dtmf, send_chat_message, stream_bytes, stream_text) defaults to None and conditionally extends the request with if destination_identities:.

Changes

  • Changed destination_identities default to None with type Optional[Sequence[str]] = None, accepting tuples, lists, or None.
  • Conditionally extends req.publish_data.destination_identities only when destination_identities is provided.
  • Updated docstring documentation.

Testing

Added unit tests in tests/rtc/test_participant_publish_data.py:

  • test_publish_data_parameter_default_is_none: verifies publish_data signature default is None.

  • test_publish_data_accepts_none_and_sequences: verifies publish_data succeeds when called with:

    • Default (omitted) -> broadcasts (empty list in request)
    • Explicit destination_identities=None -> broadcasts
    • Explicit destination_identities=[] -> broadcasts
    • List of identities (["user1", "user2"])
    • Tuple of identities (("user3", "user4"))
  • uv run pytest tests/rtc/test_participant_publish_data.py -v -W error (2/2 passed)

  • uv run ruff check livekit-rtc/livekit/rtc/participant.py tests/rtc/test_participant_publish_data.py (passed)

  • uv run ruff format --check livekit-rtc/livekit/rtc/participant.py tests/rtc/test_participant_publish_data.py (passed)

  • uv run mypy livekit-rtc/livekit/rtc/participant.py tests/rtc/test_participant_publish_data.py (0 issues)

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@NabiBukhsh-AI

Copy link
Copy Markdown
Contributor Author

Hi @cloudwebrtc @lukasIO,

Gentle check-in on this PR. It avoids mutable default arguments in LocalParticipant.publish_data (destination_identities: list[str] | None = None) and normalizes None to an empty list to prevent cross-call parameter mutation.

All CI checks and Devin review are green. Ready for review whenever you have a moment!

@cloudwebrtc cloudwebrtc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! Dropping the mutable default and accepting None is a good change, and the new test looks sound. One thing to address before merging (inline): widening to Sequence[str] lets a bare string through the type checker.

Also a nit, outside the diff: send_text, stream_text, send_file and stream_bytes in the same class still take Optional[List[str]] for destination_identities, so after this PR a tuple type-checks for publish_data but not for the others. It would be nice to use the same annotation (and the same bare-str guard) across all of them so the public API stays consistent.

Comment thread livekit-rtc/livekit/rtc/participant.py Outdated
*,
reliable: bool = True,
destination_identities: List[str] = [],
destination_identities: Optional[Sequence[str]] = None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A plain str is itself a Sequence[str], so with this annotation

await lp.publish_data(b"hi", destination_identities="alice")

now passes mypy/pyright, where List[str] used to flag it. At runtime extend() then iterates it character by character and the packet is addressed to ['a', 'l', 'i', 'c', 'e'], so it silently reaches no one. (The runtime behavior was the same before; what regresses is the type checker catching it.)

Could you guard against it, e.g.

if isinstance(destination_identities, str):
    raise TypeError("destination_identities must be a sequence of identities, not a str")

or keep the annotation as List[str]?

…List[str]]

Widening the annotation to Sequence[str] let a bare str through the type
checker, since a str is itself a Sequence[str]; at runtime it was then
sent as one identity per character. Use Optional[List[str]] = None, the
same annotation send_text, stream_text, send_file and stream_bytes use,
so the type checker flags it again and the API stays consistent.

Add a changeset for the release notes.
@NabiBukhsh-AI

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @cloudwebrtc, and good catch on the bare str.

In 36aebe9 I've gone back to Optional[List[str]] = None, the same annotation send_text, stream_text, send_file and stream_bytes already use. So mypy flags destination_identities="alice" again (incompatible type "str"; expected "list[str] | None"), and all five methods are consistent.

The test now covers None, an empty list, a list (left unmodified), and a later call that omits the argument. I also added a changeset.

This branch has not been deployed

No deployments
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