Skip to content

Group chat: a lock per group so two sends at once can't lose a message - #296

Closed
svaka2000 wants to merge 2 commits into
Open-Coding-Society:masterfrom
svaka2000:fix/group-chat-write-lock
Closed

svaka2000 wants to merge 2 commits into
Open-Coding-Society:masterfrom
svaka2000:fix/group-chat-write-lock

Conversation

@svaka2000

Copy link
Copy Markdown
Contributor

Two messages sent to the same group chat at the same moment are both saved now. Before, one of them could be gone after a reload.

  • Each group's messages are one file in S3 (<group name>/messages-images/messages.jsonl). Sending, editing and deleting a message each read that whole file, change it and upload it again, with no lock.
  • So when two of these ran at once, both read the same old copy, and whichever upload finished last wrote over the other. Both senders got a 200 and the group saw both messages live, since the chat socket pushes each one as it's sent, but after a reload one was gone.
  • An edit during a send could be undone the same way, and a message deleted during a send could come back.
  • I added messageLocks to GroupChatService, one lock per group, the same way fileMetaLocks already locks shared-files-meta.json. addMessage, editMessage and deleteMessage each hold their group's lock while they read, change and upload, so the second one reads the file after the first one has uploaded it.
  • Loading messages doesn't wait for the lock, and groups don't wait for each other. Sends, edits and deletes in one group now go one at a time.
  • The lock only works inside one Spring server. If more than one Spring server ever wrote to the same bucket, this would need S3's conditional writes instead, where S3 turns down an upload if the file changed since it was read.

Other notes:

  • I added GroupChatServiceTest with three cases: two sends at once, an edit during a send, and a delete during a send. On master's GroupChatService all three fail, and with the lock they pass.
  • Its stand-in for S3 holds each read until a second read starts, or half a second passes, so without the lock the two reads always overlap.
  • I also tried it on a local server pointed at a small stand-in S3 that waits 0.2 seconds before each read of the messages file. I sent 10 messages at once. On both master and this branch all 10 got a 200, but master saved 1 and this branch saved all 10.
  • In docs/group-chat-architecture.md I updated the storage note, added GroupChatServiceTest to the testing list and took "Two sends at once can lose one" out of the known issues.
  • 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=GroupChatServiceTest.
  2. To see the bug, put master's copy back with git checkout master -- src/main/java/com/open/spring/mvc/groups/GroupChatService.java and run the test again. All three fail. git checkout HEAD -- src/main/java/com/open/spring/mvc/groups/GroupChatService.java puts the fix back.

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.
@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