Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

echo: Guard failed-send handlers against a missing message.

Aman Agrawal952b1c7b96df
diff --git a/web/src/echo.ts b/web/src/echo.ts
index 3719dbd7298..5a0f9483b8c 100644
--- a/web/src/echo.ts
+++ b/web/src/echo.ts
@@ -147,7 +147,14 @@ function show_failed_message_success(message_id: number): void {
}
function failed_message_success(message_id: number): void {
- message_store.get(message_id)!.failed_request = false;
+ const message = message_store.get(message_id);
+ if (message === undefined) {
+ // A get-events delivery may already have reconciled the message to its
+ // real id, in which case the resend's reify_message_id early-returned
+ // and this (duplicate) id was never stored -- nothing to un-fail.
+ return;
+ }
+ message.failed_request = false;
show_failed_message_success(message_id);
}
@@ -623,7 +630,12 @@ export function process_from_server(messages: ServerMessage[]): ServerMessage[]
export let message_send_error = (message_id: number, error_response: string): void => {
// Error sending message, show inline
- const message = message_store.get(message_id)!;
+ const message = message_store.get(message_id);
+ if (message === undefined) {
+ // The message is no longer in the store -- e.g. it was removed while a
+ // (re)send was in flight -- so there is no failed send to surface.
+ return;
+ }
message.failed_request = true;
message.show_slow_send_spinner = false;
diff --git a/web/tests/echo.test.cjs b/web/tests/echo.test.cjs
index 51b03bf76e8..cfb797ef103 100644
--- a/web/tests/echo.test.cjs
+++ b/web/tests/echo.test.cjs
@@ -572,3 +572,90 @@ run_test("resend error re-marks a present message as failed", ({override}) => {
assert.equal(message.failed_request, true);
});
+
+run_test("resend doesn't crash when the message was already reconciled", ({override}) => {
+ // The original send appeared to fail on the client but had actually
+ // reached the server. The user resent it; then the original's get-events
+ // delivery arrived and reconciled the local echo to the real server id,
+ // consuming the local id. By the time the resend's response arrives,
+ // reify_message_id early-returns (the local id is gone), so the resent
+ // message is never stored under the id the response reports, and
+ // failed_message_success must tolerate that missing entry.
+ const stored_messages = new Map();
+ override(message_store, "get", (id) => stored_messages.get(id));
+
+ const local_id = "250.01";
+ const reconciled_id = 260; // server id the message was reconciled to
+ // The resend reached the server too and, absent server-side
+ // deduplication, created a second message whose id its response reports.
+ const resend_response_id = 261;
+
+ // The message has already been reconciled: keyed under its real server id,
+ // no longer locally echoed, and no longer failed.
+ const message = {
+ id: reconciled_id,
+ local_id,
+ type: "stream",
+ stream_id: general_sub.stream_id,
+ topic: "test",
+ raw_content: "hello world",
+ content: "<p>hello world</p>",
+ locally_echoed: false,
+ failed_request: false,
+ };
+ stored_messages.set(reconciled_id, message);
+
+ const on_send_message_success = (msg, data) => {
+ // compose.send_message_success calls echo.reify_message_id, which
+ // early-returns here because the local id was already consumed.
+ echo.reify_message_id(msg.local_id, data.id);
+ };
+ const send_message = (_msg, on_success) => {
+ on_success({id: resend_response_id});
+ };
+
+ echo.resend_message(message, make_spinner_row(), {on_send_message_success, send_message});
+
+ // The resend's success path only reconciles the message it resent; it does
+ // not store a message under the response's id. (A second message created
+ // on the server would instead arrive via its own get-events delivery.) So
+ // the store has no entry there, and the reconciled message stays un-failed.
+ assert.equal(stored_messages.get(resend_response_id), undefined);
+ assert.equal(message.failed_request, false);
+});
+
+run_test("resend error on a message removed from the store doesn't crash", ({override}) => {
+ // If the resend POST fails after the message was removed from the store
+ // (e.g. it was deleted while the resend was in flight), message_send_error
+ // must tolerate the missing entry rather than dereference undefined.
+ const stored_messages = new Map();
+ override(message_store, "get", (id) => stored_messages.get(id));
+
+ const message = {
+ id: 270,
+ local_id: "270.01",
+ type: "stream",
+ stream_id: general_sub.stream_id,
+ topic: "test",
+ raw_content: "gone",
+ content: "<p>gone</p>",
+ locally_echoed: true,
+ failed_request: false,
+ };
+ // The message is no longer in the store (stored_messages is empty).
+
+ const send_message = (_msg, _on_success, on_error) => {
+ on_error("error response", "");
+ };
+
+ // If message_send_error threw on the missing store entry, resend_message
+ // would propagate it and fail this test.
+ echo.resend_message(message, make_spinner_row(), {
+ on_send_message_success: noop,
+ send_message,
+ });
+
+ // The detached message was left untouched, since there was no stored entry
+ // to mark as failed.
+ assert.equal(message.failed_request, false);
+});