Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
peer_data: Ignore in-flight removals when fetching a user's channels.
Shubham Padiab6d0518d2a3a
diff --git a/web/src/peer_data.ts b/web/src/peer_data.tsindex 7a1d3a64e66..c435e30c322 100644--- a/web/src/peer_data.ts+++ b/web/src/peer_data.ts@@ -56,7 +56,18 @@ const pending_subscriber_requests = new Map<}>();// Requests for subscriptions for a user-const pending_subscription_requests = new Map<number, Promise<void>>();+const pending_subscription_requests = new Map<+ number,+ {+ subscriptions_promise: Promise<void>;+ // Channels the user was removed from while the request was in+ // flight. The server may have built its response before those+ // removals, so we skip these channels instead of re-adding the+ // user from stale data. We don't need to track additions: the+ // event already added the user, and re-adding them is harmless.+ stream_ids_removed_during_fetch: Set<number>;+ }+>();export function clear_for_testing(): void {stream_subscribers.clear();@@ -415,6 +426,7 @@ export function remove_subscriber(stream_id: number, user_id: number): void {const subscribers = get_loaded_subscriber_subset(stream_id);decrement_subscriber_count(subscribers, stream_id, user_id);subscribers.delete(user_id);+ pending_subscription_requests.get(user_id)?.stream_ids_removed_during_fetch.add(stream_id);}export function bulk_add_subscribers({@@ -464,6 +476,9 @@ export function bulk_remove_subscribers({decrement_subscriber_count(subscribers, sub.stream_id, user_id);}subscribers.delete(user_id);+ pending_subscription_requests+ .get(user_id)+ ?.stream_ids_removed_during_fetch.add(stream_id);}if (pending_subscriber_requests.has(stream_id)) {@@ -537,7 +552,16 @@ async function fetch_subscriptions_for_user_from_server(user_id: number): Promissuccess(raw_data) {const subscriptions =fetch_user_subscriptions_response_schema.parse(raw_data).subscribed_channel_ids;+ const pending_request = pending_subscription_requests.get(user_id)!;for (const stream_id of subscriptions) {+ if (pending_request.stream_ids_removed_during_fetch.has(stream_id)) {+ continue;+ }+ // We already have the exact subscriber set for this+ // stream, so a stale response must not re-add the user.+ if (fetched_stream_ids.has(stream_id)) {+ continue;+ }add_subscriber(stream_id, user_id);}fetched_user_subscriptions.add(user_id);@@ -567,7 +591,7 @@ export async function fetch_subscriptions_for_user(user_id: number): Promise<voi}if (pending_subscription_requests.has(user_id)) {- return pending_subscription_requests.get(user_id)!;+ return pending_subscription_requests.get(user_id)!.subscriptions_promise;}const subscriptions_promise = (async () => {@@ -577,6 +601,8 @@ export async function fetch_subscriptions_for_user(user_id: number): Promise<voiblueslip.warn("Failure fetching user's subscribed channels. Retrying.", {user_id,});+ // The retry's response will already reflect these removals.+ pending_subscription_requests.get(user_id)!.stream_ids_removed_during_fetch.clear();}const result = await fetch_subscriptions_for_user_from_server(user_id);// Bad request, so just give up here.@@ -604,7 +630,10 @@ export async function fetch_subscriptions_for_user(user_id: number): Promise<voi}})();- pending_subscription_requests.set(user_id, subscriptions_promise);+ pending_subscription_requests.set(user_id, {+ subscriptions_promise,+ stream_ids_removed_during_fetch: new Set(),+ });return subscriptions_promise;}diff --git a/web/tests/peer_data.test.cjs b/web/tests/peer_data.test.cjsindex a077dd65ef1..5e5e018c5f0 100644--- a/web/tests/peer_data.test.cjs+++ b/web/tests/peer_data.test.cjs@@ -721,3 +721,103 @@ test("fetch_subscriptions_for_user", async () => {await peer_data.fetch_subscriptions_for_user(fred.user_id);assert.equal(channel_get_calls, 0);});++test("fetch_subscriptions_for_user stale response", async () => {+ people.add_active_user(fred);++ const social = make_stream({+ stream_id: 2,+ name: "social",+ subscribed: true,+ });+ stream_data.add_sub_for_tests(social);+ const rome = make_stream({+ name: "Rome",+ subscribed: true,+ stream_id: 1001,+ });+ stream_data.add_sub_for_tests(rome);+ const devel = make_stream({+ name: "devel",+ subscribed: true,+ stream_id: 1,+ });+ stream_data.add_sub_for_tests(devel);+ const paris = make_stream({+ name: "Paris",+ subscribed: true,+ stream_id: 1002,+ });+ stream_data.add_sub_for_tests(paris);++ peer_data.set_subscribers(social.stream_id, [fred.user_id], false);+ peer_data.set_subscribers(rome.stream_id, [fred.user_id], false);+ peer_data.set_subscribers(devel.stream_id, [fred.user_id], false);+ peer_data.set_subscribers(paris.stream_id, [fred.user_id], false);++ // The server computed this response before the removals below.+ mock_channel_get(channel, (opts) => {+ opts.success({+ subscribed_channel_ids: [+ social.stream_id,+ rome.stream_id,+ devel.stream_id,+ paris.stream_id,+ ],+ });+ });++ // The mocked response is only delivered once we await the promise,+ // so the removals below arrive while the request is in flight.+ const promise = peer_data.fetch_subscriptions_for_user(fred.user_id);+ peer_data.bulk_remove_subscribers({+ stream_ids: [social.stream_id],+ user_ids: [fred.user_id],+ });+ peer_data.remove_subscriber(devel.stream_id, fred.user_id);+ // While the request is in flight, we get the exact subscriber set+ // for Paris, which no longer includes fred.+ peer_data.set_subscribers(paris.stream_id, [me.user_id]);+ assert.ok(!peer_data.subscriber_data_loaded_for_user(fred.user_id));+ await promise;++ // The stale response must not re-add fred to social, devel, or Paris.+ assert.ok(!stream_data.is_user_loaded_and_subscribed(social.stream_id, fred.user_id));+ assert.ok(!stream_data.is_user_loaded_and_subscribed(devel.stream_id, fred.user_id));+ assert.ok(!stream_data.is_user_loaded_and_subscribed(paris.stream_id, fred.user_id));+ assert.ok(stream_data.is_user_loaded_and_subscribed(rome.stream_id, fred.user_id));+ assert.ok(peer_data.subscriber_data_loaded_for_user(fred.user_id));+});++test("fetch_subscriptions_for_user removal before retry", async () => {+ people.add_active_user(fred);++ const social = make_stream({+ stream_id: 2,+ name: "social",+ subscribed: true,+ });+ stream_data.add_sub_for_tests(social);+ peer_data.set_subscribers(social.stream_id, [fred.user_id], false);++ let num_attempts = 0;+ mock_channel_get(channel, (opts) => {+ num_attempts += 1;+ if (num_attempts === 1) {+ // fred leaves social while the first attempt is in flight,+ // and then that attempt fails.+ peer_data.remove_subscriber(social.stream_id, fred.user_id);+ opts.error({status: 500});+ return;+ }+ // fred rejoined social before the retry, so the retry's+ // response lists social and must be applied.+ opts.success({+ subscribed_channel_ids: [social.stream_id],+ });+ });+ blueslip.expect("warn", "Failure fetching user's subscribed channels. Retrying.");+ await peer_data.fetch_subscriptions_for_user(fred.user_id);+ assert.equal(num_attempts, 2);+ assert.ok(stream_data.is_user_loaded_and_subscribed(social.stream_id, fred.user_id));+});