fix(gateway): reject unknown rpc methods instead of crashing (#2238)

This commit is contained in:
Hampus
2026-08-31 14:34:35 +02:00
committed by GitHub
parent 0e73346c5f
commit 2dd35c0d6e
4 changed files with 34 additions and 8 deletions
@@ -281,6 +281,8 @@ export class GatewayService {
private readonly MAX_BATCH_CONCURRENCY = 50;
private readonly PENDING_REQUEST_TIMEOUT_MS = ms('30 seconds');
private readonly AUTH_CONTEXT_FALLBACK_MS = ms('5 minutes');
private readonly BADGE_COUNTS_FALLBACK_MS = ms('5 minutes');
private badgeCountsUnsupportedUntil = 0;
constructor() {
this.rpcClient = GatewayRpcClient.getInstance();
@@ -703,15 +705,33 @@ export class GatewayService {
}
async invalidatePushBadgeCounts({userIds}: InvalidatePushBadgeCountsParams): Promise<void> {
if (Date.now() < this.badgeCountsUnsupportedUntil) {
await this.invalidatePushBadgeCountsIndividually(userIds);
return;
}
const batches: Array<Array<UserID>> = [];
for (let index = 0; index < userIds.length; index += PUSH_BADGE_COUNT_BATCH_SIZE) {
batches.push(userIds.slice(index, index + PUSH_BADGE_COUNT_BATCH_SIZE));
}
await Promise.all(
batches.map((batch) =>
this.call('push.invalidate_badge_counts', {user_ids: batch.map((userId) => userId.toString())}),
),
);
try {
await Promise.all(
batches.map((batch) =>
this.call('push.invalidate_badge_counts', {user_ids: batch.map((userId) => userId.toString())}),
),
);
} catch (error) {
const transformedError = this.transformGatewayError(error);
if (!this.isAuthContextUnsupportedError(transformedError)) {
throw transformedError;
}
this.badgeCountsUnsupportedUntil = Date.now() + this.BADGE_COUNTS_FALLBACK_MS;
Logger.warn({error}, '[gateway-rpc] push.invalidate_badge_counts unavailable, falling back to per-user calls');
await this.invalidatePushBadgeCountsIndividually(userIds);
}
}
private async invalidatePushBadgeCountsIndividually(userIds: ReadonlyArray<UserID>): Promise<void> {
await Promise.all(userIds.map((userId) => this.invalidatePushBadgeCount({userId})));
}
async invalidatePushSubscriptions({userId}: InvalidatePushSubscriptionsParams): Promise<void> {
@@ -33,7 +33,9 @@ execute_method(<<"process.voice_state_counts">>, _Params) ->
gateway_rpc_misc_presence:collect_and_aggregate_voice_state_counts(ActiveNodes);
execute_method(<<"process.active_voice_rooms">>, _Params) ->
ActiveNodes = gateway_node_router:active_nodes(),
gateway_rpc_misc_push:collect_and_aggregate_active_voice_rooms(ActiveNodes).
gateway_rpc_misc_push:collect_and_aggregate_active_voice_rooms(ActiveNodes);
execute_method(Method, _Params) ->
gateway_rpc_error:raise(<<"Unknown method: ", Method/binary>>).
-spec get_local_node_id() -> binary().
get_local_node_id() ->
@@ -20,7 +20,9 @@ execute_method(<<"presence.terminate_all_sessions">>, P) -> handle_terminate_all
execute_method(<<"presence.has_active">>, P) -> handle_has_active(P);
execute_method(<<"presence.add_temporary_guild">>, P) -> handle_add_temp_guild(P);
execute_method(<<"presence.remove_temporary_guild">>, P) -> handle_remove_temp_guild(P);
execute_method(<<"presence.sync_group_dm_recipients">>, P) -> handle_sync_dm_recipients(P).
execute_method(<<"presence.sync_group_dm_recipients">>, P) -> handle_sync_dm_recipients(P);
execute_method(Method, _Params) ->
gateway_rpc_error:raise(<<"Unknown method: ", Method/binary>>).
-spec handle_dispatch(map()) -> true.
handle_dispatch(#{<<"user_id">> := UserIdBin, <<"event">> := Event, <<"data">> := Data}) ->
@@ -17,7 +17,9 @@ execute_method(<<"push.invalidate_badge_counts">>, P) ->
execute_method(<<"push.clear_channel_notifications">>, P) ->
do_clear_channel_notifications(P);
execute_method(<<"push.invalidate_subscriptions">>, P) ->
do_invalidate_subscriptions(P).
do_invalidate_subscriptions(P);
execute_method(Method, _Params) ->
gateway_rpc_error:raise(<<"Unknown method: ", Method/binary>>).
-spec do_sync_user_guild_settings(map()) -> true.
do_sync_user_guild_settings(#{