Skip to content

Group chat files: 503 instead of 500 when S3 can't store the file - #299

Closed
svaka2000 wants to merge 10 commits into
Open-Coding-Society:masterfrom
svaka2000:fix/shared-file-write-503
Closed

svaka2000 wants to merge 10 commits into
Open-Coding-Society:masterfrom
svaka2000:fix/shared-file-write-503

Conversation

@svaka2000

Copy link
Copy Markdown
Contributor

Sharing a file in a group chat now returns 503 instead of 500 when S3 can't store it for a moment, like #281 did for the path check before the write.

It's built on #298 (all four of these change GroupChatService), so #296, #297 and #298 merge first, in that order; until then this diff includes them, and this PR's own commits start at "Group files: answer 503 when S3 can't take the write".

  • After the path check (Spring asking S3 whether a file is already shared at that path), the upload stored the file with S3FileHandler.uploadFile, which catches every failure and returns null. So when S3 was throttling, down or didn't answer in time, the upload returned 500 "Upload failed", which reads as a bug in our code.
  • Shared files now store with a new uploadFileOrThrow, a strict uploadFile. uploadFile is unchanged for the other S3 features.
  • When S3 is unavailable (throttling or another 5xx, no answer in time, or a RequestTimeout), the upload returns 503 (Service Unavailable, so try again later) with Retry-After: 5 and {message, filename}, the same body as a failed check.
  • The file isn't shared then: no "shared by" record, and no sendFileServer message on the chat socket.
  • S3 refusing the write (a 4xx, like no permission) or a bug here still returns 500 "Upload failed", and every failed write is logged.
  • RequestTimeout is S3's error for a connection that was too slow. It comes as a 400, but the AWS SDK retries it like a 5xx (its retryable error codes).
  • A send's read from Group chat: a send returns 503 instead of wiping the history when S3 can't read it #297 counts a RequestTimeout the same way now, so it gets a 503 there instead of a 400.
  • SharedFileCheckFailedException is now SharedFileStorageUnavailableException, since the write throws it too, and its message reads like "Could not share notes.md in this group. Try again in a moment." One method, isUnavailable, decides it for the path check, the write and the send's read.

Other notes:

  • One case ends the same way the old 500 did: S3 saves the file but its answer doesn't reach Spring. The upload returns 503 and the file stays in S3 without a "shared by" record.
  • Sharing that file again then gets 409 (name taken), which the page handles like any taken name. I noted this in the doc and in a comment at the write.
  • The Messages page doesn't need a change. It shows the same error for a 503 as for a 500, and only treats a 409 differently.
  • I added S3FileHandlerUploadTest and three tests in GroupChatFilesTest: a throttle, a timeout and a RequestTimeout on the write get a 503, an S3 403, another S3 400 and a bug get a 500, and sharing again works once S3 is back.
  • GroupChatSendTest also checks a RequestTimeout on the read.
  • In docs/group-chat-architecture.md I updated the shared files 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='GroupChatFilesTest,S3FileHandlerUploadTest,GroupChatSendTest'.

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.
svaka2000 added a commit to svaka2000/Spring-JJ that referenced this pull request Nov 26, 2026
The group chat fixes (Open-Coding-Society#296 to Open-Coding-Society#299) each add a line after the chat
regression tests, so the reactions line goes above it and the two
change different places.
…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.
At every start, StartupService checks each group's messages file and
creates an empty one for a group that has none. The check used
fileExists, which reads any failure (a throttle, a 5xx, a timeout) as a
missing file, so a check that failed at boot wrote an empty file over
that group's whole history.

ensureGroupStorageExists now checks with fileExistsOrThrow: only S3's
404 creates the file, and any other failure throws before anything is
written. StartupService logs that group with the cause and goes on to
the next one, so the server still starts. The check holds the group's
message lock, so a message sent while the server is starting isn't
written over, and a server without S3 settings checks nothing
(S3FileHandler.isConfigured). The shared-files/ placeholder keeps the
lenient check: it is empty, so writing it again loses nothing.
The storage notes now say StartupService creates a group's empty
messages file and shared-files/ placeholder only when S3 answers 404
(fileExistsOrThrow). Any other failure to check is logged and leaves
that group as it is, the other groups are still checked, the check
holds the group's message lock, and a server without S3 settings
checks nothing. The testing list gets a bullet
for StartupServiceTest, with its command.
After the path check, the upload wrote the file with
S3FileHandler.uploadFile, which catches every failure and answers null. So
when S3 throttled the write, timed out or couldn't be reached, the upload got
a 500 "Upload failed", which reads as a bug in this server. Shared files now
write with uploadFileOrThrow, a strict version beside it, the way
fileExistsOrThrow sits beside fileExists. uploadFile is unchanged for the
other S3 features.

A timeout, no connection or a 5xx on the write now answers 503 with a
try-again message and Retry-After, like a failed check, and the file isn't
shared: no "shared by" record and no socket frame. S3 refusing the write (a
4xx such as no permission) or a bug here is still a 500 "Upload failed", and
every failed write is still logged.

S3's RequestTimeout counts as a timeout too, for the check and the write:
S3 sends it as a 400, but it means the connection was too slow, and the AWS
SDK retries it like a 5xx.

SharedFileCheckFailedException is now SharedFileStorageUnavailableException,
since a failed write throws it too, so the 503 is still answered in one
place. One check (isUnavailable) decides what counts as S3 being unavailable
for both the check and the write. The architecture doc says the same.

Tests cover a throttle, a timeout and a RequestTimeout on the write (503), an
S3 403, an S3 400 and a bug (500), sharing again once S3 is back, and
uploadFileOrThrow itself.
S3's RequestTimeout comes as a 400, but it means the connection was too
slow, and the AWS SDK retries it like a 5xx. The shared-file check and write
already count it as S3 being unavailable (isUnavailable), but the send's
strict read had its own rule that only counted a 5xx, so a timed-out read got
the route's 400 instead of a 503 with Retry-After. The read now uses
isUnavailable too, so one check decides it for the whole group chat. The
history was kept either way: only the status was wrong.

The send test adds a RequestTimeout to the failures that answer 503.
S3 can store a shared file and then fail to answer, and the AWS SDK's own
retries can fail too. The upload then answers 503 without a "shared by"
record or a sendFileServer frame, and sharing it again finds the path taken
(409), which the page handles like any other taken name. Nothing is lost,
and the old 500 ended the same way. Recognizing "this stored file is mine"
on a retry would need a way to match the stored copy to the upload, so for now
the doc and a comment at the write say what happens.
@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