Skip to content

fix(channel): cast null join options to their defaults - #2531

Open
hsusul wants to merge 2 commits into
supabase:mainfrom
hsusul:fix/join-config-null-defaults
Open

hsusul wants to merge 2 commits into
supabase:mainfrom
hsusul:fix/join-config-null-defaults

Conversation

@hsusul

@hsusul hsusul commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Why

A client that joins with "config": {"private": null} joins a public channel, but every broadcast it sends is dropped. The join succeeds and nothing is logged above info level, so the sender sees no error. With ack: true the push never gets a reply. A garbage value like "private": "yes" falls back to false, so null was the only value that broke the channel.

What changed

  • Ecto casts null to nil without calling FlexibleBoolean, so Join.private?/1 returned nil. Neither handle_in("broadcast", ...) clause matches private?: nil (one needs true, the other false), so broadcasts fell through to the catch-all handle_in/3 and were dropped.
  • Added Join.empty_values/0 ([nil | Ecto.Changeset.empty_values()]). Every join payload cast/4 (Config, Broadcast, Presence, PostgresChangesOptions) now passes it, so a null option keeps its default just like an omitted one.
  • This also fixes postgres_changes_options.timeout: null. With wait: true it was clamped to the server maximum (min(nil, max)) instead of the 15 second default.

Scope

Join payload parsing only (lib/realtime_web/channels/payloads/). Values other than null cast exactly as before, including "", which was already an empty value.

Blast Radius

Only joins that send null for private, broadcast.ack/self/replication_ready, presence.enabled/key or postgres_changes_options.wait/timeout change. Those options now read as their documented defaults. Apart from private and timeout, the server already treated nil like false, so their behaviour does not change.

Verification

  • mix test test/realtime_web/channels/payloads/join_test.exs:63 test/realtime_web/channels/realtime_channel_test.exs:424 on the test-only commit: 2 failures. The config came back with private: nil, ack: nil and so on, and no broadcast reply arrived within 100ms. After the fix: 2 passed.
  • mix test test/realtime_web/channels/payloads test/realtime_web/channels/realtime_channel_test.exs test/realtime_web/channels/realtime_channel: 458 passed.
  • mix format --check-formatted: passes.
  • mix compile --warnings-as-errors: fails only on existing Elixir 1.20 type warnings in untouched files (api.ex, application.ex, migrations.ex, forum/muster/scope.ex). The repo pins Elixir 1.19.5, so this was not checked on the pinned version.

A join with "private": null joins a public channel, but its client
broadcasts are dropped. Null broadcast, presence and postgres_changes_options
values should also act as if they were not sent.
Ecto casts null to nil without calling FlexibleBoolean, so "private": null
assigned private?: nil. Neither handle_in("broadcast", ...) clause matches nil,
so the channel joined as public and dropped every broadcast the client sent.
A null postgres_changes_options.timeout also became the server maximum
instead of the 15 second default.

Add Join.empty_values/0 and pass it to every join payload cast, so a null
option keeps its default like an omitted one.

@mangeshraut712 mangeshraut712 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.

Clear root-cause write-up. nil skipping the custom type's cast/1 and then matching neither handle_in("broadcast", …) clause is exactly the kind of silent failure that's hard to spot from the client side. The end-to-end channel test with ack: true is the right regression. Two defence-in-depth suggestions.

`"private": null` would override the `false` default with `nil`.
"""
@spec empty_values() :: list()
def empty_values, do: [nil | Ecto.Changeset.empty_values()]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This fixes the parse side for the four payload modules. The accessors just above still pass the raw field through, though: private?/1 (do: private), ack_broadcast?/1, self_broadcast?/1, presence_enabled?/1. So any future path that leaves one of these nil would bring back the same silent drop. Examples: a new embed that forgets empty_values, or a %Join{} built or patched in code rather than via validate/1. Making the accessors return strict booleans (do: private == true, etc.) would make handle_in's private?: true / private?: false clauses exhaustive by construction. It costs nothing on the happy path.

Related, optional: when a "broadcast" push does reach the catch-all handle_in/3 (L605), the client gets no reply even with ack: true, and the log is at info. Replying {:reply, {:error, %{reason: "..."}}, socket} for that specific event would have made this bug visible to the sender immediately, without flooding Logflare.

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