Skip to content

Group chat: a send returns 503 instead of wiping the history when S3 can't read it - #297

Closed
svaka2000 wants to merge 5 commits into
Open-Coding-Society:masterfrom
svaka2000:fix/group-chat-read-failure
Closed

svaka2000 wants to merge 5 commits into
Open-Coding-Society:masterfrom
svaka2000:fix/group-chat-read-failure

Conversation

@svaka2000

@svaka2000 svaka2000 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Sending a group chat message can't wipe the group's history anymore when Spring can't read the group's messages file from S3.

It's built on #296 (all four of these change GroupChatService), so #296 merges first; until then this diff includes #296, and this PR's own commits start at "Group chat: a send keeps the history when S3 can't read it".

  • A send reads the group's whole messages file, adds the message and uploads the file again. Before, it read with S3FileHandler.decodeFile, which returns null for any failure, so a failed read looked the same as a group with no messages yet.
  • So when S3 failed that read (a throttle, a 5xx or a timeout) but took the upload right after it, the send saved a file with only the new message over the whole history. It still answered 200, and the group saw the message live.
  • The send now reads with a new decodeFileOrThrow, a strict decodeFile, the same way fileExistsOrThrow (Group chat files: an index without the bytes, one-file downloads, and no overwriting #280) is a strict fileExists. Only a 404 from S3 (no file at that path) means there are no messages yet.
  • If that read fails because S3 is throttling, down or doesn't answer in time, nothing is saved or sent to the group. The route returns 503 (Service Unavailable, so try again later) with Retry-After: 5 and a try-again message.
  • If S3 refuses the read (a 4xx, like no permission) or there's a bug, nothing is written either, and the route's catch-all still answers 400, same as on master. I left that alone so this PR stays about the history.
  • Every read S3 fails or refuses is logged with its cause.
  • Edits and deletes still read with decodeFile, but a failed read can't wipe anything there: they don't find the message, so they write nothing.
  • One gap is the same as before: a send whose read works but whose upload fails still answers 200 and isn't saved, since the upload still uses uploadFile.

Other notes:

  • I added GroupChatSendTest, which sends through the controller to the real S3FileHandler over a stand-in S3 client. A throttle and a timeout keep the stored file byte for byte and get a 503, and an S3 403 and a bug write nothing and get a 400.
  • On master's GroupChatService the throttle and 403 cases fail: the send answers 200.
  • It also checks that a group with no messages file starts its history, and that a server with no S3 settings still sends live. S3FileHandlerDecodeTest covers decodeFileOrThrow itself.
  • The test builds the controller with a small TestBeans helper that gives any constructor dependency it doesn't pass a mock, so it keeps compiling and running when Chat reactions: react to a DM or group message with an emoji #302 or Mentors: no direct messages, and only the groups they're added to #303 adds one.
  • The Messages page doesn't need a change. It already shows an error when a send fails, like "Could not send the message. Try again."
  • In docs/group-chat-architecture.md I updated the send route and the storage notes and added the new tests to the testing list.
  • The full suite passes except ImportPersonPasswordTest, which needs a local .env, and PersonCapstoneApprovalRouteTest, which fails the same way on master b94d9f3 (its Spring context doesn't load).

How to try it:

  1. Run ./mvnw test -Dtest='GroupChatSendTest,S3FileHandlerDecodeTest'.

Sending, editing and deleting a message each read the group's whole messages
file from S3, change it and write the file back, with no lock. Two at once
both read the same old copy, so the later write dropped the other's change:
two people sending at the same moment lost a message, and an edit or delete
during a send lost the edit or the new message. They now take one lock per
group, the same pattern as the shared-files metadata lock. Reads don't wait,
and other groups don't wait on each other.

The lock only works within one server. Several servers writing the same file
would need S3's conditional writes instead.

Tests run two sends, an edit and a send, and a delete and a send at once
against an S3 stub that holds each read until the other one has started.
The storage notes said every send rewrites the file with no lock, and Known
issues listed two sends at once losing one. The note now says sending,
editing and deleting take one lock per group, within one server only. The
testing list names GroupChatServiceTest, and the known issue is gone.
A send reads the group's whole messages file, adds the message and writes
the file back. decodeFile answers null for any failure, not only a missing
file, so when S3 timed out, was throttled or answered a 5xx, the send took
the chat for empty and wrote the one new message over the whole history. It
still answered 200 and broadcast the message.

The send now reads with a strict decodeFileOrThrow, like fileExistsOrThrow:
only S3's 404 means no messages yet. A timeout, no connection or a 5xx
throws MessagesReadFailedException, nothing is saved or broadcast, and
POST /api/groups/chat/{id}/messages answers 503 with Retry-After, the same
as a shared-file check that fails. S3 refusing the read (a 4xx) or a bug
here also writes nothing and gets the route's usual 400, since waiting
won't fix it. S3 failures to read are logged with their cause. Without S3
settings a send still broadcasts live, as before. decodeFile, edits and
deletes are unchanged.

Tests send through the controller to a real S3FileHandler over a stand-in
S3 client: a 503 and a timeout keep the stored file byte for byte and
answer 503, a 403 and a bug write nothing and get no retry, a missing file
starts the history, and a server without S3 still broadcasts. The two older
tests that stub S3 now stub the strict read a send makes.
The send route and the storage notes now say a send reads the messages
file with decodeFileOrThrow: S3 being unavailable answers 503 with
Retry-After and nothing is saved or broadcast, S3 refusing the read or a
bug gets the route's usual 400, and edits and deletes still read with
decodeFile. The testing list gets a bullet
for GroupChatSendTest and S3FileHandlerDecodeTest, with the command that
runs the S3 handler tests.
…or's types

GroupChatSendTest built GroupChatApiController, GroupChatRealtimeService and
GroupChatReadService by naming their constructors. Open PRs add constructor
dependencies to the controller and the realtime service (Open-Coding-Society#302 a
MessageReactionService, Open-Coding-Society#303 a MentorAccess), so once one of them and this
test were both on master, the tests would stop compiling, with no merge
conflict to show it.

TestBeans builds a class through its biggest constructor: each parameter gets
the first object the test passes that fits it, and a Mockito mock otherwise.
The test then keeps compiling and running whatever dependencies those classes
gain.
@svaka2000

Copy link
Copy Markdown
Contributor Author

Combined into #314 with the same commits.

@svaka2000 svaka2000 closed this Nov 26, 2026
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.

1 participant